Android在ViewPager中页面的特定项目上的点击监听器上

时间:2018-08-10 13:23:20

标签: android android-viewpager

我已经了解了如何从视图分页器here获取页面的位置,但是我想知道有什么方法可以对视图分页器的页面中的特定项目进行点击操作。

例如:我有一个视图寻呼机,其中包含3个页面,分别是Page1,Page2和Page3。在Page1中有两个项目,一个图像1个文本视图,我要单击操作图像。

这是我尝试过的:

适配器:

public class CustomPagerAdapter extends PagerAdapter {

    private Context mContext;

    public CustomPagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {

        BottomNavigationModel modelObject = BottomNavigationModel.values()[position];
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
        collection.addView(layout);

        return layout;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        BottomNavigationModel customPagerEnum = BottomNavigationModel.values()[position];
        return mContext.getString(customPagerEnum.getTitleResId());
    }

}

活动:

viewPager.setAdapter(new CustomPagerAdapter(this));

viewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            // Not calling this one TODO here
            }
        });

型号:

public enum BottomNavigationModel {

    ONE(R.string.page_one, R.layout.view_row_one),
    TWO(R.string.page_two, R.layout.view_row_two),
    THREE(R.string.page_three, R.layout.view_row_three);

    private int mTitleResId;
    private int mLayoutResId;

    BottomNavigationModel(int titleResId, int layoutResId) {
        mTitleResId = titleResId;
        mLayoutResId = layoutResId;
    }

    public int getTitleResId() {
        return mTitleResId;
    }

    public int getLayoutResId() {
        return mLayoutResId;
    }

}

布局view_row_one

<?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="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/dashboard_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_dashboard_blue" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"
            android:text="Dashboard"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/messenger_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_messenger_white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"
            android:text="Messenger"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/merlin_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_custom_white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"
            android:text="Custom"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/score_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_score_white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"
            android:text="Score"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/tasks_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_tasks_white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"
            android:text="Tasks"
            android:textSize="12sp" />
    </LinearLayout>

</LinearLayout>

我要在“活动”中单击任何图像视图。

2 个答案:

答案 0 :(得分:0)

您应该像这样重写FragmentPagerAdapter或FRagmentStatePagerAdapter类的getItem()方法:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new DescriptionFragment();
        case 1:
            return new ReviewFragment();
        case 3:
            return new OrderFragment();
        default:
            return null;
    }
}

如您所见,根据您单击的选项卡位置,给定的片段会膨胀。 因此,您应该像在任何活动中一样,在这些单独的片段中实施所需的按钮单击操作。

答案 1 :(得分:0)

我认为这可以为您工作:

在您的InstantiateItem()方法中,您可以获取图像并在其上设置点击侦听器,如下所示:

@Override
public Object instantiateItem(ViewGroup collection, int position) {

    BottomNavigationModel modelObject = BottomNavigationModel.values()[position];
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
    ImageView dashboardImage = (ImageView) layout.findViewById(R.id.dashboard_iv);
    dashboardImage.setOnClickListener(myClickListener);
    //you cann add all wanted click on images here. 
    collection.addView(layout);

    return layout;
}

但是我可以认为,这种设计在这里并不是完美的。