2017-08-07 10 views
1

私は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; 
} 
+0

あなたrecycleviewは他の断片の内部にありますか? –

+0

@ BrunoFerreiraいいえ、RecyclerViewはFragmentArchivos Fragment内にあります。 – JosCarrillo

+0

ok私は今理解している –

答えて

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); 
} 



} 
FragmentArchivosクラスで

@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; 
} 
+0

皆さんありがとう、これは私のために働く!ありがとうございました – JosCarrillo

0

あなたがこの方法を行う試すことができます。

public interface RecyclerViewOnClickListenerHack { 
    public void onClickListener(View view, int position); 
    public void onLongPressClickListener(View view, int position); 

} 

そしてフラグメントでこの操作を行います:

まずRecyclerViewOnClickListenerHackという名前のそのようなインターフェイスnaを作成する)

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) {} 

    } 

} 
0

リストビューは、(setOnItemClickListenerを有している方法は次のようです)メソッドではなく、RecyclerViewにはありません。下記のリンクはあなたのソリューションです:

Why doesn't RecyclerView have onItemClickListener()?

編集:あなたがあなたの活動から、この機能を呼び出したい場合は、以下のようなものが必要:

まずこのRecyclerViewを持っている(あなたの活動にメソッドを追加)を適切なパラメータで置き換えます。次に、コンテキスト変数をアクティビティーにキャストできます。キャストして、あなたのメソッドを呼び出します。これで、あなたのアクティビティには、アイテムがクリックされたリスナーがあります。

関連する問題