2017-05-29 16 views
1

リスト内の任意のアイテムをクリックすると、2つのリスト(売り手名と商品名の両方)の「汎用」アダプタが表示されます。 onClickメソッド、ただし、私が送信したいときlistener.onClickPositionメソッドはフラグメントに到達しません。つまり、フラグメントがインタフェースを実装していない場合はそのままです。インタフェースがフラグメント上で動作しない

私の断片が、それは何も

マイアダプタ

public class AdapterRecyclerView extends 
RecyclerView.Adapter<AdapterRecyclerView.AdapterRecyclerViewViewHolder>{ 
private static Context context; 
private List items; 
private String type; 
private static OnClickAdapterRecyclerView listener; 


public AdapterRecyclerView(List items, Fragment fragment,String type) { 
    this.items = items; 
    this.type =type; 
    listener = (OnClickAdapterRecyclerView) fragment; 
} 


@Override 
public AdapterRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view=null; 
    context=parent.getContext(); 
    switch (type){ 
     case SELLER:{ 
      view = LayoutInflater.from(parent.getContext()).inflate(R.layout.seller_card, parent, false); 
      break; 
     } 
     case PRODUCTS:{ 
     ... 
     } 

    } 
    AdapterRecyclerViewViewHolder adapter = new AdapterRecyclerViewViewHolder(view); 
    return adapter; 
} 

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

    switch (type){ 
     case SELLER:{ 
      Seller seller = (Seller) items.get(position); 
      holder.binItem(seller); 
      break; 
     } 
     case PRODUCTS:{ 
     ... 
     } 
    } 
} 

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

public static class AdapterRecyclerViewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public TextView textViewNameProduct; 
    public TextView textViewNameSeller; 


    public AdapterRecyclerViewViewHolder(View v) { 
     super(v); 
     v.setOnClickListener(this); 
     switch (v.getId()){ 
      case R.id.seller_card:{ 
       textViewNameSeller = (TextView) v.findViewById(R.id.textViewNameSeller); 
       break; 
      } 
      case R.id.products_card:{ 
      ... 
      } 

     } 

    } 

    public void binItem(Seller seller){ 
     textViewNameSeller.setText(product.getName()); 
    } 

    public void binItem(Product product){ 
    ... 
    } 

    @Override 
    public void onClick(View v) { 
     //The position is printed correctly 
     Log.d("Position",getAdapterPosition()+""); 
     //Here this call does not work 
     listener.onClickPosition(getAdapterPosition()); 
    } 
} 

public interface OnClickAdapterRecyclerView{ 
    void onClickPosition(int pos); 
} 
} 

マイ・フラグメント

public class SellerListFragment extends Fragment implements AdapterRecyclerView.OnClickAdapterRecyclerView { 


public static final String SELLER="SELLER"; 


@BindView(R.id.recyclerViewSeller) 
protected RecyclerView recyclerView; 

private ArrayList<Seller>sellers; 

private AdapterRecyclerView adapterSeller; 

private OnSelectedSellerListener listener; 

public SellerListFragment() { 

} 

public static SellerListFragment newInstance() { 
    SellerListFragment fragment = new SellerListFragment(); 
    Bundle bundle = new Bundle(); 
    fragment.setArguments(bundle); 
    return fragment; 
} 


@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    sellers = new ArrayList<>(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view =inflater.inflate(R.layout.fragment_seller_list, container, false); 
    ButterKnife.bind(this, view); 
    return view; 
} 



@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    adapterSeller = new AdapterRecyclerView(sellers,SellerListFragment.this,SELLER); 
    recyclerView.setAdapter(adapterSeller); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); 

} 


@Override 
public void onClickPosition(int pos) { 
//Never called does not work 
    listener.onSelectedSellerListener(pos,sellers); 
} 


public interface OnSelectedSellerListener { 

    void onSelectedSellerListener(int position, ArrayList<Seller> seller); 
} 


@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    Activity activity; 
    if (context instanceof Activity) { 
     activity = (Activity) context; 
     try { 
      listener = (OnSelectedSellerListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + "implement OnSelectedSellerListener"); 
     } 
    } 
} 
} 

答えて

1

インタフェース

を使用せずに私の一時的な解決策に影響を与える場合、私は知らないviewpager内であることに注意してください
public void itemPosition(int position) { 
    Log.d("Position",position+""); 
} 


@Override 
    public void onClick(View v) { 
     if(context instanceof ListSellerActivity) { 
     ((ListSellerActivity)context).itemPosition(getAdapterPosition()); 
     }} 
  1. アダプタ
におけるメソッドのonClick関数を呼び出しアクティビティ
  • のメソッドを作成します。
  • 関連する問題