我一直在尝试将从RecyclerView
点击的项目位置发送到Fragment
父亲,我已经看过很多教程,但我没有成功完成这项工作。请帮我解决这个具体案例:
这是我的Adapter
班级:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_FOOTER = 2;
private HeaderViewHolder headerHolder;
private ArrayList<Doc> documentos;
private Context context;
private ArrayAdapter<String> arrayAdapter;
private SQLiteHandler db;
public MyAdapter(Context context, ArrayList<Doc> generics) {
this.context = context;
this.documentos = generics;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_header, parent, false);
return new HeaderViewHolder (v);
} else if(viewType == TYPE_FOOTER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_foter, parent, false);
return new FooterViewHolder (v);
} else if(viewType == TYPE_ITEM) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_item, parent, false);
return new GenericViewHolder (v);
}
return null;
}
private Doc getItem (int position) {
return documentos.get (position);
}
@Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof HeaderViewHolder) {
headerHolder = (HeaderViewHolder) holder;
headerHolder.card_view_header.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
createDialogServices();
}
});
} else if(holder instanceof FooterViewHolder) {
FooterViewHolder footerHolder = (FooterViewHolder) holder;
//footerHolder.txtTitleFooter.setText ("Footer");
footerHolder.send_Button.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
Toast.makeText (context, "Enviando documentos...", Toast.LENGTH_SHORT).show ();
}
});
} else if(holder instanceof GenericViewHolder) {
Doc currentItem = getItem (position - 1);
final Integer num = currentItem.getPosition();
GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
genericViewHolder.txtTitle.setText(currentItem.getTittle());
genericViewHolder.txtDescription.setText(currentItem.getDescription());
genericViewHolder.txtTitle.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
ImagePicker.pickImage((Activity) context, "Select your image:");
}
});
}
}
@Override
public int getItemViewType (int position) {
if(isPositionHeader (position)) {
return TYPE_HEADER;
} else if(isPositionFooter (position)) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader (int position) {
return position == 0;
}
private boolean isPositionFooter (int position) {
return position == documentos.size () + 1;
}
@Override
public int getItemCount () {
return documentos.size () + 2;
}
private class FooterViewHolder extends RecyclerView.ViewHolder {
Button send_Button;
FooterViewHolder(View itemView) {
super (itemView);
this.send_Button = (Button) itemView.findViewById (R.id.btn_send_docs);
}
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
CardView card_view_header;
TextView card_txt_title;
TextView card_txt_description;
HeaderViewHolder(View itemView) {
super (itemView);
this.card_view_header = (CardView) itemView.findViewById (R.id.card_view_header);
this.card_txt_title = (TextView) itemView.findViewById(R.id.card_header_textView_tittle);
this.card_txt_description = (TextView) itemView.findViewById(R.id.card_header_textView_description);
}
}
private class GenericViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
TextView txtDescription;
GenericViewHolder(View itemView) {
super(itemView);
this.txtTitle = (TextView) itemView.findViewById(R.id.card_item_textView_tittle);
this.txtDescription = (TextView) itemView.findViewById(R.id.card_item_textView_description);
}
}
这是我的Fragment
班级:
public class FragmentArchivos extends Fragment {
private RecyclerView.Adapter MyAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView rv;
private Button btn_send_fr_docs;
private Bitmap bitmap;
private ImageView photo;
private SQLiteHandler db;
private Integer x;
public FragmentArchivos(){
//Empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View myView = inflater.inflate(R.layout.fr_docs, container, false);
db = new SQLiteHandler(getActivity());
getAllDocsFromServer();
x = db.getDocsCount();
RecyclerView recyclerView = (RecyclerView) myView.findViewById (R.id.cardList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager (getActivity());
MyAdapter adapter = new MyAdapter(getActivity(), getDocumentList());
recyclerView.setLayoutManager (linearLayoutManager);
recyclerView.setAdapter (adapter);
return myView;
}
答案 0 :(得分:0)
您可以尝试这样做。
首先创建名为RecyclerViewOnClickListenerHack
的
public interface RecyclerViewOnClickListenerHack {
public void onClickListener(View view, int position);
public void onLongPressClickListener(View view, int position);
}
然后在片段中执行此操作:
1)实现RecyclerViewOnClickListenerHack;
2)添加方法;
3)然后在Onclick方法中你有一个参数位置。方法是这样的:
@Override
public void onClickListener(View view, int position) {
}
4)最后将此添加到oncreateview()方法中的recycleview:
recyclerViewtodas.addOnItemTouchListener(new RecyclerViewTouchListener(getActivity(), your_recycleview, this));
所以完整的代码是这样的:
public class separadortodas extends Fragment implements RecyclerViewOnClickListenerHack {
...
@Override
public void onClickListener(View view, int position) {
}
@Override
public void onLongPressClickListener(View view, int position) {
}
private static class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
private Context mContext;
private GestureDetector mGestureDetector;
private RecyclerViewOnClickListenerHack mRecyclerViewOnClickListenerHack;
RecyclerViewTouchListener(Context c, final RecyclerView rv, RecyclerViewOnClickListenerHack rvoclh){
mContext = c;
mRecyclerViewOnClickListenerHack = rvoclh;
mGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener(){
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return(true);
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {}
}
}
答案 1 :(得分:0)
ListView有setOnItemClickListener()方法,但RecyclerView没有。以下链接是您的解决方案:
Why doesn't RecyclerView have onItemClickListener()?
编辑:如果您想从活动中调用此功能,则需要以下内容:
首先在您的活动(具有此RecyclerView)中添加具有适当参数的方法。其次,您可以将上下文变量强制转换为您的活动。投下它并调用你的方法。现在,您的活动有一个监听器,可以单击该项目。
答案 2 :(得分:0)
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_FOOTER = 2;
private HeaderViewHolder headerHolder;
private ArrayList<Doc> documentos;
private Context context;
private ArrayAdapter<String> arrayAdapter;
private SQLiteHandler db;
MyClickListener myClickListener;
public MyAdapter(Context context, ArrayList<Doc> generics, MyClickListener myClickListener) {
this.context = context;
this.documentos = generics;
this. myClickListener = myClickListener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_header, parent, false);
return new HeaderViewHolder (v);
} else if(viewType == TYPE_FOOTER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_foter, parent, false);
return new FooterViewHolder (v);
} else if(viewType == TYPE_ITEM) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.card_item, parent, false);
return new GenericViewHolder (v);
}
return null;
}
private Doc getItem (int position) {
return documentos.get (position);
}
@Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof HeaderViewHolder) {
headerHolder = (HeaderViewHolder) holder;
headerHolder.card_view_header.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
myClickListener.onItemClick(getAdapterPosition())
createDialogServices();
}
});
} else if(holder instanceof FooterViewHolder) {
FooterViewHolder footerHolder = (FooterViewHolder) holder;
//footerHolder.txtTitleFooter.setText ("Footer");
footerHolder.send_Button.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
myClickListener.onItemClick(getAdapterPosition())
Toast.makeText (context, "Enviando documentos...", Toast.LENGTH_SHORT).show ();
}
});
} else if(holder instanceof GenericViewHolder) {
Doc currentItem = getItem (position - 1);
final Integer num = currentItem.getPosition();
GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
genericViewHolder.txtTitle.setText(currentItem.getTittle());
genericViewHolder.txtDescription.setText(currentItem.getDescription());
genericViewHolder.txtTitle.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
myClickListener.onItemClick(getAdapterPosition())
ImagePicker.pickImage((Activity) context, "Select your image:");
}
});
}
}
@Override
public int getItemViewType (int position) {
if(isPositionHeader (position)) {
return TYPE_HEADER;
} else if(isPositionFooter (position)) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader (int position) {
return position == 0;
}
private boolean isPositionFooter (int position) {
return position == documentos.size () + 1;
}
@Override
public int getItemCount () {
return documentos.size () + 2;
}
private class FooterViewHolder extends RecyclerView.ViewHolder {
Button send_Button;
FooterViewHolder(View itemView) {
super (itemView);
this.send_Button = (Button) itemView.findViewById (R.id.btn_send_docs);
}
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
CardView card_view_header;
TextView card_txt_title;
TextView card_txt_description;
HeaderViewHolder(View itemView) {
super (itemView);
this.card_view_header = (CardView) itemView.findViewById (R.id.card_view_header);
this.card_txt_title = (TextView) itemView.findViewById(R.id.card_header_textView_tittle);
this.card_txt_description = (TextView) itemView.findViewById(R.id.card_header_textView_description);
}
}
private class GenericViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
TextView txtDescription;
GenericViewHolder(View itemView) {
super(itemView);
this.txtTitle = (TextView) itemView.findViewById(R.id.card_item_textView_tittle);
this.txtDescription = (TextView) itemView.findViewById(R.id.card_item_textView_description);
}
public interface MyClickListener {
public void onItemClicked(int position);
}
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View myView = inflater.inflate(R.layout.fr_docs, container, false);
db = new SQLiteHandler(getActivity());
getAllDocsFromServer();
x = db.getDocsCount();
RecyclerView recyclerView = (RecyclerView) myView.findViewById (R.id.cardList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager (getActivity());
MyAdapter adapter = new MyAdapter(getActivity(), getDocumentList(), new MyClickListener(){
@Override
public void onItemClick(int position) {
//TODO do ur work here
}
});
recyclerView.setLayoutManager (linearLayoutManager);
recyclerView.setAdapter (adapter);
return myView;
}