私はRecyclerView
からFragment
の父にクリックしたアイテムの位置を送信しようとしていましたが、私はすでに多くのチュートリアルを見ましたが、これで成功しませんでした。この特定のケースで私を助けてください:アイテムの位置をRecyclerViewからフラグメントの父親にクリック
これは私の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;
}
あなたrecycleviewは他の断片の内部にありますか? –
@ BrunoFerreiraいいえ、RecyclerViewはFragmentArchivos Fragment内にあります。 – JosCarrillo
ok私は今理解している –