我有一个带有自定义onClickListener的Recyclerview。现在,当点击整个recyclerview项目时,我可以进入下一页。如何单独点击图片,文字和复选框。
我的当前代码如下:
Recycler class
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;
}
}
Recycler Adapter
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) {
}
}
答案 0 :(得分:1)
您应该创建自己的侦听器并将其传递给HomeManager
。然后在onBindViewHolder中为你想要的每个元素设置onClickListeners,并在onClick中调用侦听器中的正确方法。