2

我已经开始使用 ViewPager 并且无法理解如何通过按下主布局上的按钮来添加新元素,如 TextView。我尝试了不同的方法,但没有一个成功。

任何想法都会对我有好处...

这是我的代码:

public class SimpleViewPagerActivity extends Activity {

    private ViewPager myPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyPagerAdapter adapter = new MyPagerAdapter();
        myPager = (ViewPager) findViewById(R.id.mypager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);

        Button btn_add = (Button) findViewById(R.id.button1);

        btn_add.setOnClickListener(new OnClickListener() {      

            @Override
            public void onClick(View v) {

            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {


        @Override
        public View 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.left;
                break;
            case 1:
                resId = R.layout.middle;
                break;
            case 2:
                resId = R.layout.right;
                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

        }

    }

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lt_lin"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <android.support.v4.view.ViewPager
        android:id="@+id/mypager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

中间.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />

</LinearLayout>
4

1 回答 1

4

我没有看到任何获取 ViewPager 上当前视图的方法,这很奇怪。但是您可以通过其他方式实现适配器并将视图存储在任何集合或地图中。我不知道它是否会工作,从我的脑海中编写代码。

例如

private class MyPagerAdapter extends PagerAdapter {

 private Map<Integer, View> views = new HashMap<Integer, View>();
 ...

    @Override
    public View 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.left;
                break;
            case 1:
                resId = R.layout.middle;
                break;
            case 2:
                resId = R.layout.right;
                break;
            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);
            views.put(position, view);
            return view;


    }   

    public View getView(int pos){
        return views.get(pos);
    }   

}

btn_add.setOnClickListener(new OnClickListener() {      

            @Override
            public void onClick(View v) {
                View view = adapter.getView(pager.getCurrentItem());
                if (view instance ViewGroup){
                    view.add(your_new_view);
                }
            }
        });
于 2012-08-07T22:41:27.883 回答