I have a very basic question about ViewPager. I have a ViewPagerActivity that includes 5 pages, where each page has its own layout xml, in addition to the main.xml (see code below) that is referenced in the ViewPagerActivity.java’s setContentView(R.layout.main).
The “middle” page (middle.xml which is one of the 5 layout xml’s) includes a Listview that is dynamically populated with data from a database.
Here is my question. Where should I put the following code that pupulates the Listview and set ClickListener?
mMemoListView = (ListView)findViewById(R.id.list_in_middle_layout);
mMemoListAdapter = new MemoListAdapter(this);
mMemoListView.setAdapter(mMemoListAdapter);
mMemoListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
viewMemo(position);
}
});
I don’t think it will be in ViewPagerActivity.java, unless it can reference both main.xml and middle.xml through setContentView(). I also thought about adding a java class that will reference middle.xml, but I don’t know how it can be activated(?) when the middle page is inflated. (See MyPageAdapter below to see how each page is inflated.)
Thanks for reading. Please feel free to ask for further clarification.
Here is main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myfivepanelpager"/>
</LinearLayout>
Here is middle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
>
<RelativeLayout
android:id="@+id/titleLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffffff"
>
<com.ViewPager.TitleBackgroundButton
android:id="@+id/titleBackgroundBtn"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:text="Memo"
android:textSize="18dp"
android:textStyle="bold"
/>
</RelativeLayout>
<ListView
android:id="@+id/list_in_middle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:layout_below="@+id/titleLayout"
android:layout_above="@+id/buttonLayout"
android:divider="#00000000"
android:listSelector="#00000000"
android:cacheColorHint="#00000000"
/>
<LinearLayout
android:id="@+id/buttonLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
>
<LinearLayout
android:id="@+id/buttonLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<com.ViewPager.TitleBitmapButton
android:id="@+id/newMemoBtn"
android:layout_width="100dp"
android:layout_height="48dp"
android:text="New"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#ff420000"
/>
<com.ViewPager.TitleBitmapButton
android:id="@+id/closeBtn"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginLeft="20dp"
android:text="Close"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#ff420000"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is ViewPagerActivity.java
public class ViewPagerActivity extends Activity {
/** Called when the activity is first created. */
public static final String TAG = "ViewPagerActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
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.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}
View view = inflater.inflate(resId, null);
((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
}
}
You should add it in your switch case in instantiateItem: