Неправильно обрабатывается нажатие для RecycleView
В попытках сделать поисковик в своём приложении, была придумана схема с EditText и RecyclerView. Для прослушивания нажатий добавил onClickListener к адаптеру, так же добавил атрибут в виде String к каждому новому CardView, что бы для каждого элемента обрабатывать нажатия отдельно.
Код главной активности, адаптера и ItemView ниже(по порядку):
@SuppressLint("ClickableViewAccessibility")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Activity Content as LAYOUT
setContentView(R.layout.app_bottomappbar_in_searchrules);
//Some stuff to write result
createExampleList();
buildRecyclerView();
EditText editText=findViewById(R.id.app_rulessearch_textedit);
editText.addTextChangedListener(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) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
//items onClick's
mAdapter.setOnItemClickListener(new ItemViewAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
if (ItemView.mButtonPosReference.equals("0")) {
Toast.makeText(v.getContext(), "position == 0", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("1")) {
Toast.makeText(v.getContext(), "position == 1", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("2")) {
Toast.makeText(v.getContext(), "position == 2", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("3")) {
Toast.makeText(v.getContext(), "position == 3", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("4")) {
Toast.makeText(v.getContext(), "position == 4", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("5")) {
Toast.makeText(v.getContext(), "position == 5", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("6")) {
Toast.makeText(v.getContext(), "position == 6", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("7")) {
Toast.makeText(v.getContext(), "position == 7", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("8")) {
Toast.makeText(v.getContext(), "position == 8", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemLongClick(int position, View v) {
//TODO
}
});
}
//List of items
private void createExampleList() {
mExampleList = new ArrayList<>();
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_1), getString(R.string.orpho_1), "0"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_2), getString(R.string.orpho_1), "1"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_3), getString(R.string.orpho_1), "2"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_4), getString(R.string.orpho_1), "3"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_5), getString(R.string.orpho_1), "4"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_6), getString(R.string.orpho_1), "5"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_7), getString(R.string.orpho_2), "6"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_8), getString(R.string.orpho_2), "7"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_9), getString(R.string.orpho_2), "8"));
}
private void filter(String text) {
ArrayList<ItemView> filteredList = new ArrayList<>();
for (ItemView item : mExampleList) {
if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
} else if (item.getText2().toLowerCase().contains(text.toLowerCase())){
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ItemViewAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
public class ItemViewAdapter extends RecyclerView.Adapter<ItemViewAdapter.SearchViewHolder> {
private ArrayList<ItemView> mExampleList;
private static ClickListener clickListener;
public static class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public SearchViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
@Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public void setOnItemClickListener(ClickListener clickListener) {
ItemViewAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}
public ItemViewAdapter(ArrayList<ItemView> exampleList) {
mExampleList = exampleList;
}
@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,
parent, false);
SearchViewHolder evh = new SearchViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
ItemView currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public void filterList(ArrayList<ItemView> filteredList) {
mExampleList = filteredList;
notifyDataSetChanged();
}
}
public class ItemView {
private int mImageResource;
private String mText1;
private String mText2;
public static String mButtonPosReference;
public ItemView(int imageResource, String text1, String text2, String pos) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
mButtonPosReference = pos;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
public String getButtonPositon() {
return mButtonPosReference;
}
}
Вопрос, почему, даже когда для каждого CardView был прописано своё значение атрибута, отвечающего за позицию в списке, для всех item`ов присваевается самое старшее значение в списке, в данном случае - "8", в результате, на какой бы CardView я не нажимал, запускается один и тот же Toast.
//Вот здесь я задаю Item`ы
private void createExampleList() {
mExampleList = new ArrayList<>();
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_1), getString(R.string.orpho_1), "0")); //<-- цифра, заключённая в скобки, на конце строки и есть значение того самого атрибута (mButtonPosReference)
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_2), getString(R.string.orpho_1), "1"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_3), getString(R.string.orpho_1), "2"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_4), getString(R.string.orpho_1), "3"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_5), getString(R.string.orpho_1), "4"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_1_6), getString(R.string.orpho_1), "5"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_7), getString(R.string.orpho_2), "6"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_8), getString(R.string.orpho_2), "7"));
mExampleList.add(new ItemView(R.drawable.app_book_colored, getString(R.string.orpho_2_9), getString(R.string.orpho_2), "8"));
}
//Вот здесь я обрабатываю нажатия, относительно значения атрибута mButtonPosReference(pos)
mAdapter.setOnItemClickListener(new ItemViewAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
if (ItemView.mButtonPosReference.equals("0")) {
Toast.makeText(v.getContext(), "position == 0", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("1")) {
Toast.makeText(v.getContext(), "position == 1", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("2")) {
Toast.makeText(v.getContext(), "position == 2", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("3")) {
Toast.makeText(v.getContext(), "position == 3", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("4")) {
Toast.makeText(v.getContext(), "position == 4", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("5")) {
Toast.makeText(v.getContext(), "position == 5", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("6")) {
Toast.makeText(v.getContext(), "position == 6", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("7")) {
Toast.makeText(v.getContext(), "position == 7", Toast.LENGTH_SHORT).show();
} else if (ItemView.mButtonPosReference.equals("8")) {
Toast.makeText(v.getContext(), "position == 8", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemLongClick(int position, View v) {
//TODO
}
});