简体   繁体   English

Android Pager适配器,onClick

[英]Android Pager Adapter, onClick

I've been coding an Android app and faced a little problem. 我一直在编写Android应用程序,但遇到了一些小问题。 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. 我尝试使用Pager Adapter扩展类(请参见下面的代码)并制作一些背景墙纸来展示该应用程序的工作原理,以实现本教程。 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. 我认为可能是通过使用onClick方法。

Thanks on advance and sorry for my English. 在此先感谢您,我的英语很抱歉。

tutorial.java 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 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 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. 辛苦了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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