gpt4 book ai didi

android - 自定义 RecyclerClickListener 无法针对多个 onClicks 正常工作

转载 作者:行者123 更新时间:2023-11-30 01:43:19 25 4
gpt4 key购买 nike

我有一个带有自定义 onClickListener 的 Recyclerview。现在我可以在点击整个 recyclerview 项目时转到下一页。我将如何单独点击图像、文本和复选框。

我目前的代码如下:

回收类

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.root5solutions.mirrealtors.HomeRecyclerDetailActivity;
import com.example.root5solutions.mirrealtors.R;
import com.example.root5solutions.mirrealtors.projectdatabase.HomeManager;
import com.example.root5solutions.mirrealtors.projectdatabase.HomeData;
import com.example.root5solutions.mirrealtors.projectdatabase.RecyclerClickListener;
import com.kogitune.activity_transition.ActivityTransitionLauncher;

public class RecyclerTab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_tab1_recycler, container, false);
RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rv.setLayoutManager(llm);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setHasFixedSize(true); // to improve performance
rv.setAdapter(new HomeManager()); // the projectdatabase manager is assigner to the RV

rv.addOnItemTouchListener( // and the click is handled
new RecyclerClickListener(getContext(), new RecyclerClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getActivity(), HomeRecyclerDetailActivity.class);
intent.putExtra(HomeRecyclerDetailActivity.ID, HomeData.PROJECTS[position].getId());
ActivityTransitionLauncher.with(getActivity()).from(view).launch(intent);
}
}));
return v;
}
}

回收适配器

import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.root5solutions.mirrealtors.R;


public class HomeManager extends RecyclerView.Adapter<HomeManager.RecyclerViewHolder> {

public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

TextView mProjectName, mProjectCity, mProjectType;
ImageView mImage;
CheckBox mCheck;
RelativeLayout mProjectStatus;

RecyclerViewHolder(View itemView) {
super(itemView);
mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
mProjectCity = (TextView) itemView.findViewById(R.id.PROJECT_city);
mProjectType = (TextView) itemView.findViewById(R.id.PROJECT_type);
mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
mProjectStatus = (RelativeLayout) itemView.findViewById(R.id.label);
}
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v);
}

@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
// get the single element from the main array

final HomeData projects = HomeData.PROJECTS[i];
// Set the values
viewHolder.mProjectName.setText(projects.get(HomeData.Field.NAME));
viewHolder.mProjectCity.setText(projects.get(HomeData.Field.CITY));
viewHolder.mProjectType.setText(projects.get(HomeData.Field.TYPE));
viewHolder.mImage.setImageResource(projects.geti(HomeData.Field.IMAGE));

}


@Override
public int getItemCount() {

return HomeData.PROJECTS.length;
}


}

自定义 RecyclerClickListener

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


public class RecyclerClickListener implements RecyclerView.OnItemTouchListener {

private OnItemClickListener mOnClickListener;
GestureDetector mGestureDetector;

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

public RecyclerClickListener(Context context, OnItemClickListener listener) {
mOnClickListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mOnClickListener != null && mGestureDetector.onTouchEvent(e)) {
mOnClickListener.onItemClick(childView, view.getChildPosition(childView));
return true;
}
return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

}

最佳答案

您应该创建自己的监听器并将其传递给 HomeManager。然后在 onBindViewHolder 中为您想要的每个元素设置 onClickListeners,并在 onClick 中调用监听器中的正确方法。

关于android - 自定义 RecyclerClickListener 无法针对多个 onClicks 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34129423/

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