我對Android開發和開發總體上比較陌生。我無法理解ViewPager的語法/過程。Android ViewPager和ListViews
我有幾個ListViews,我想實現到一個ViewPager。我已經加載了兼容包以及一切。但我還沒有看到如何做到這一點的完整例子。
我通過查看示例學習得最好。如果有人可以發佈任何您已經實施過這類項目的項目的示例,請告訴我。
的問題是,我得到一個空指針異常此線路上嘗試推出我的活動時:
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;
}
}
https://androidbeasts.wordpress.com/2015/08/11/tabs-with-swipe-views/#more-79 – Aakash