0

I am currently trying to implement a Gallery-like view. Since it has been deprecated i have been trying to find alternatives. I have successfully experimented with Universal Image Loader's image view pager with Dave Smith's implementation of view pager in the following link. http://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html The result is a gallery-like widget.

Edit My current problem is that the onclicklistener seems to activate only at the focused page. I have tried to set the onclicklistener to the viewPager instead but the the problem still persists. Any help and advice is appreciated.

Here is a snippet of my code:

(Updated)My Activity:

public class PagerActivity extends Activity {

PagerContainer mContainer;

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

    mContainer = (PagerContainer) findViewById(R.id.pager_container);

    ViewPager pager = mContainer.getViewPager();
    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);
    //Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(adapter.getCount());
    //A little space between pages
    pager.setPageMargin(15);

    //If hardware acceleration is enabled, you should also remove
    // clipping on the pager for its children.
    pager.setClipChildren(false);
}

//Nothing special about this adapter, just throwing up colored views for demo
private class MyPagerAdapter extends PagerAdapter {

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View page=getLayoutInflater().inflate(R.layout.page, container, false);
        TextView tv = (TextView)page.findViewById(R.id.pagetext);
        tv.setText("Item "+position);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));


        final int cPos = position;
        tv.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(PagerActivity.this,"Page at: "+String.valueOf(cPos) , Toast.LENGTH_LONG)
                   .show();
            }

        });

        container.addView(page);
        return page;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }
}
}

Layout File

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

<com.example.pagercontainer.PagerContainer
    android:id="@+id/pager_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CCC">
    <android.support.v4.view.ViewPager
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />
</com.example.pagercontainer.PagerContainer>

</RelativeLayout>
  • I cannot reproduce your problem. I have updated my version of this sample to register click handlers on each `TextView`, and they all fire as expected: https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/MultiView1 – CommonsWare Jun 30 '13 at 13:12
  • The example you linked does not make use of a container to wrap around the viewpagers. I am trying out Dave Smith's code which makes use of this container. I think the problem is that only the focused page is able to fire off. – user2533611 Jul 01 '13 at 10:54

1 Answers1

0

The thing that works for me was this line:

final int cPos = position;

I have been trying to figure out how to combine setOffscreenPageLimit() with get focused item position. So, making this variable final and then pass it to the onclicklistener it will display the correct item.

Titogelo
  • 73
  • 7