Android寻呼机适配器,onClick

时间:2014-09-22 14:58:35

标签: java android

我一直在编写Android应用程序并面临一些问题。我尝试使用Pager Adapter扩展类(参见下面的代码)并制作一些显示应用程序工作方式的背景壁纸来实现教程。现在,您可以从一个页面到另一个页面在屏幕上滑动手指。我想通过触摸来更改屏幕。那可能吗?如果是这样,怎么办呢?我可能是通过使用onClick方法。

先谢谢你,对不起我的英语。

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>

1 个答案:

答案 0 :(得分:1)

首先,您必须将接口传递给适配器。这个例子应该启发你:

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

接下来,您应该实现此接口并添加代码以手动更改视图。接下来将此接口的实例传递给适配器。像这样:

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

接下来,在您的适配器上,您需要将一个实例保存到构造函数中传递的此接口,然后在单击布局时调用它。

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

这只是您需要自己处理的基础知识。但是,达到最终解决方案还不止于此。

干得好。