RecyclerView点击事件不起作用

时间:2018-08-29 15:40:30

标签: java android xml android-recyclerview android-adapter

由于某些原因,尽管项目显示为正常,但单击事件对我的RecyclerView无效。我已经根据屏幕尺寸使用了2种不同的适配器。有谁知道为什么它不起作用以及如何解决?

recyclerview_item_gridcell(用于大屏幕)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:padding="20dp"
    android:clickable="true"
    android:focusable="true"   
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

recyclerview_item_textview(用于较小的屏幕)

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"            
    android:background="?android:attr/selectableItemBackground"
    />

活动课程

public class MainActivity extends Activity implements MyRecyclerViewAdapterGL.ItemClickListener, MyRecyclerViewAdapterLL.ItemClickListener {

    boolean themeState;
    MyRecyclerViewAdapterGL adapterGL;
    MyRecyclerViewAdapterLL adapterLL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean isScreenSmall = getResources().getBoolean(R.bool.is_screen_small);

        setContentView(R.layout.activity_main);

        String[] dataArray = {"Item A", "Item B", "Item C", "Item D" ,"Item E" ,"Item F"};

        if(isScreenSmall){
            // ItemDecoration for small devices
            RecyclerView recyclerViewLL = findViewById(R.id.recyclerView_list);
            recyclerViewLL.setLayoutManager(new LinearLayoutManager(this));
            adapterLL = new MyRecyclerViewAdapterLL(this, dataArray);
            adapterLL.setClickListener(this);
            recyclerViewLL.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
            recyclerViewLL.setAdapter(adapterLL);
        }
        else{
            // ItemDecoration for large devices
            RecyclerView recyclerViewGL = findViewById(R.id.recyclerView_list);
            int numberOfColumns = 2;
            recyclerViewGL.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
            adapterGL = new MyRecyclerViewAdapterGL(this, dataArray);
            adapterGL.setClickListener(this);
            recyclerViewGL.setAdapter(adapterGL);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        if (position == 0) {
            Toast.makeText(MainActivity.this, "Item A was clicked", LENGTH_SHORT).show();
        } else if (position == 1) {
            Toast.makeText(MainActivity.this, "Item B was clicked", LENGTH_SHORT).show();
        } else if (position == 2) {
            Toast.makeText(MainActivity.this, "Item C was clicked", LENGTH_SHORT).show();
        }
    }
}

RecyclerView适配器类(用于较小的屏幕)

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

    private String[] mData;
    private LayoutInflater mInflater;
    private MyRecyclerViewAdapterLL.ItemClickListener mClickListener;

    MyRecyclerViewAdapterLL(Context context, String[] data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    @Override
    @NonNull
    public MyRecyclerViewAdapterLL.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_item_textview, parent, false);
        return new MyRecyclerViewAdapterLL.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerViewAdapterLL.ViewHolder holder, int position) {
        holder.myTextView.setText(mData[position]);
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.item_text);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    String getItem(int id) {
        return mData[id];
    }

    void setClickListener(MyRecyclerViewAdapterLL.ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

RecyclerView适配器类(用于大屏幕)

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

    private String[] mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    MyRecyclerViewAdapterGL(Context context, String[] data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    @Override
    @NonNull
    public MyRecyclerViewAdapterGL.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_item_gridcell, parent, false);
        return new MyRecyclerViewAdapterGL.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.myTextView.setText(mData[position]);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.info_text);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    String getItem(int id) {
        return mData[id];
    }

    void setClickListener(MyRecyclerViewAdapterGL.ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

2 个答案:

答案 0 :(得分:1)

检查您的根视图在xml单元格中是否可单击和可聚焦。您应该能够看到触地时的涟漪。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="20dp"
android:layout_width="match_parent"
android:clickable="true"
android:focusable="true"
android:layout_height="wrap_content"> ....

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
/>

更新2

您的线性布局没有可触摸的区域。这就是为什么我们无法触发click方法的原因,因为该按钮填充了根目录。我们无法触及根元素。因此,您应该在该状态下使用button.setClickListener。

答案 1 :(得分:1)

在适配器类(两个类)中 您可以在整个项目视图上设置OnClickListener,而应该在OnClickListener上设置myTextView

所以会像

myTextView = itemView.findViewById(R.id.info_text);
            myTextView.setOnClickListener(this);