Question

I tried to build my first custom gui element but I get problems when I try to change appearance or adapter (using Gallery) later in code.

My Problem: I can't change Custom Gallery properties

My actual Code:

First I create an XML which is the widget customGallery.xml

<?xml version="1.0" encoding="utf-8"?>


<merge xmlns:android="http://schemas.android.com/apk/res/android">       
    <ImageButton android:id="@+id/toLeft"
    android:background="@drawable/arrow_left"
        android:layout_width="wrap_content"
        android:layout_height="40dip"
        android:layout_marginBottom="1dip" />
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_toRightOf="@+id/toLeft"
        android:spacing="40dip"
        android:scrollbars="horizontal"/>

    <ImageButton android:id="@+id/toRight"
        android:background="@drawable/arrow_right"
        android:layout_width="wrap_content"
        android:layout_height="40dip" 
        android:layout_toRightOf="@+id/gallery" />
</merge>

Later i create a test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lin_layout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

 <com.Test.GallerySlider
     android:id="@+id/choose"
     android:layout_span="2"
     android:layout_width="300dip"
     android:layout_height="wrap_content"    />

</LinearLayout>

My next Step was to include this custom Widget into my project and change the adapter from my Widget:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout);
    ViewStub st3 = new ViewStub(TestwidgetActivity.this);
    LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lineLayout.addView(st3,paramst3);
    st3.setLayoutResource(R.layout.test);
    View st3InflaView =st3.inflate();
    GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose);
    gSlider.setNewAdapter( new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"}));
}

This is the Widgetclass I wrote:

    public class GallerySlider extends RelativeLayout implements OnClickListener {
    private ArrayAdapter<String> adapter;
private Gallery gallery;
private ImageButton toLeftBtn = null;
private ImageButton toRightbtn = null;

public GallerySlider(Context context) {
     super(context, null);   
     init(context);
}

public GallerySlider(Context context, AttributeSet attrs) {
     super(context, attrs);
     init(context);
}

public GallerySlider(Context context, AttributeSet attrs, int defStyle) {
     super(context, attrs);
     init(context);
}

public void init(Context ctx){
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
    inflater.inflate(R.layout.customGallery, this, true);

    toLeftBtn = (ImageButton) findViewById(R.id.toLeft);
    toLeftBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(gallery.getSelectedItemPosition() > 0){
                gallery.setSelection(gallery.getSelectedItemPosition()-1);
            }
        }

    });

    toRightbtn = (ImageButton) findViewById(R.id.toRight);
    toRightbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){
                gallery.setSelection(gallery.getSelectedItemPosition()+1);
            }
        }

    });
    adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1",
                                                                                                    "1")});
    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setBackgroundResource(R.drawable.h_sliderl);
    gallery.setAdapter(adapter);
}



@Override
public void onClick(DialogInterface dialog, int which) {

    switch(which){
    case R.id.toLeft:   gallery.setSelection(gallery.getFocusedChild().getId()-1);
                        break;

    case R.id.toRight:  gallery.setSelection(gallery.getFocusedChild().getId()+1);
                        break;
    }

}


public void setNewAdapter(ArrayAdapter<String> _adapter){

        gallery.setAdapter(_adapter);
        ((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged ();
}

}

If I call setNewAdapter(ArrayAdapter _adapter) nothing change.. . I also tried to change the size of the gallery but it also fails(nothig happen). Is my approach false?
greetings marcel

Was it helpful?

Solution

The first thing I can detect is that you are creating your custom view two times.

The first creation occurs with the ViewStub when you set the layout. And the second one, which doesn't get added to the contentView when you inflate R.layout.test.

You are setting the adapter to the second custom view which it isn't added to the view hierarchy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top