gpt4 book ai didi

java - RecyclerView ClickListener 无法识别触摸

转载 作者:行者123 更新时间:2023-12-01 06:23:07 25 4
gpt4 key购买 nike

我已经创建了一个RecyclerView,并且在适配器中我希望创建一个ClickListener。我希望 RecyclerView 中的每个项目在单击后发送唯一的响应。换句话说,一次点击会为每个特定的 RecyclerView 项目加载不同的 fragment 。现在,RecyclerView 没有响应,因为我已经记录了它,甚至添加了一个 Toast,但没有任何运气:

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


private ArrayList<LatestPoll> mDataSet;
int lastPosition = -1;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
protected TextView pollQuestion;
protected ImageView pollImage;
protected RelativeLayout individualItem;


public ViewHolder(View v) {
super(v);
pollQuestion = (TextView) v.findViewById(R.id.latest_item_question);
pollImage = (ImageView) v.findViewById(R.id.pollThumbNailImage);
individualItem = (RelativeLayout) v.findViewById(R.id.individual_item);
individualItem.setOnClickListener(this);



}

@Override
public void onClick(View v) {
v.getId();
int itemPosition = getLayoutPosition();
String item = String.valueOf(mDataSet.get(itemPosition));
Toast.makeText(getActivity().getApplicationContext(), item, Toast.LENGTH_LONG).show();
Log.v("TESTING", "ITEM CLICKED!");
}
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<LatestPoll> myDataset) {
mDataSet = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.latest_item, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)

//The OutOfBoundsException is pointing here
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.v("ON_BIND", "ON_BINDVIEWHOLDER CALLED");
LatestPoll latestPoll = mDataSet.get(position);
holder.pollQuestion.setText(latestPoll.getQuestion());
Picasso.with(getActivity())
.load(latestPoll.getPollImage())
.fit()
.placeholder(R.drawable.loading_spinnter_white)
.into(holder.pollImage);

}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.size();
}
}

private void onLoad() {
if (mFragmentListener != null) {
mFragmentListener.onFragmentLoaded();
}
}

public interface FragmentListener {
void onFragmentLoaded();
}


}

最佳答案

您需要将 onclick 监听器设置为 onBindViewHolder 内的父布局。

holder.parentID.setOnClickListener(mRecyclerViewListener);

在您的 View 持有者中

parentID = iteView.findViewById(R.id.parent);

关于java - RecyclerView ClickListener 无法识别触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38164430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com