我想测试RecyclerView
setOnClickListener
,但它不会生效,问题是什么?
碎片:
public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.crime_list_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setAdapter(new CrimeAdapter(CrimeLab.get(getActivity()).getCrimes()));
mRecyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
Log.d("CrimeListFragment", "test");
}
});
return view;
}
private class CrimeViewHolder extends RecyclerView.ViewHolder {
private TextView mTitleTextView;
public CrimeViewHolder(View itemView) {
super(itemView);
}
}
private class CrimeAdapter extends RecyclerView.Adapter {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes) {
this.mCrimes = crimes;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(getActivity());
final View view = inflater.inflate(R.layout.list_item_crime, parent, false);
return new CrimeViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return mCrimes.size();
}
}
}
片段布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/crime_list_view"></android.support.v7.widget.RecyclerView>
</LinearLayout>
膨胀布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_title"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:padding="4dp"
android:text="Title"/>
</RelativeLayout>
答案 0 :(得分:0)
通过开始活动而不是敬酒来测试它,我有同样的问题yasterday id没有生效与烤面包但工作与实际工作
答案 1 :(得分:0)
您可以创建自己的类来实现RecyclerView.OnItemTouchListener
来处理OnClickListner
:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
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 view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
使用方法:
RecyclerView mRecyclerView = view.findViewById(R.id.crime_list_view);
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// you can handle here
Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
Log.d("CrimeListFragment", "test");
}
})
);
答案 2 :(得分:0)
RecyclerView
不响应setOnClickListener
,
我更新了用于获取recyclerView
private class CrimeAdapter extends RecyclerView.Adapter<CrimeAdapter.ViewHolder> {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes) {
this.mCrimes = crimes;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(getActivity());
final View view = inflater.inflate(R.layout.list_item_crime, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.place_TextView.setText("" + mCrimes.get(position).getTheStringValueYouWant());
}
@Override
public int getItemCount() {
return mCrimes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView place_TextView;
public ViewHolder(View itemView) {
super(itemView);
place_TextView = (TextView) itemView.findViewById(R.id.list_item_crime_title);
place_TextView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
}
}
}
OnClick
事件与我们在RecyclerView
中使用的ListView
中的事件不同。
随着自定义列表视图在其大小的视图和动画中不断增长,Google已尝试在您的手中提供更多灵活性。
您可以采取以下措施来获取点击事件 岗位 OnItemClickListener in RecyclerView