Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - ViewPager OnItemClickListener

I'm new to Viewpager, and after reading http://developer.android.com/reference/android/support/v4/view/ViewPager.html from google, I can't seem to find anything related on viewPager.setOnItemClickListener(new OnItemClickListener() { Do we have other options to act as Item Clicked?

Testing out

viewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent i = new Intent(MainActivity.this, SingleItemView.class);
                i.putExtra("flag", flag);
                i.putExtra("position", position);
                startActivity(i);
            }

        });

Error : The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ViewPager

question from:https://stackoverflow.com/questions/16350987/viewpager-onitemclicklistener

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no OnItemClick callback method for ViewPager. If you want click events on each page then you'll have to build your listener into the page content within your Adapter.

something like this:

@Override
public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()
    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View page = inflater.inflate(R.layout.YOUR_PAGE, null);

    page.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + pos);
        }
    });


    ((ViewPager) collection).addView(page, 0);
    return page;
}

exactly what you need will depend a bit on what else you are doing inside of instantiateItem() which you haven't posted so I can't give you a more specific answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...