4

在将数据设置为列表视图中的列表项时,我需要您的帮助。在我的列表视图中,每个项目都有一个 ViewPager,每个 viewpager 应该有不同的数据。我不知道如何为每个 viewpager 设置不同的数据。实际上基于 listitem 索引我需要设置数据查看器。请仔细阅读我粘贴在下面的代码片段并尝试帮助我。

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

    }
}
}

但是在这里,所有列表项的 Viewpagers 都设置了相同的数据。理想情况下,我的要求是设置不同的数据。如果您有/知道任何解决方案,请告诉我。

4

2 回答 2

0

将 Viewpager 用作 ListItem 是一个坏主意,在您的情况下,要使其正常工作,请进行以下更改。

 MyPagerAdapter adapter = new MyPagerAdapter(position);

在 PagerAdapter 中:

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

在开关中:

  switch (mListPosition) {
......
}
于 2014-06-15T14:06:29.487 回答
0

尝试这个

1) 使用其中的 ViewPager 设计您的项目布局。2)注意您的适配器代码以根据项目位置填充 viewpager 3)在 Viewholder 中保留 Viewpager 的引用

我认为应该这样做。此外,从 UI 的角度来看,将 ViewPager 放在每个列表视图项中是个坏主意。您可以尝试在 listItem 中单击某些按钮时过度打开。

于 2014-01-11T13:44:19.983 回答