1

我有一个关于 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

            }

    }
4

1 回答 1

2

您应该将其添加到您的 switch case in instantiateItem 中:

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;
        }
于 2012-06-04T07:46:20.487 回答