简体   繁体   中英

listview having viewpager in each list item in android

I'm in need of your help while setting the data to listitems in a listview. In my listview each item is having a ViewPager and each viewpager should have different data. I'm not getting how to set different data to each viewpager. Actually based on the listitem index I need to set the data viewpager. Please go through my code snippet pasted below and try to help me out.

public class MyListAdapter extends BaseAdapter {

private Activity activity;
private String [] titleData;
private int [] imageData;
private static LayoutInflater inflater=null;

private int iListPosition;
private GraphicalView graphView ;
ListView dashListView;
private Context context;
public MyListAdapter(View listView, Activity a, String[] titles, int[] images) {
    activity = a;

    context = a.getApplicationContext();
    dashListView = (ListView)listView;
}

public int getCount() {
    return titleData.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    iListPosition = position;
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {


    LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.dash_board_list_row, parent, false);



MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) rowView.findViewById(R.id.threepageviewer);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    return rowView;
} 

private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
            return 3;
    }

    public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;
            switch (position) {
            case 0:
                    resId = R.layout.weight_middle;


                    break;
            case 1:
                    resId = R.layout.weight_left;
                    break;
            case 2:
                    resId = R.layout.weight_right;
                    break;
            }

            View view = inflater.inflate(resId, null);
            if(resId == R.layout.weight_left  && iListPosition == 0){
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.chartView);
            layout.addView(graphView);
           }

            ((ViewPager) collection).addView(view, 0);

            return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
    }

    @Override
    public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

    }
}
}

But here the same data is geting set to the Viewpagers of all the list items. Ideally my requirement is to set different data. Please let me know if you have/know any solution for this.

Try this

1) Design your item layout with ViewPager in it. 2) Takecare in you adapter code to populate viewpager based on the item position 3) Keep reference of Viewpager in Viewholder

I think this should do. Also, It is bad idea from UI perspective to put ViewPager inside each listview item. You can rather try to open overly on some button click in listItem.

Using Viewpager as ListItem is a bad idea, in your case to make it work make the following change.

 MyPagerAdapter adapter = new MyPagerAdapter(position);

in PagerAdapter:

private class MyPagerAdapter extends PagerAdapter {
    int mListPosition;
MyPagerAdapter(int listPosition) {
   mListPosition = listPosition;
}
public int getCount() {
        return 3;
}

and in switch:

  switch (mListPosition) {
......
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM