Question

There are over 30 views to be displayed in my activity. Each one is a RelativeLayout with nested ImageView and some TextViews within (some of them are static, some of them are variable). Refactored to programmatic way of displaying results it looks like:

tbnImages = db.getTbnImages();
tbnNames = db.getTbnNames();
tbnExts = db.getTbnExts();
tbnDescs = db.getTbnDescs();

vf = (ViewFlipper) findViewById(R.id.viewFlipperTbnImages);
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));

detector = new SimpleGestureFilter(this, this);

tbnImage = (ImageView) findViewById(R.id.tbnImage); 
tbnName = (TextView) findViewById(R.id.tbnName);        
tbnExt = (TextView) findViewById(R.id.tbnExt);      
tbnDesc = (TextView) findViewById(R.id.tbnDesc);        

How should I configure my ViewFlipper so that .showNext() and .showPrevious() methods would display proper views, including correct behaviour when launching method upon the last view (.showNext() redirects to the first view) and the first one (.showPrevious() presents the last view)?

public void onSwipe(int direction) {

switch (direction) {

    case SimpleGestureFilter.SWIPE_RIGHT:
        vf.showPrevious();
        break;

    case SimpleGestureFilter.SWIPE_LEFT:
        vf.showNext();
        break;
    }

All variables (ImageView picture and TextView texts) are taken from an ArrayList. How to set them as ViewFlipper children?

Was it helpful?

Solution

You need to generate all the 30 views and add them as children of the ViewFlipper. If all your views share one single layout, you should create a custom view to handle it.

public class CustomView extends RelativeLayout{

    public CustomView(Context context, Uri imageUri, String tbnName, String tbnDescripton, String tbnDesc) {
        super(context);

        LayoutInflater.from(context).inflate(R.layout.your_layout_resource, this, true);

        ImageView tbnImage = (ImageView) findViewById(R.id.tbnImage);
        TextView tbnName = (TextView) findViewById(R.id.tbnName);
        TextView tbnExt = (TextView) findViewById(R.id.tbnExt);
        TextView tbnDesc = (TextView) findViewById(R.id.tbnDesc);

        tbnImage.setImageURI(imageUri);
        tbnName.setText(tbnName);
        tbnImage.setText(tbnDescription);
        tbnExt.setText(tbnDesc);
    }
}

Where 'your_layout_resource' is your current RelativeLayout resource with a 'merge' element as a root. Then you just have to create all the views you need and add them to your ViewFlipper:

    for(int i = 0; i<tbnImages.size(); i++){
        vf.addView(new CustomView(context,
                tbnImages.get(i),
                tbnNames.get(i),
                tbnDescripton.get(i),
                tbnDescs.get(i)));
    }

Anyway, 30 views are a lot of views. Are you sure a ViewPager wouldn't work for you?

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