如何在ViewPager布局的其中一个页面中填充Listview

时间:2012-06-04 07:28:50

标签: android listview android-viewpager

我有一个关于ViewPager的基本问题。我有一个包含5个页面的ViewPagerActivity,除了在ViewPagerActivity.java的setContentView(R.layout.main)中引用的main.xml(参见下面的代码)之外,每个页面都有自己的布局xml。

“中间”页面(middle.xml是5个布局xml之一)包含一个Listview,它使用数据库中的数据进行动态填充。

这是我的问题。我应该在哪里放置以下代码来组合Listview并设置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);
}
   });   

我认为它不会出现在ViewPagerActivity.java中,除非它可以通过setContentView()引用main.xml和middle.xml。我还想过添加一个引用middle.xml的java类,但我不知道如何在中间页膨胀时激活它(?)。 (请参阅下面的MyPageAdapter,了解每个页面的充气方式。)

感谢阅读。请随时要求进一步澄清。

这是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>

这是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>

这是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

            }

    }

1 个答案:

答案 0 :(得分:2)

您应该在instantiateItem:

中的switch case中添加它
public Object instantiateItem(View collection, int position) {


                View view=null;

                LayoutInflater inflater = (LayoutInflater) collection.getContext()
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                int resId = 0;
                switch (position) {
                case 0:
                        resId = R.layout.farleft;
                        view = inflater.inflate(resId, null);
                        break;
                case 1:
                        resId = R.layout.left;
                        view = inflater.inflate(resId, null);
                        break;
                case 2:
                        resId = R.layout.middle;
                        view = inflater.inflate(resId, null);
                        ListView memoListView = (ListView)view.findViewById(R.id.list_in_middle_layout);
                        MemoListAdapter memoListAdapter = new MemoListAdapter(this);
                        memoListView.setAdapter(memoListAdapter);

                        break;
                case 3:
                        resId = R.layout.right;
                        view = inflater.inflate(resId, null);
                        break;
                case 4:
                        resId = R.layout.farright;
                        view = inflater.inflate(resId, null);
                        break;
                }



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

                return view;
        }