Android ViewPager和ListViews

时间:2011-09-11 06:07:43

标签: android android-listview android-viewpager

我对Android开发和整体开发相对较新。我在掌握ViewPager的语法/过程时遇到了麻烦。

我想在ViewPager中实现几个ListView。我已经加载了兼容包和一切。但我还没有看到任何完整的例子来说明如何做到这一点。

通过查看示例,我学得最好。如果有人可以发布你已实现此类事情的任何项目的示例,请告诉我。

问题是,在尝试启动我的活动时,我在此行上遇到Null Pointer Exception:

listView1.setAdapter(new ArrayAdapter<Object>(this, R.layout.rowlayout, list1));

我怀疑我这样做是错的。如果我不使用ViewPager,我可以获得两个列表来显示其内容。所以我知道列表不是空的......

编辑:

感谢VenomM的答案如下!这是我最终使用的代码,稍微修改了VenomM的示例。

ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider
{
    private ListView listView1;
    private static String[] titles = new String[]
    {
       "Page 1",
       "Page 2",
       "Page 3",
    };
    private final Context context;

    public ViewPagerAdapter( Context context )
    {
        this.context = context;
    }

    @Override
    public String getTitle( int position )
    {
        return titles[ position ];
    }

    @Override
    public int getCount()
    {
        return titles.length;
    }

    @Override
    public Object instantiateItem(View collection, int position) {

        LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();

        listView1 = (ListView) layoutInflater.inflate(R.layout.listview1, null);

        String[] listData = null;
        MyArrayAdapter dataAdapter;

        if (position == 0) {
          listData = context.getResources().getStringArray(R.array.list1);
          dataAdapter = new MyArrayAdapter((Activity) context,
              R.layout.rowlayout, listData);
        } else if (position == 1) {
          listData = context.getResources().getStringArray(R.array.list2);
          dataAdapter = new MyArrayAdapter((Activity) context,
              R.layout.rowlayout, listData);
        } else {
          listData = context.getResources().getStringArray(R.array.list3);
          dataAdapter = new MyArrayAdapter((Activity) context,
              R.layout.rowlayout, listData);
        }

        listView1.setAdapter(dataAdapter);
        listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg3) {
              Toast.makeText(context,
                  adapter.getAdapter().getItem(position).toString(),
                  Toast.LENGTH_LONG).show();
            }
        });

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

        return listView1;
    }


    @Override
    public void destroyItem(View collection, int position, Object view) {
        System.out.println("on destroyItem()");
        ((ViewPager) collection).removeView((ListView) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        System.out.println("on isViewFromObject()");
        return view == ((ListView) object);
    }

    @Override
    public void finishUpdate( View view ) {}

    @Override
    public void restoreState( Parcelable p, ClassLoader c ) {}

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate( View view ) {}
}

ArrayAdapter:

public class MyArrayAdapter extends ArrayAdapter<String>{

    private Activity context = null;
    private String[] names = null;
    private int rowLayoutId;
    public MyArrayAdapter(Activity context, int textViewResourceId, String[] names) {
        super(context, textViewResourceId, names);
        this.context = context;
        this.names = names;
        this.rowLayoutId =textViewResourceId;
    }

    // static to save the reference to the outer class and to avoid access to
    // any members of the containing class
    static class ViewHolder {
        protected ImageView imageView;
        protected TextView textView;
    }

}

3 个答案:

答案 0 :(得分:5)

我仍然无法理解为什么要使用object参数更改Arrayadapter,如果您希望它包含字符串项。 尝试更改

new ArrayAdapter<Object>(this, R.layout.rowlayout, list1)

new ArrayAdapter<String>(this, R.layout.rowlayout, list1)

我使用了自定义ArrayAdapter,一切都运行正常。如果你成功,请告诉我。

答案 1 :(得分:3)

以下是一个例子:

MainActivity:

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"}));

    }
}

CustomViewPagerAdapter.java

public class CustomPagerAdapter extends PagerAdapter {

 private Context mContext;
 private Vector<View> pages;

 public CustomPagerAdapter(Context context, Vector<View> 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);
 }

}

有关更多信息,请查看我的博客。 Here

答案 2 :(得分:0)

您使用的是哪个ViewPager? V4兼容性jar?

如果是,请仔细检查事件。具体来说,onCreate,onCreateView&amp; onActivityCreated。