2017-02-02 17 views
-3

私はこのトピックに関していくつかのリンクを紹介しましたが、RecyclerViewからFragmentへのデータの受け渡し方法はまだ理解できませんでした[Cardviewクリックでフラグメントを開く]。次は私のRecyclerViewクラスです:RecyclerViewからフラグメントにデータを渡す

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotosHolder> 
{ 
static class PhotosHolder extends RecyclerView.ViewHolder 
{ 
    CardView cv; 
    ImageView photo_img; 

    PhotosHolder(final View itemView) 
    { 
     super(itemView); 
     cv = (CardView) itemView.findViewById(R.id.cv_photo); 
     photo_img = (ImageView) itemView.findViewById(R.id.thum_photo);   
    } 
} 

private List<PhotoInitialise> photo; 
private Activity mContext; 

public PhotoAdapter(List<PhotoInitialise> photos, Context mContext) 
{ 
    this.photo=photos; 
    this.mContext= (Activity) mContext; 
} 
@Override 
public PhotosHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.phototab_layout,parent,false); 
    return new PhotosHolder(layoutView); 
} 

@Override 
public void onBindViewHolder(PhotosHolder holder, final int position) 
{  

    holder.photo_count.setText(photo.get(position).gettotalImages()); 
    holder.cv.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     {     
      // want to pass the value to a Fragment here and invoke the Fragment 
     } 
    }); 
} 


@Override 
public int getItemCount() 
{ 
    return photo.size(); 
} 
} 

これが表示されるべきフラグメントのXMLレイアウトです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/photo_detail_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/photodetail_description"   
    android:textSize="20sp" 
    android:paddingLeft="5dp" 
    android:layout_marginTop="5dp" 
    android:paddingRight="5dp"> 

</RelativeLayout> 

私はすでにフラグメントに値を渡す方法についてのアイデアを持っていますが、私ができることはありませんレイアウトを呼び出すことができます。私はonClick()に書いて、フラグメント・レイアウトを開始するにはどうすればよいですか?

答えて

0

指定されたフラグメントのクリックを処理するインターフェイスを実装します。 サンプルコードの構造は以下のようになります。

お使いのアダプタのアクセスのコンストラクタインターフェイスで
public class SampleFragment extends BaseFrgment implements InterfaceListItemClickListener{ 

    @Override 
    public void listItemClickAction(<parameters to pass>) { 
     //override the interface function  
     //handle your click action here 
    } 

    //pass the instance of your interface to your adapter like below line 
    YourAdapter yourAdapter = new YourAdapter(otherParameters, this); 
} 

public YourAdapter(otherParameters, InterfaceListItemClickListener clickListenerInferface){ 
    //other assignations 
    this.clickListenerInferface = clickListenerInferface; 
} 

とあなたのフラグメントのクラスで

public InterfaceListItemClickListener{ 
    void listItemClickAction(<parameters to pass>); 
} 

このインタフェースを実装する:あなたはあなたの必要性に応じてカスタマイズし、それを使用することができますonClickコールバックを呼び出すことができます:

clickListenerInferface.listItemClickAction(<parameter to pass>); 
+0

私は、recyclerviewクラスからfragmentを呼び出す方法を知りたいだけです。 [cardviewクリックでフラグメントを呼び出す] –

関連する問題