我一直在努力使我的RecyclerView列表可以点击。我还没有成功从点击中检索任何结果。我搜索了stackoverflow和youtube,但没有理解我实现的错误。我的适配器是
食物适配器
ublic class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {
private Context mCtx;
private List<Food> foodList;
public FoodAdapter(Context mCtx, List<Food> foodList) {
this.mCtx = mCtx;
this.foodList = foodList;
}
@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_item, null);
FoodViewHolder foodView = new FoodViewHolder(view, mCtx, foodList);
return foodView;
}
@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
Food food = foodList.get(position);
holder.txtFoodTitle.setText(food.getFoodTitle());
holder.txtFoodDesc.setText(food.getFoodDesc());
holder.txtFoodLoc.setText(food.getFoodLoc() + ", CA");
holder.txtFoodAuthor.setText(food.getFoodAuthor());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(food.getImage(), null));
}
@Override
public int getItemCount() {
return foodList.size();
}
class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView txtFoodTitle, txtFoodDesc, txtFoodLoc, txtFoodAuthor;
List<Food> foods;
Context ctx;
public FoodViewHolder(View itemView, Context ctx, List<Food> foods) {
super(itemView);
itemView.setOnClickListener(this);
this.ctx = ctx;
this.foods = foods;
imageView = itemView.findViewById(R.id.foodImg);
txtFoodTitle = itemView.findViewById(R.id.foodTitle);
txtFoodDesc = itemView.findViewById(R.id.foodDesc);
txtFoodLoc = itemView.findViewById(R.id.foodLoc);
txtFoodAuthor = itemView.findViewById(R.id.foodAuthor);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
//Food food = this.foods.get(position);
Log.i(TAG, "Clicked!");
}
}
}
FragmentList
public class FragmentList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_list, container, false);
RecyclerView recyclerView;
final FoodAdapter adapter;
final List<Food> foodList = new ArrayList<>();
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new FoodAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);
String url = "url";
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i(TAG, response);
try {
JSONArray foodResp = new JSONArray(response);
for (int i = 0; i < foodResp.length(); i++) {
JSONObject foodObj = foodResp.getJSONObject(i);
foodList.add(
new Food(
foodObj.getInt("u_id"),
R.drawable.ic_launcher_background,
foodObj.getString("mealTitle"),
foodObj.getString("mealDesc"),
foodObj.getString("cityName"),
foodObj.getString("userName")
));
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.i(TAG, "Error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "err: " + error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("data", "value");
return params;
}
};
Volley.newRequestQueue(getActivity()).add(sr);
return v;
}
我正在观看的一个原始视频显示它是使用ArrayList&lt;&gt;完成的,但我已经使用List设置了我的RecyclerView。我不认为那会很重要。任何意见,将不胜感激。谢谢!
答案 0 :(得分:0)
您需要在特定元素上设置单击侦听器。 例如,如果您想在单击图像时执行某些操作,那么:
您需要致电holder.imageview.setOnClickListener(this);
试试这个!!
如果要将单击侦听器设置为整个行,则可能需要在行项的根元素上设置单击侦听器。
答案 1 :(得分:0)
尝试在FragmentList.java
。
食物适配器
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {
private Context mCtx;
private List<Food> foodList;
public FoodAdapter(Context mCtx, List<Food> foodList) {
this.mCtx = mCtx;
this.foodList = foodList;
}
public static class FoodViewHolder extends RecyclerView.FoodViewHolder {
ImageView imageView;
TextView txtFoodTitle, txtFoodDesc, txtFoodLoc, txtFoodAuthor;
//List<Food> foods;
//Context ctx;
public FoodViewHolder(View v) {
super(v);
imageView = itemView.findViewById(R.id.foodImg);
txtFoodTitle = itemView.findViewById(R.id.foodTitle);
txtFoodDesc = itemView.findViewById(R.id.foodDesc);
txtFoodLoc = itemView.findViewById(R.id.foodLoc);
txtFoodAuthor = itemView.findViewById(R.id.foodAuthor);
}
}
@Override
public FoodAdapter.FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_item, parent, false);
return FoodViewHolder(view);
}
@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
Food food = foodList.get(position);
holder.txtFoodTitle.setText(food.getFoodTitle());
holder.txtFoodDesc.setText(food.getFoodDesc());
holder.txtFoodLoc.setText(food.getFoodLoc() + ", CA");
holder.txtFoodAuthor.setText(food.getFoodAuthor());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(food.getImage(), null));
}
@Override
public int getItemCount() {
return foodList.size();
}
}
<强> FragmentList 强>
public class FragmentList extends Fragment {
RecyclerView recyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_list, container, false);
final FoodAdapter adapter;
final List<Food> foodList = new ArrayList<>();
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new FoodAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);
String url = "url";
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i(TAG, response);
try {
JSONArray foodResp = new JSONArray(response);
for (int i = 0; i < foodResp.length(); i++) {
JSONObject foodObj = foodResp.getJSONObject(i);
foodList.add(
new Food(
foodObj.getInt("u_id"),
R.drawable.ic_launcher_background,
foodObj.getString("mealTitle"),
foodObj.getString("mealDesc"),
foodObj.getString("cityName"),
foodObj.getString("userName")
));
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.i(TAG, "Error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "err: " + error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("data", "value");
return params;
}
};
Volley.newRequestQueue(getActivity()).add(sr);
return v;
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/* --- Adding the Listener here --- */
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
//Food food = this.foods.get(position);
Log.i(TAG, "Clicked!");
}
}));
}
}
这将是您的 RecyclerItemClickListener.java 文件:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
GestureDetector mGestureDetector;
public interface OnItemClickListener{
void onItemClick(View view, int position);
}
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View childView = rv.findChildViewUnder(e.getX(),e.getY());
if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e)){
mListener.onItemClick(childView, rv.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
希望它会有所帮助:)
答案 2 :(得分:0)
处理整个项目“the viewholder”的点击次数
<强> 1。创建自定义侦听器
public class RecyclerClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
<强> 2。在
中使用我们的监听器监听点击次数 recyclerView.addOnItemTouchListener(
//use getActivity() if you are using fragment instead of MyActivity.this
new RecyclerClickListener(MyActivity.this, recyclerView, new RecyclerClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Food food= foodList.get(position);
//do whatever
}
@Override
public void onLongItemClick(View view, int position) {
// to be implement add to favourite
}
})
);
如果您正在收听视图持有者中的特定项目。
1.在适配器内创建一个内部接口并创建它的实例。
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(Food item);
}
//and also the constructor
public MyAdapter(List<Food> foodList,Context ctx,OnItemClickListener
listener){
// ......
this.mListener=mListener;
}
2.在您的观察者类中创建setListener()
方法
public void SetListener(final Food item, final OnItemClickListener listener) {
mButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
3.在你onBindView
@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
//...
viewHolder.setListener(foodList.get(position), mListener);
}
4.包括您的活动或片段
MyAdapter adapter=new MyAdapter(foodList,getActivity(),new MyAdapter.OnItemClickListener(){
@Override
public void onItemClick(Food item){
}
});