4

我是 Android 开发的新手,我正在学习编码和设计一个 Android 项目。我有问题,ViewPager无法在本网站或谷歌搜索中找到答案。

问题Button当它在ViewPager.

您可以在此处查看我的项目和 apk 文件: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>
4

5 回答 5

7

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

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

这设置onClickListener在一个ViewPager.

于 2014-05-08T09:15:48.647 回答
2

尽管在 2021 年给出这个答案为时已晚,但我想分享这个,因为它似乎是最简单和最简单的一个:

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private String[] imageUrls;

    public ViewPagerAdapter(Context context, String[] imageUrls) {
        this.context = context;
        this.imageUrls = imageUrls;
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(context)
                .load(imageUrls[position])
                .thumbnail(Glide.with(context).load(R.drawable.loading))
                .dontAnimate()
                .centerCrop()
                .into(imageView);

        container.addView(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(position == 0) {
                    Toast.makeText(context, "One", Toast.LENGTH_SHORT).show();
                } else if(position == 1) {
                    Toast.makeText(context, "Two", Toast.LENGTH_SHORT).show();
                } else if(position == 2) {
                    Toast.makeText(context, "Three", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return imageView;
    }

在这里,我们只需ImageView要从实例化Adapter项目中访问 的位置。

于 2021-03-03T15:02:49.120 回答
1

我为视图寻呼机找到了一些棘手的解决方案。由于视图寻呼机没有任何点击事件,您可以尝试在视图寻呼机的子节点上单击事件。我的意思是在每个页面的根元素上设置 on Click 侦听器。那将肯定工作。

于 2013-02-19T21:04:10.433 回答
1

我做了这个问题如下

您必须首先实现ClickableViewPager

package ai.units;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

public class ClickableViewPager extends ViewPager {

  private OnItemClickListener mOnItemClickListener;

  public ClickableViewPager(Context context) {
    super(context);
    setup();
  }

  public ClickableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
  }

  private void setup() {
    final GestureDetector tapGestureDetector = new    GestureDetector(getContext(), new TapGestureListener());

    setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        tapGestureDetector.onTouchEvent(event);
        return false;
      }
    });
  }

  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
  }

  public interface OnItemClickListener {
    void onItemClick(int position);
  }

  private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
      if(mOnItemClickListener != null) {
        mOnItemClickListener.onItemClick(getCurrentItem());
      }
      return true;
    }
  }
}

然后像这样在xml中

  <ai.units.ClickableViewPager
   android:id="@+id/viewPager"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

您现在可以使用setOnItemClickListener方法

viewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
     @Override
     public void onItemClick(int position) {
       Log.i(TAG, "onItemClick: position = "+position);
     }
   });

笔记

ai.单位。我的包裹的名字是,你必须用你自己的包裹来移动它

从那里得到的答案的主要链接

于 2021-10-13T13:11:11.763 回答
0
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int Position) {
            position=Position;
        }
 });

您将使用任何视图进行单击,viewpager并使用 viewpager 的选定位置转到详细信息

于 2019-07-03T09:57:54.697 回答