我的方法没有像我预期的那样运行,当我点击顶行时,它显示正确,但是当我搜索另一个键时,当我点击它时,它显示相同。我错过了什么?它以某种方式连接到arraylist [0,1,2,3,4,5] 所以,当我点击顶行时,它指向0,它总是正确的, 我该如何解决?
这是我的方法:
private void buildRecyclerView(){
// Create recycler view.
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
// Create an adapter and supply the data to be displayed.
mAdapter = new ExampleAdapter(mExampleList);
// Give the recycler view a default layout manager.
mRecyclerView.setLayoutManager(mLayoutManager);
// Connect the adapter with the recycler view.
mRecyclerView.setAdapter(mAdapter);
//buat hilang dulu diawal
mRecyclerView.setVisibility(View.GONE);
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, mRecyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
ExampleItem item = (ExampleItem) mExampleList.get(position);
if (item.getText1().equals("A12345")){
AlertDialog.Builder builder = new AlertDialog.Builder(menu_utama.this);
builder.setMessage("The text is A12345")
.setNegativeButton("Retry", null)
.create()
.show();
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(menu_utama.this);
builder.setMessage("NOT THE SAME")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
@Override
public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
}
这是数据:
private void createExampleList(){
mExampleList = new ArrayList<>();
mExampleList.add(new ExampleItem(R.drawable.folder_download, "A12345", "Kata-kata nya"));
mExampleList.add(new ExampleItem(R.drawable.folder_download, "B54321", "Kata-kata nya"));
mExampleList.add(new ExampleItem(R.drawable.folder_download, "C23456", "Kata-kata nya "));
mExampleList.add(new ExampleItem(R.drawable.folder_download, "D65432", "Kata-kata nya"));
mExampleList.add(new ExampleItem(R.drawable.folder_download, "E34567", "Kata-kata nya"));
}
这是RecyclerItemClickListener:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
这是我的适配器:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>{
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(View itemView ) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem 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<ExampleItem> filteredList) {
mExampleList = filteredList;
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
当您过滤数据时,点击的项目位置不一定是全局&#39;中的位置。 mExampleList,因此您获得的项目实际上不是您单击的项目。
您需要向ExampleAdapter添加方法:
public ExampleItem getItemForPosition(int position) {
return mExampleList.get(position); //This is the filtered list!
}
在你的onItemClick&#39;回调使用该方法而不是从全局mExampleList中检索数据,如下所示:
ExampleItem item = mAdapter.getItemForPosition(position);