1

我有一个RecyclerView内幕BottomSheetDialogFragment。当它缓慢滚动时,这些RecyclerView项目正常工作。

但是,当RecyclerView快速滚动并在列表停止后(不触摸)时,触摸任何项目都不适用于快速触摸。它需要双重触摸。

请参阅下面的示例 gif,当触摸它时,Andhra Pradesh它工作正常。缓慢滚动后,触摸Haryana也可以正常工作。然后快速滚动并触摸在Punjab第一次触摸时不起作用。再次触摸它就可以了。

房车 GIF

以下是代码: OperatorListDialogFragment.java

package com.*;

import *;

public class OperatorListDialogFragment extends BottomSheetDialogFragment{

    private static final String ARG_NAME = "item_name";
    private static final String ARG_LOGO = "item_logo";
    private Listener mListener;
    private String header;
    private Context mContext;

    public static OperatorListDialogFragment newInstance(String[] name, int[] logo, String header) {
        final OperatorListDialogFragment fragment = new OperatorListDialogFragment();
        final Bundle args = new Bundle();
        args.putStringArray(ARG_NAME, name);
        args.putIntArray(ARG_LOGO, logo);
        args.putString("header", header);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        TextView headerTV = view.findViewById(R.id.title);
        headerTV.setText(getArguments().getString("header"));

        final RecyclerView recyclerView = view.findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new OperatorAdapter(getArguments().getStringArray(ARG_NAME), getArguments().getIntArray(ARG_LOGO)));

        view.findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mContext = context;

        final Fragment parent = getParentFragment();
        if (parent != null) {
            mListener = (Listener) parent;
        } else {
            mListener = (Listener) context;
        }
    }

    @Override
    public void onDetach() {
        mListener = null;
        super.onDetach();
    }

    public interface Listener {
        void onFilterSelected(String selected, String selectedQuery);
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        final TextView text;
        ImageView logo;

        ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            // TODO: Customize the item layout
            super(inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog_item, parent, false));
            text = itemView.findViewById(R.id.tv_operator_name);
            logo = itemView.findViewById(R.id.iv_recharge_provider_icon);
        }
    }

    private class OperatorAdapter extends RecyclerView.Adapter<ViewHolder> {

        private String[] mNames;
        private int[] mLogos;

        OperatorAdapter(String[] name, int[] logo) {
            mNames = name;
            mLogos = logo;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {

            holder.text.setText(mNames[position]);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("clicked", "" + position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mNames.length;
        }
    }
}

对话框.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

    <ImageView
        android:focusable="true"
        android:clickable="true"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:contentDescription="Close"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/dismiss"
        android:padding="14dp"
        android:src="@drawable/ic_close_black_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/title"
        app:layout_constraintTop_toTopOf="@id/dismiss"
        app:layout_constraintBottom_toBottomOf="@id/dismiss"
        app:layout_constraintLeft_toRightOf="@id/dismiss"
        android:padding="14dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        tools:text="Select operator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <View
        app:layout_constraintTop_toBottomOf="@id/dismiss"
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"/>

    <androidx.recyclerview.widget.RecyclerView
        app:layout_constraintTop_toBottomOf="@id/dismiss"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        app:layout_constrainedHeight="true"
        android:paddingTop="@dimen/list_item_spacing_half"
        android:paddingBottom="@dimen/list_item_spacing_half"
        tools:context=".fragments.OperatorListDialogFragment"
        tools:listitem="@layout/fragment_operator_list_dialog_list_dialog_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:id="@+id/ll_operator_list_wrapper"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:visibility="gone"
        android:layout_marginLeft="16dp"
        android:id="@+id/iv_recharge_provider_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginVertical="16dp"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_bsnl_logo"
        tools:visibility="visible"/>

    <TextView
        android:padding="16dp"
        android:textColor="#212121"
        android:textSize="14sp"
        android:ellipsize="end"
        android:id="@+id/tv_operator_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:text="BSNL"
        android:layout_toRightOf="@+id/iv_recharge_provider_icon"
        android:layout_centerInParent="true"/>

    <View
        android:layout_below="@id/iv_recharge_provider_icon"
        android:id="@+id/divider0"
        android:background="#eeeeee"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/iv_recharge_provider_icon"
        />

</RelativeLayout>
4

1 回答 1

1

添加android:nestedScrollingEnabled="false"RecyclerView

于 2020-09-05T06:59:17.013 回答