Android:RecyclerView onClick Listener没有获得

时间:2015-11-16 09:02:16

标签: android android-layout android-activity android-recyclerview

我有600个项目的数组。我列出了RecyclerView中的所有数组项并滚动得很好。但是当我尝试访问 onClick 监听器时,它没有得到。我尝试实现View.OnClickListener和Override onClick.But它无法正常工作。

当用户单击视图中的某个行项

时,我想要在列表项中使用Toast项

适配器代码

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

public static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {
    private String mItem;
    private TextView mTextView;

    public ViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        mTextView = (TextView) view.findViewById(R.id.item_title);
    }

    public void setItem(String item) {
        mItem = item;
        mTextView.setText(item);
    }

    @Override
    public void onClick(View view) {
        Log.d("TAG", "onClick " + getPosition() + " " + mItem);
    }
}

private String[] mDataset;

public MyAdapter(String[] dataset) {
    mDataset = dataset;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.setItem(mDataset[position]);
}

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

}

任何人都可以帮助我 在此先感谢:)

3 个答案:

答案 0 :(得分:0)

请参阅jcobs,它可能会对您有所帮助

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // do whatever
      }
    })
);

onitemclick 中实施您的代码。 快乐的编码:)

答案 1 :(得分:0)

您可以通过将单击侦听器设置为父视图来处理它。 Recyclerview不提供与listview相同的默认项目点击列表。 因此,创建父布局的对象并将其设置为单击列表器。

你的持有人初始化。

class UserListHolder extends RecyclerView.ViewHolder {
         RelativeLayout layoutMain;

      UserListHolder(View itemView) {

         layoutMain = (RelativeLayout)itemView.findViewById(R.id.layoutSquadItem);
     }
}

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,final int position) {
((UserListHolder)viewHolder).layoutMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               //handle click
            }
    });
}

您也可以参考recycler view on click listner

答案 2 :(得分:0)

我使用你的代码onClickListener正常工作创建了一个示例项目。就像我点击项目时显示我在日志中点击的项目位置。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    private MyAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private String[] dataSet = {"Waleed", "Sarwar", "Yousuf"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new MyAdapter(dataSet);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);


    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />
</LinearLayout>

<强> MyAdapter.java

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

    private String[] mDataset;

    public MyAdapter(String[] dataset) {
        mDataset = dataset;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setItem(mDataset[position]);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private String mItem;
        private TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            mTextView = (TextView) view.findViewById(R.id.item_title);
        }

        public void setItem(String item) {
            mItem = item;
            mTextView.setText(item);
        }

        @Override
        public void onClick(View view) {
            Log.d("TAG", "onClick " + getPosition() + " " + mItem);
        }
    }
}

<强> item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="56dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

如果您仍然遇到问题,请从下面的链接下载示例项目。

https://www.dropbox.com/s/xf3y8oxok0zlgxk/RecyclerViewOnClick.zip?dl=0