2016-07-28 99 views
0

我的適配器類:它得到實現設置onclicklistener在recyclerview

public class Adaptor extends RecyclerView.Adapter<Adaptor.Holder>{ 

private ArrayList<Winkel> winkels; 
private LayoutInflater inflater; 
private ImageView icon; 
private ItemCLickCallback itemCLickCallback; 

public interface ItemCLickCallback { 
    void onItemClick(int p); 
    void onSecItemClick(int p); 

} 

public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){ 
    ;this.itemCLickCallback = itemCLickCallback; 
} 

public Adaptor (ArrayList<Winkel> winkels,Context c){ 
    this.inflater = LayoutInflater.from(c); 
    this.winkels = winkels; 
} 


@Override 
public Holder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.list_item,parent,false); 
    return new Holder(view); 
} 

@Override 
public void onBindViewHolder(Holder holder, int position) { 
    Winkel winkel = winkels.get(position); 
    holder.title.setText(winkel.getNaam()); 
    if (winkel.isFavourtite()){ 
     holder.icon.setImageResource(R.drawable.ic_star_black_18dp); 
    } 
    else{ 
     holder.icon.setImageResource(R.drawable.ic_star_border_black_18dp); 
    } 

} 

@Override 
public int getItemCount() { 

    return winkels.size(); 
} 

class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

    private TextView title; 
    private View container; 
    private ImageView icon; 

    public Holder(View itemView) { 
     super(itemView); 

     title = (TextView)itemView.findViewById(R.id.lbl_item_text); 
     container = itemView.findViewById(R.id.cont_item_root); 
     icon = (ImageView) itemView.findViewById(R.id.im_item_icon_secondary); 
     icon.setOnClickListener(this); 
     container.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v.getId()==R.id.cont_item_root){ 
      itemCLickCallback.onItemClick(getAdapterPosition()); 

     } 
     else{ 
      itemCLickCallback.onSecItemClick(getAdapterPosition()); 
     } 
    } 
} 
} 


    @Override 
    public void onClick(View v) { 

    } 
} 
} 

代碼(我的問題是在這裏adaptor.setItemClickCallback(上下文)

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    Adaptor adaptor = new Adaptor(winkels,context); 
    recyclerview.setLayoutManager(new LinearLayoutManager(context)); 
    recyclerview.addItemDecoration(new VerticalSpace(30)); 
    recyclerview.setAdapter(adaptor); 
    adaptor.setItemCLickCallback(context); 

} 

從片段概覽,稱爲:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_overview, container, false); 
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle); 
    ODKortrijkWebservice webs = new ODKortrijkWebservice(this.getActivity(),mRecyclerView); 
    webs.execute(); 
    return view; 
} 

如何設置ItemCLickCallback(this)的上下文?它應該是上下文(至少我認爲所以?),因爲這是從片段傳遞,但我似乎無法弄清楚如何讓這個工作,所以一些幫助,將不勝感激。基本上,我想爲我的recyclerview中的每個項目添加onclicklisteners,而recyclerview是使用來自webservice的數據創建的,因此,爲什麼從我從Web服務獲取數據的類中調用適配器類,這會在我的分段。謝謝。

回答

1

我創建一個接口推薦你製作ODKortrijkWebservice.Callback李可以關注。

在你ODKortrijkWebservice:

public class ODKortrijkWebservice extends AsyncTask<...> { 

    private Callback mCallback; 

    public interface Callback { 
     void onSuccess(ArrayList<Winkel> winkels); 
    } 

    public ODKortrijkWebservice(Context context, Callback callback) { 
     ... 
     mCallback = callback; 
     ... 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     ... 
     mCallback.onSuccess(winkels); 
    } 
} 

在您的片段:

public class YourFragment extends Fragment implements ODKortrijkWebservice.Callback, Adaptor.ItemCLickCallback { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_overview, container, false); 
     mRecyclerView = (RecyclerView) view.findViewById(R.id.recycle); 
     ODKortrijkWebservice webs = new ODKortrijkWebservice(this.getActivity(), this); 
     webs.execute(); 

     return view; 
    } 

    @Override 
    public void onSuccess(ArrayList<Winkel> winkels) { 
     Adaptor adaptor = new Adaptor(winkels, getContext()); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
     mRecyclerView.addItemDecoration(new VerticalSpace(30)); 
     mRecyclerView.setAdapter(adaptor); 
     adaptor.setItemCLickCallback(this); 
    } 

    @Override 
    public void onItemClick(int p) { 

    } 

    @Override 
    public void onSecItemClick(int p) { 

    } 
} 
+0

謝謝你,好主意! – user3117628

0

這裏, 你需要在RecycleView.Adapter <>

private static OnItemClickListener listener; 
    // Define the listener interface 
    public interface OnItemClickListener { 
     void onItemClick(View itemView, int position); 
    } 
    // Define the method that allows the parent activity or fragment to define the listener 
    public void setOnItemClickListener(OnItemClickListener listener) { 
     this.listener = listener; 
    } 

發送要求的細節與接口方法

public class ViewHolder extends RecyclerView.ViewHolder{ 

     TextView header_title; 

     public ViewHolder(final View itemView) { 
      super(itemView); 

      txt_country_name = (TextView)itemView.findViewById(R.id.header_title); 


      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // Triggers click upwards to the adapter on click 
        if (listener != null) 
         listener.onItemClick(itemView, getLayoutPosition()); 
       } 
      }); 
     } 

    } 

呼叫轉接器監聽

ListAdapter listAdapter; 

listAdapter.setOnItemClickListener(new listAdapter.OnItemClickListener() { 
      @Override 
      public void onItemClick(View view, int position) { 
      Toast.makeText(getActivity(), position+ " was clicked!", Toast.LENGTH_SHORT).show(); 

      } 
     });