简体   繁体   中英

How to close recycler view holder after clicking an item?

I'm trying to close the view holder (the list of items which is shown after searching in edit text). But I can't find the right function or way to do it! Any help?

I have a search adapter which I tried overriding onBindViewHolder and try to close the recycler view there. But i'm struggling with finding the right function or way..

@Override
    public void onBindViewHolder(final SearchViewHolder holder, int position) {

        holder.full_name.setText(fullNameList.get(position));
        holder.full_name.setBackgroundColor(selectedPos == position ? Color.GREEN : Color.LTGRAY);

        holder.full_name.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(final View v) {


                if (holder.getAdapterPosition() == RecyclerView.NO_POSITION) return;
                notifyItemChanged(selectedPos);
                selectedPos = holder.getLayoutPosition();
                notifyItemChanged(selectedPos);
                Toast.makeText(context, ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); //Here I get the text string


                // close keyboard on click

                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }



            }
        });
    }

Your question is not clear. You can maybe clear the fullNameList and do a notifychange?

@Override
    public void onClick(final View v) {
        if (holder.getAdapterPosition() == RecyclerView.NO_POSITION) return;
        notifyItemChanged(selectedPos);
        selectedPos = holder.getLayoutPosition();
        notifyItemChanged(selectedPos);
        Toast.makeText(context, ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); //Here I get the text string

        //Clears the list and notify the adapter
        fullNameList.clear();
        notifyDataSetChanged();

        v.clearFocus();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

Although an AutoCompleteTextView should do the job just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM