Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current view from ViewPager

I'm trying to get current page view from ViewPager with Viewpager.findViewWithTag. When try to get current view from ViewPager it returns Null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_main);
    Bundle bundle = getIntent().getExtras();
    chapter = bundle.getInt("chapter");


    ViewPagerAdapter adapter = new ViewPagerAdapter(this, chapter);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    View myView = pager.findViewWithTag(pager.getCurrentItem());

... and here's my ViewPageAdapter

 @Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater;
    if(position != 10){
            inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);
      itemView.setTag(position);

    } else {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         itemView = inflater.inflate(R.layout.results, container,
                false);
    }
    container.addView(itemView);
    return itemView ;
}

Here's viewpager_main.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.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

and here's viewpager_item.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:gravity="center_horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:scaleType="fitCenter"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="1"
        android:text="1" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="2"
        android:text="2" />

    <Button
        android:id="@+id/Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="3"
        android:text="3" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >

    <Button
        android:id="@+id/Button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="4"
        android:text="4" />

    <Button
        android:id="@+id/Button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="5"
        android:text="5" />
</LinearLayout>

I don't know where did i do a mistake but couldn't fix it. Thanks

like image 817
alynurly Avatar asked Dec 11 '22 17:12

alynurly


2 Answers

You can try something like this -

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater;
    if(position != 10){
            inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);
      itemView.setTag("View"+position);

    } else {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         itemView = inflater.inflate(R.layout.results, container,
                false);
    }
    container.addView(itemView);
    return itemView ;
}

And when you need the current view-

View myView = pager.findViewWithTag("View"+pager.getCurrentItem());
like image 197
Shadab Ansari Avatar answered Jan 15 '23 04:01

Shadab Ansari


The issue here is that you are calling pager.getCurrentItem() right after setting the PagerAdapter via

pager.setAdapter(adapter);

The page is not necessarily created immediately. That is PageAdapter.instantiateItem is called after your call to

View myView = pager.findViewWithTag(pager.getCurrentItem());

If you need the view right after it is created, just do what you need to do in the Adapter, otherwise your call should work if called in a listener such as OnPageChangeListener.onPageSelected.

like image 31
ptoinson Avatar answered Jan 15 '23 05:01

ptoinson