如何在 RecyclerView 的 CardView 中实现对 Item Click 的操作以显示结果
Posted
技术标签:
【中文标题】如何在 RecyclerView 的 CardView 中实现对 Item Click 的操作以显示结果【英文标题】:How to implement an Action on Item Click in Order to show the result in CardView of RecyclerView 【发布时间】:2020-08-27 11:24:18 【问题描述】:我对 android 编程非常陌生。我找不到解决我几天来一直试图解决的当前问题的方法。
我想点击填充数组列表的项目并获取位置,即 Cardview 中的搜索词结果(ItemClickListener 和 OnSuggestionListener 都没有在这里工作。这是我的 MainActivity 代码,如果有人可以帮助我,我将非常感激出:
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init View
recyclerView = (RecyclerView) findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar = (MaterialSearchBar) findViewById(R.id.search_bar);
database = new Database(this);
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
loadSuggestList();
materialSearchBar.addTextChangeListener (new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
List<String> suggest = new ArrayList<>();
for (String search : suggestList)
if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
materialSearchBar.setLastSuggestions(suggest);
@Override
public void afterTextChanged(Editable s)
);
materialSearchBar.setOnSearchActionListener (new MaterialSearchBar.OnSearchActionListener()
@Override
public void onSearchStateChanged(boolean enabled)
if (!enabled)
adapter = new SearchAdapter(getBaseContext(), database.getLughats());
recyclerView.setAdapter(adapter);
@Override
public void onSearchConfirmed(CharSequence text)
startSearch(text.toString());
@Override
public void onButtonClicked(int buttonCode)
);
adapter = new SearchAdapter(this, database.getLughats());
recyclerView.setAdapter(adapter);
private void startSearch(String text)
adapter = new SearchAdapter(this, database.getLughatByWort(text));
recyclerView.setAdapter(adapter);
private void loadSuggestList()
suggestList = database.getWorts();
materialSearchBar.setLastSuggestions(suggestList);
```
Let me know if I should post here also my AdapterCode for the ViewHolder.
Thank you for your help in advance!
【问题讨论】:
您希望在点击的项目上显示什么结果?适配器代码在哪里? 我想通过单击或选择要在卡片视图中显示的项目来获得如上所述的结果。目前我必须通过键盘激活搜索,即使用 setOnSearchActionListener 方法。但是我可以通过单击该项目或选择它来激活搜索。 有人知道这个问题的解决方案吗? 【参考方案1】:这是我的适配器代码:
class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
RecyclerItemClick itemClickListener;
public TextView wort, praeteritum, partizip2, artikelGrammatik, uebersetzung1, uebersetzung2;
public ImageButton button1, button2;
public SearchViewHolder(@NonNull View itemView)
super(itemView);
wort = (TextView)itemView.findViewById(R.id.wort);
praeteritum = (TextView)itemView.findViewById(R.id.praeteritum);
partizip2 = (TextView)itemView.findViewById(R.id.partizip2);
artikelGrammatik = (TextView)itemView.findViewById(R.id.artikelGrammatik);
uebersetzung1 = (TextView)itemView.findViewById(R.id.uebersetzung1);
uebersetzung2 = (TextView)itemView.findViewById(R.id.uebersetzung2);
button1 = (ImageButton)itemView.findViewById(R.id.button_id_1);
button2 = (ImageButton) itemView.findViewById(R.id.button_id_2);
itemView.setOnClickListener(this);
@Override
public void onClick(View v)
this.itemClickListener.onItemClickListener(v, getLayoutPosition());
public void setItemClickListener(RecyclerItemClick ic)
this.itemClickListener = ic;
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder>
private Context context;
private List<Lughat> lughats;
public SearchAdapter(Context context, List<Lughat> lughats)
this.context = context;
this.lughats = lughats;
@NonNull
@Override
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View itemView = inflater.inflate(R.layout.layout_item, parent, false);
return new SearchViewHolder(itemView);
@Override
public void onBindViewHolder(@NonNull final SearchViewHolder holder, final int position)
holder.wort.setText(lughats.get(position).getWort());
holder.praeteritum.setText(lughats.get(position).getPraeteritum());
holder.partizip2.setText(lughats.get(position).getPartizip2());
holder.artikelGrammatik.setText(lughats.get(position).getArtikelGrammatik());
holder.uebersetzung1.setText(lughats.get(position).getUebersetzung1());
holder.uebersetzung2.setText(lughats.get(position).getUebersetzung2());
CharSequence praet;
praet = holder.praeteritum.getText();
if (praet.length() == 0)
holder.praeteritum.setVisibility(View.GONE);
holder.button1.setVisibility(View.GONE);
else
holder.praeteritum.setVisibility(View.VISIBLE);
holder.button1.setVisibility(View.VISIBLE);
CharSequence part2;
part2 = holder.partizip2.getText();
if (part2.length() == 0)
holder.partizip2.setVisibility(View.GONE);
else
holder.partizip2.setVisibility(View.VISIBLE);
CharSequence artGr;
artGr = holder.artikelGrammatik.getText();
if (artGr.length() == 0)
holder.artikelGrammatik.setVisibility(View.GONE);
holder.button2.setVisibility(View.GONE);
else
holder.artikelGrammatik.setVisibility(View.VISIBLE);
holder.button2.setVisibility(View.VISIBLE);
CharSequence ueb2;
ueb2 = holder.uebersetzung2.getText();
if (ueb2.length() == 0)
holder.uebersetzung2.setVisibility(View.GONE);
else
holder.uebersetzung2.setVisibility(View.VISIBLE);
holder.button1.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.button2.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.setItemClickListener(new RecyclerItemClick()
@Override
public void onItemClickListener(View v, int position)
Toast.makeText(context, "Begriff", Toast.LENGTH_LONG).show();
);
@Override
public int getItemCount()
return lughats.size();
【讨论】:
【参考方案2】:如果我也使用 OnClickListener 执行此操作,它也不起作用,它不会调用方法 startSearch。
button3.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
CharSequence text = materialSearchBar.getText();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
startSearch(text.toString());
【讨论】:
以上是关于如何在 RecyclerView 的 CardView 中实现对 Item Click 的操作以显示结果的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Recyclerview 中显示图像 - Recyclerview 图像显示错误
如何在 RecyclerView 中使用 ItemAnimator?
当 Recyclerview 在 NestedScrollview 中时,如何避免绑定所有项目?