简体   繁体   中英

Android Pager Adapter, onClick

I've been coding an Android app and faced a little problem. I've tried to implement a tutorial using a Pager Adapter extended Class ( see below code ) and making some background wallpapers that show how the app works. Right now you can go from page to page swiping your finger through the screen. I would like to change the screen by touching it. Is that possible? if so, how can it be done? i supose it might be by using an onClick method.

Thanks on advance and sorry for my English.

tutorial.java

    public class tutorial extends Activity {

    int currentPage;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
         //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

       //set content view AFTER ABOVE sequence (to avoid crash)
        this.setContentView(R.layout.tutorial_pannels_pager);

        MyPagerAdapter adapter = new MyPagerAdapter();

        final ViewPager myPager = (ViewPager) findViewById(R.id.tutorial_pannel);

        myPager.setAdapter(adapter);

        myPager.setCurrentItem(0);









    }


}

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {


    // set number of pages
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 7;
    }


    // Set each screen's content
    @Override
    public Object instantiateItem(final View container, final int position) {
        Context context = container.getContext();
        LinearLayout layout = new LinearLayout(context);
        // Add elements
        TextView textItem = new TextView(context);



        switch (position) {
        case 0:
           layout.setBackgroundResource(R.drawable.tut_0);

           break;
        case 1:
           layout.setBackgroundResource(R.drawable.tut_1);

           break;
        case 2:
            layout.setBackgroundResource(R.drawable.tut_2);

            break;

        case 3:
            layout.setBackgroundResource(R.drawable.tut_3);

            break;
        case 4:
            layout.setBackgroundResource(R.drawable.tut_4);

            break;
        case 5:
            layout.setBackgroundResource(R.drawable.tut_5);

            break;
        case 6:
            layout.setBackgroundResource(R.drawable.tut_6);
            break;
    }
    layout.addView(textItem);
    ((ViewPager) container).addView(layout, 0); // This is the line I added
    return layout;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

}

tutorial_pannels_pager.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:gravity="bottom"
    android:orientation="vertical" >

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

</LinearLayout>

First you have to pass a interface to the adapter. This example should enlighten you:

  public interface OnItemClick{
    void onClick(int position);
  }

Next you should implement this interface and add the code to manually change the view. Next pass an instance of this interface to the adapter. Like this:

   MyPagerAdapter adapter = new MyPagerAdapter(new OnItemClick(){
        @Override
        public void onClick(int position)
        {
            myPager.setCurrentItem(position+1); //example.. change it to your needs
        }
    });

Next on your adapter you need save an instance to this interface that was passed in the constructor and then call it when you click on the layout, for example.

   layout.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            interface.onClick(position);
        }
    });

This is just the basics as you need to work on it by yourself. However it's more than necessary to reach the final solution.

Good work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM