RecylerView中的ViewPager,单击不起作用

时间:2018-07-07 18:24:31

标签: android android-recyclerview android-viewpager click

我的RecyclerView的项目布局在顶部有ViewPager,在其下方有一些视图。 ViewPager工作正常,滑动良好。但是单击“回收器”项无法在单击ViewPager时起作用(我已将整个项目设置为单击)。但是,如果我单击ViewPager下面的内容,则单击可以正常工作。

这似乎是一个非常简单的问题,但是我已经被这个问题困扰了一段时间。感谢您的帮助

这是RecyclerView的列表项

<?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" xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:card_view="http://schemas.android.com/tools"
              android:orientation="horizontal"
              xmlns:attrs="http://schemas.android.com/apk/res-auto">


    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardBackgroundColor="@android:color/white"
            card_view:cardCornerRadius="8dp"
            card_view:cardElevation="6dp"
            card_view:cardUseCompatPadding="true"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="8dp">

        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="0dp" android:layout_height="0dp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintDimensionRatio="H,4:2"/>

            <com.rd.PageIndicatorView
                    android:id="@+id/pageIndicatorView"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    app:piv_animationType="scale"
                    app:piv_dynamicCount="true"
                    app:piv_interactiveAnimation="true"
                    app:piv_selectedColor="@color/whiteSmoke"
                    app:piv_unselectedColor="@color/white25Transparent"
                    app:piv_viewPager="@id/viewPager"
                    attrs:piv_padding="2dp"
                    attrs:piv_radius="4dp"
                    app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintBottom_toBottomOf="@id/viewpager" android:layout_marginBottom="0dp"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPRegularTextView
                    android:id="@+id/name"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@id/viewpager" android:layout_marginTop="6dp"
                    app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
                    android:layout_marginEnd="12dp" android:layout_marginRight="12dp"
                    android:text="Cosy Duplex - 42m2" android:textColor="@color/stPatricksBlue"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
                    android:id="@+id/tv1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    style="@style/RoomDimensTextView"
                    app:layout_constraintTop_toBottomOf="@id/name" android:layout_marginTop="6dp"
                    app:layout_constraintStart_toStartOf="@id/name"
                    android:text="3p"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
                    android:id="@+id/tv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    style="@style/RoomDimensTextView"
                    app:layout_constraintTop_toBottomOf="@id/name" android:layout_marginTop="6dp"
                    app:layout_constraintStart_toEndOf="@id/tv1"
                    android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
                    android:text="2ch"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPMediumTextView
                    android:id="@+id/tv3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    style="@style/RoomDimensTextView"
                    app:layout_constraintTop_toBottomOf="@id/name" android:layout_marginTop="6dp"
                    app:layout_constraintStart_toEndOf="@id/tv2"
                    android:layout_marginStart="12dp" android:layout_marginLeft="12dp"
                    android:text="4m"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPRegularTextView
                    android:id="@+id/location"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@id/tv1" android:layout_marginTop="6dp"
                    app:layout_constraintStart_toStartOf="@id/tv1" app:layout_constraintEnd_toEndOf="parent"
                    android:text="Villeurbanne" android:textColor="@color/darkSlateBlue"
                    app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="4dp"/>

            <com.codeairs.wattamatte.customcontrols.textview.MPBoldTextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"
                    android:layout_marginRight="12dp"
                    app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="12dp"
                    android:text="450€ C.C" android:textColor="@color/stPatricksBlue" android:textSize="17sp"/>

            <View
                    android:id="@+id/toggleBG"
                    android:layout_width="36dp" android:layout_height="36dp"
                    android:background="@drawable/solid_white_circle"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
                    app:layout_constraintTop_toBottomOf="@id/viewpager"
                    app:layout_constraintBottom_toBottomOf="@id/viewpager"/>

            <ToggleButton
                    android:id="@+id/favoritesToggleButton"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:background="@drawable/bg_fav_toggle"
                    android:textOn=""
                    android:textOff=""
                    app:layout_constraintTop_toTopOf="@id/toggleBG"
                    app:layout_constraintBottom_toBottomOf="@id/toggleBG"
                    app:layout_constraintStart_toStartOf="@id/toggleBG"
                    app:layout_constraintEnd_toEndOf="@id/toggleBG"/>

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

RecylerView适配器

public class RoomsListRecyclerViewAdapter extends RecyclerView.Adapter<RoomsListRecyclerViewAdapter.ViewHolder> {

    private final List<Room> mValues;
    private final List<String> favRoomsIds;
    private final OnListFragmentInteractionListener mListener;
    private final Context mContext;

    public RoomsListRecyclerViewAdapter(List<Room> items, List<String> favRoomsIds,
                                        OnListFragmentInteractionListener listener, Context mContext) {
        mValues = items;
        mListener = listener;
        this.favRoomsIds = favRoomsIds;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_rooms_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);

        holder.imgPager.setAdapter(new ImageAdapter(mContext, holder.mItem.property.pictures));
        holder.pageIndicatorView.setCount(holder.mItem.property.pictures.size());
        holder.pageIndicatorView.setSelection(0);
        holder.imgPager.addOnPageChangeListener(getPageChangeListener(holder));

        holder.name.setText(holder.mItem.property.name);
        holder.location.setText(holder.mItem.property.address.city);
        holder.price.setText(MessageFormat.format("{0} € C.C", holder.mItem.property.price));

        holder.tv1.setText(MessageFormat.format("{0}p", holder.mItem.property.personCount));
        holder.tv2.setText(MessageFormat.format("{0}ch", holder.mItem.property.pieces));
        holder.tv3.setText(MessageFormat.format("{0}m²", holder.mItem.property.surface));

        holder.favToggle.setChecked(favRoomsIds.contains(holder.mItem.property.id));
        holder.favToggle.setOnCheckedChangeListener((buttonView, isChecked) ->
                toggleFavorite(holder.mItem.property.id, isChecked));

        holder.mView.setOnClickListener(v -> {
            if (null != mListener) {
                mListener.onListFragmentInteraction(holder.mItem, favRoomsIds.contains(holder.mItem.property.id));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        final View mView;
        Room mItem;
        ViewPager imgPager;
        TextView name, location, price, tv1, tv2, tv3;
        ToggleButton favToggle;
        PageIndicatorView pageIndicatorView;

        ViewHolder(View view) {
            super(view);
            mView = view;
            imgPager = view.findViewById(R.id.viewpager);
            name = view.findViewById(R.id.name);
            location = view.findViewById(R.id.location);
            price = view.findViewById(R.id.price);
            tv1 = view.findViewById(R.id.tv1);
            tv2 = view.findViewById(R.id.tv2);
            tv3 = view.findViewById(R.id.tv3);
            favToggle = view.findViewById(R.id.favoritesToggleButton);
            pageIndicatorView = view.findViewById(R.id.pageIndicatorView);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + "'";
        }
    }

    private void toggleFavorite(String propertyId, boolean fav) {
        if (fav)
            favRoomsIds.add(propertyId);
        else
            favRoomsIds.remove(propertyId);

        JsonObjectRequest request = NetworkUtils.toggleRoomFavoritesRequest(
                mContext, propertyId, fav, this::handleResponse, this::handleError);

        VolleySingleton.getInstance(mContext).addToRequestQueue(request);
    }

    private void handleResponse(JSONObject response) {
        Log.d("RoomFav.Response", response.toString());
    }

    private void handleError(VolleyError error) {

    }

    public void clear() {
        mValues.clear();
        notifyDataSetChanged();
    }

    public void addItems(List<Room> rooms) {
        mValues.addAll(rooms);
        notifyDataSetChanged();
    }

    private ViewPager.OnPageChangeListener getPageChangeListener(ViewHolder holder) {
        return new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                holder.pageIndicatorView.setSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        };
    }
}

寻呼机适配器

public class ImageAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> mItems;

    public ImageAdapter(Context context, List<String> items) {
        mContext = context;
        mItems = items;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup collection, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ImageView img = (ImageView) inflater.inflate(R.layout.layout_image, collection, false);

        String url = mItems.get(position);
        if (!TextUtils.isEmpty(url))
            Picasso.get().load(ApiPaths.IMG_PATH_PREFIX + url).into(img);

        collection.addView(img);
        return img;
    }

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

    @Override
    public int getCount() {
        return mItems.size();
    }

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

}

寻呼机项

<ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/img"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:clickable="false"/>

0 个答案:

没有答案