2015-12-24 101 views
1

有沒有人使用RecyclerView找到一種方法來設置onClickListener到RecyclerView中的項目?如何使RecyclerView手柄onclicklistener

這是我的代碼:

Pasal_Bab.java

public class Pasal_Bab extends Fragment implements SearchView.OnQueryTextListener { 

private RecyclerViewEmptySupport rv; 
private List<PasalBabModel> mPBH; 
private PasalBabAdapter adapter; 
private static ArrayList<PasalBabModel> people; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.activity_pasal, container, false); 

    rv = (RecyclerViewEmptySupport) view.findViewById(R.id.rv_pasal); 
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    rv.setLayoutManager(layoutManager); 
    rv.setEmptyView(view.findViewById(R.id.empty)); 
    rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); 
    return view; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    setHasOptionsMenu(true); 

    String[] locales = Locale.getISOCountries(); 
    mPBH = new ArrayList<>(); 

    for (String countryCode : locales) { 
     Locale obj = new Locale("Pasalxyz", countryCode); 
     mPBH.add(new PasalBabModel(obj.getDisplayCountry(), obj.getISO3Country())); 
    } 

    adapter = new PasalBabAdapter(mPBH); 
    rv.setAdapter(adapter); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu_main, menu); 

    final MenuItem item = menu.findItem(R.id.action_search); 
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); 
    searchView.setOnQueryTextListener(this); 

    MenuItemCompat.setOnActionExpandListener(item, 
      new MenuItemCompat.OnActionExpandListener() { 
       @Override 
       public boolean onMenuItemActionCollapse(MenuItem item) { 
        // Do something when collapsed 
        adapter.setFilter(mPBH); 
        return true; // Return true to collapse action view 
       } 

       @Override 
       public boolean onMenuItemActionExpand(MenuItem item) { 
        // Do something when expanded 
        return true; // Return true to expand action view 
       } 
      }); 
} 

@Override 
public boolean onQueryTextSubmit(String query) { 
    return false; 
} 

@Override 
public boolean onQueryTextChange(String newText) { 
    final List<PasalBabModel> filteredModelList = filter(mPBH, newText); 
    adapter.animateTo(filteredModelList); 
    adapter.setFilter(filteredModelList); 
    rv.scrollToPosition(0); 
    return false; 
} 

private List<PasalBabModel> filter(List<PasalBabModel> models, String query) { 
    query = query.toLowerCase(); 

    final List<PasalBabModel> filteredModelList = new ArrayList<>(); 
    for (PasalBabModel model : models) { 
     final String text = model.getpasalbab_p().toLowerCase(); 
     if (text.contains(query)) { 
      filteredModelList.add(model); 
     } 
    } 
    return filteredModelList; 
} 

} 

PasalBabAdapter.java

public class PasalBabAdapter extends RecyclerView.Adapter<PasalBabVH> { 

private List<PasalBabModel> mPasalBabModel; 

public PasalBabAdapter(List<PasalBabModel> mPasalBabModel) { 
    this.mPasalBabModel = mPasalBabModel; 
} 

@Override 
public void onBindViewHolder(PasalBabVH PasalBabVH, int i) { 
    final PasalBabModel model = mPasalBabModel.get(i); 
    PasalBabVH.bind(model); 
} 

@Override 
public PasalBabVH onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.pasalbab_row, viewGroup, false); 
    return new PasalBabVH(view); 
} 


public void setFilter(List<PasalBabModel> PasalBabModels) { 
    mPasalBabModel = new ArrayList<>(); 
    mPasalBabModel.addAll(PasalBabModels); 
    notifyDataSetChanged(); 
} 

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

public void animateTo(List<PasalBabModel> models) { 
    applyAndAnimateRemovals(models); 
    applyAndAnimateAdditions(models); 
    applyAndAnimateMovedItems(models); 
} 

private void applyAndAnimateRemovals(List<PasalBabModel> newModels) { 
    for (int i = mPasalBabModel.size() - 1; i >= 0; i--) { 
     final PasalBabModel model = mPasalBabModel.get(i); 
     if (!newModels.contains(model)) { 
      removeItem(i); 
     } 
    } 
} 

private void applyAndAnimateAdditions(List<PasalBabModel> newModels) { 
    for (int i = 0, count = newModels.size(); i < count; i++) { 
     final PasalBabModel model = newModels.get(i); 
     if (!mPasalBabModel.contains(model)) { 
      addItem(i, model); 
     } 
    } 
} 

private void applyAndAnimateMovedItems(List<PasalBabModel> newModels) { 
    for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) { 
     final PasalBabModel model = newModels.get(toPosition); 
     final int fromPosition = mPasalBabModel.indexOf(model); 
     if (fromPosition >= 0 && fromPosition != toPosition) { 
      moveItem(fromPosition, toPosition); 
     } 
    } 
} 

public PasalBabModel removeItem(int position) { 
    final PasalBabModel model = mPasalBabModel.remove(position); 
    notifyItemRemoved(position); 
    return model; 
} 

public void addItem(int position, PasalBabModel model) { 
    mPasalBabModel.add(position, model); 
    notifyItemInserted(position); 
} 

public void moveItem(int fromPosition, int toPosition) { 
    final PasalBabModel model = mPasalBabModel.remove(fromPosition); 
    mPasalBabModel.add(toPosition, model); 
    notifyItemMoved(fromPosition, toPosition); 
} 
} 

PasalBabVH.java

public class PasalBabVH extends RecyclerView.ViewHolder { 

public TextView p_TextView; 
public TextView b_TextView; 

public PasalBabVH(View itemView) { 
    super(itemView); 
    p_TextView = (TextView) itemView.findViewById(R.id.uud_pasal); 
    b_TextView = (TextView) itemView.findViewById(R.id.uud_bab); 
} 

public void bind(PasalBabModel mx) { 
    p_TextView.setText(mx.getpasalbab_p()); 
    b_TextView.setText(mx.getpasalbab_b()); 
} 

} 

PasalBabModel.java

public class PasalBabModel { 

String pasalbab_p; 
String pasalbab_b; 

public PasalBabModel(String pasalbab_p, String pasalbab_b) { 
    this.pasalbab_p = pasalbab_p; 
    this.pasalbab_b = pasalbab_b; 
} 


public String getpasalbab_p() { 
    return pasalbab_p; 
} 

public String getpasalbab_b() { 
    return pasalbab_b; 
} 
} 

請helpme ...我是一個初學者:)

+0

通過此http轉到:// www.androidhive.info/2015/04/android-getting-started-with-material-design/ – Wizard

+0

謝謝,但它不起作用 –

回答

2

創建適配器像這樣:

public class CustomAdapter extends RecyclerView.Adapter<Custom.ViewHolder> { 

    private List<SomeObject> mDataSet; 
    public OnItemClickListener mItemClickListener; 
    private Context mContext; 

    public CustomAdapter(Context context, List<SomeObject> myDataset, OnItemClickListener mItemClickListener) { 
     this.mDataSet = myDataset; 
     this.mItemClickListener = mItemClickListener; 
     this.mContext = context; 
    } 

    public void updateData(List<SomeObject> mDataSet) { 
     this.mDataSet = mDataSet; 
     notifyDataSetChanged(); 
    } 

    public void removeItem(int position) { 
     mDataSet.remove(position); 
     notifyItemRemoved(position); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     // create a new view 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); 
     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 

     final SomeObject obj = mDataSet.get(position); 

     holder.name.setText(obj.getName()); 
    } 

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 

     TextView name; 

     public ViewHolder(View v) { 
      super(v); 
      name = (TextView) v.findViewById(R.id.naam); 
      v.setOnClickListener(this); 
      v.setOnLongClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      // If not long clicked, pass last variable as false. 
      mItemClickListener.onItemClick(v, getLayoutPosition(), false, mDataSet); 
     } 

     @Override 
     public boolean onLongClick(View v) { 
      // If long clicked, passed last variable as true. 
      mItemClickListener.onItemClick(v, getLayoutPosition(), true, mDataSet); 
      return true; 
     } 
    } 

    public interface OnItemClickListener { 
     void onItemClick(View view, int position, boolean isLongClick, List<SomeObject> mFilteredList); 
    } 
} 

,並設置適配器像這樣:

mAdapter = new CustomAdapter(getActivity(), dataSet, new CustomAdapter.OnItemClickListener() { 

     @Override 
     public void onItemClick(View v, int position, boolean isLongClick, List<SomeObject> mDataSet) { 
      if (isLongClick) { 
       //Long click 
       SomeObject obj = mDataSet.get(position); 
      } else { 
       //Normal click 
       SomeObject obj = mDataSet.get(position); 
      } 
     } 
    }); 
1

使用接口來處理onClicks。

在適配器將這個:

public interface OnItemClickListener { 
     void onClickItem(View view, int position); 
    } 
    public void SetOnItemClickListener(OnItemClickListener mItemClickListener) { 
     this.mItemClickListener = mItemClickListener; 
    } 

,然後在視圖持有人做到這一點:

public class PasalBabVH extends RecyclerView.ViewHolder { 

public TextView p_TextView; 
public TextView b_TextView; 

public PasalBabVH(View itemView) { 
    super(itemView); 
    p_TextView = (TextView) itemView.findViewById(R.id.uud_pasal); 
    b_TextView = (TextView) itemView.findViewById(R.id.uud_bab); 
p_TextView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mItemClickListener != null) { 

        } 
       } 
      }); 
b_TextView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (mItemClickListener != null) { 

        } 
       } 
      }); 
} 

public void bind(PasalBabModel mx) { 
    p_TextView.setText(mx.getpasalbab_p()); 
    b_TextView.setText(mx.getpasalbab_b()); 
} 

} 

在片段做到這一點:

adapter.SetOnItemClickListener(new PasalBabAdapter.OnItemClickListener() { 
       @Override 
       public void onClickItem(View view, int position) { 
}});