Viewpager and listviews

被刻印的时光 ゝ 提交于 2019-12-19 04:55:27

问题


I'm trying to use viewpager in my app.

I want to create 4 view pages with listviews and every listview only data will change.When app started every page will load own data but only first one will be shown at the first time.

But i can't do this.My fragment refresh own data every page changes and all the listviews always have same data.Please help me about this.I have asked this at stackoverflow but no one answer it.If there is a source about that can you send its link?


回答1:


Take a look at this project https://github.com/Shereef/ViewPagerPlusExpandableList (import it in eclipse indigo, or take a look at the code online), this was my answer to my question: How to implement an ExpandableList in a ViewPager in Android?

Should be very helpful to your question if it doesn't fully solve it.




回答2:


Here is one implementation of a ViewPager with different ListView's as pages.

MainActivity.java

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

 private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.main);
        ListView listview1 = new ListView(mContext);
        ListView listview2 = new ListView(mContext);
        ListView listview3 = new ListView(mContext);

        Vector<View> pages = new Vector<View>();

        pages.add(listview1);
        pages.add(listview2);
        pages.add(listview3);

        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
        CustomPagerAdapter adapter = new CustomPagerAdapter(mContext,pages);
        vp.setAdapter(adapter);

        listview1.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));
        listview2.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A2","B2","C2","D2"}));
        listview3.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A3","B3","C3","D3"}));

    }
}

CustomPagerAdapter.java

public class CustomPagerAdapter extends PagerAdapter {

 private Context mContext;
 private Vector<View> pages;

 public CustomPagerAdapter(Context context, Vector&lt;View&gt; pages) {
  this.mContext=context;
  this.pages=pages;
 }

 @Override
 public Object instantiateItem(ViewGroup container, int position) {
  View page = pages.get(position);
  container.addView(page);
  return page;
 }

 @Override
 public int getCount() {
  return pages.size();
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
  return view.equals(object);
 }

 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((View) object);
 }

}

More info here.



来源:https://stackoverflow.com/questions/8289425/viewpager-and-listviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!