如何在 ViewPager 中设置 OnClickListener

How to set OnClickListener in ViewPager(如何在 ViewPager 中设置 OnClickListener)
本文介绍了如何在 ViewPager 中设置 OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发的新手,我正在学习编码 &设计一个安卓项目.我在使用 ViewPager 时遇到问题,无法在本网站或通过 Google 搜索找到答案.

I'm new to Android development and I'm studying to code & design an Android project. I have problem with ViewPager and can't find answer on this site or via a Google search.

问题:我无法让 ButtonViewPager 中执行其操作.

Problem: I can't make a Button execute its action while it's in ViewPager.

你可以看到我的项目 &apk 文件在这里:https://dl.dropbox.com/u/9820517/ForOneTimeDownload/TestPagerView.rar

You can see my project & apk file here: https://dl.dropbox.com/u/9820517/ForOneTimeDownload/TestPagerView.rar

这是我的 PagerDemo.java:

 public class PagerDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pager_demo);

        myPagerAdapter adapter = new myPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(2);

        SetAllBtnFunc();
    }

    public void SetAllBtnFunc() {
        //all func
        View.OnTouchListener touch = new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Log.d("hlv_trinh", "Button be Touched!");
                    return false;
                }
            };

            View.OnClickListener click = new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.d("hlv_trinh", "Button be Clicked!");
                }
            };

        //Get view
        View v = getLayoutInflater().inflate(R.layout.middle, null);

        Button b = (Button) v.findViewById(R.id.myBtn);
        b.setOnClickListener(click);
        b.setOnTouchListener(touch);
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_pager_demo, menu);
        return true;
    }
    private class myPagerAdapter extends PagerAdapter {

        public int getCount() {
            return 5;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.farleft;
                break;
            case 1:
                resId = R.layout.left;
                break;
            case 2:
                resId = R.layout.middle;
                break;
            case 3:
                resId = R.layout.right;
                break;
            case 4:
                resId = R.layout.farright;
                break;
            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @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;
        }
    }
    }

我的布局内容在这里:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Middle"
        android:textSize="30dp" >
    </TextView>

    <Button
        android:id="@+id/myBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Click" />
</LinearLayout>

推荐答案

这是一个简单的解决方案.
在您的布局 XML 中,在按钮标记中,设置 android:onClick="AnyName",然后在您的 PagerDemo.java 上,放置:

Here is an easy solution.
In your layout XML, in the button tag, set android:onClick="AnyName", then on your PagerDemo.java, place:

public void AnyName(View v) {
    // Do your stuff
}

ViewPager 中设置 onClickListener.

这篇关于如何在 ViewPager 中设置 OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)
iOS: Send multiple actions to a bar button(IOS:向一个栏按钮发送多个动作)