alunicus
alunicus

Reputation: 23

Add element to ViewPager dynamically

I have started to work with ViewPager and can't understand how to add new element, like TextView, by pressing the button on main layout. I have tried different ways but none of them were successful.

Any ideas will be good for me...

Here is my code:

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

        }

    }

}

main.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>

middle.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>

Upvotes: 2

Views: 6727

Answers (1)

Georgy Gobozov
Georgy Gobozov

Reputation: 13731

I don't see any method to obtain current view on ViewPager it is strange.. However you can implement your adapter other way and store views in any collection or map. I don't know either it will work or not, write code from my head.

for example

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);
                }
            }
        });

Upvotes: 4

Related Questions