Home > Back-end >  When I click on RecyclerView Item I want the TextView in same activity to display Item's title
When I click on RecyclerView Item I want the TextView in same activity to display Item's title

Time:09-26

RecyclerView UI

As you can see the TextView is on the top, that's where I want to display the title of selected RecyclerView item. Most of the tutorials and blog teach to navigate to next activity when an item is clicked, but that isn't something I am looking for.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Item> itemList;
    private ItemAdapter itemAdapter;
    private SearchView searchView;
    TextView itemname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(savedInstanceState);
        initializeLogic();
    }

    private void initialize(Bundle savedInstanceState) {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.clearFocus();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                filterList(newText);
                return true;
            }
        });
    }

    private void filterList(String newText) {
        List<Item> filteredList = new ArrayList<>();
        for (Item item : itemList) {
            if (item.getItemName().toLowerCase().contains(newText.toLowerCase())){
                filteredList.add(item);
            }
        }

        if (filteredList.isEmpty()) {
            Toast.makeText(this, "No Match", Toast.LENGTH_SHORT).show();
        } else {
            itemAdapter.setFilteredList(filteredList);
        }
    }

    private void initializeLogic() {
        RecyclerView();
    }

    private void RecyclerView () {
        itemList = new ArrayList<>();
        //  Adding Data to Item List
        itemList.add(new Item("Black", R.drawable.android_black));
        itemList.add(new Item("Blue", R.drawable.android_blue));
        itemList.add(new Item("Green", R.drawable.android_green));
        itemList.add(new Item("Pink", R.drawable.android_pink));
        itemList.add(new Item("Red", R.drawable.android_red));
        itemList.add(new Item("Yellow", R.drawable.android_yellow));
        itemList.add(new Item("Black", R.drawable.android_black));
        itemList.add(new Item("Blue", R.drawable.android_blue));
        itemList.add(new Item("Green", R.drawable.android_green));
        itemList.add(new Item("Pink", R.drawable.android_pink));
        itemList.add(new Item("Red", R.drawable.android_red));
        itemList.add(new Item("Yellow", R.drawable.android_yellow));

        itemAdapter = new ItemAdapter(itemList);
        recyclerView.setAdapter(itemAdapter);
    }
}

Item.java

public class Item {
    private String itemName;
    private int itemImage;

    public Item(String itemName, int itemImage) {
        this.itemImage = itemImage;
        this.itemName = itemName;
    }

    public int getItemImage() {
        return itemImage;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
}

ItemAdapter.java

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

    private List<Item> itemList;
    int row_index = -1;

    public ItemAdapter(List<Item> itemList) {
        this.itemList = itemList;
    }

    public void setFilteredList(List<Item> filteredList) {
        this.itemList = filteredList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item, parent, false );
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {

        Item item = itemList.get(position);
        holder.itemNameTv.setText(item.getItemName());
        holder.itemImageView.setImageResource(item.getItemImage());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ////////////////////////////
                Toast.makeText(view.getContext(), "Item is clicked"   item.getItemName(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                row_index = position;
                notifyDataSetChanged();
                Toast.makeText(v.getContext(), "Item is clicked "   item.getItemName(), Toast.LENGTH_SHORT).show();

            }
        });

        if (row_index == position) {
            holder.cardView.setBackgroundColor(Color.LTGRAY);
        } else {
            holder.cardView.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public int getItemCount() {
        if (itemList == null) {
            return 0;
        } else {
            return itemList.size();
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{
        public TextView itemNameTv;
        public ImageView itemImageView;
        public CardView cardView;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);

            cardView = itemView.findViewById(R.id.eachCardView);
            itemNameTv =  itemView.findViewById(R.id.eachItemTextView);
            itemImageView = itemView.findViewById(R.id.eachItemImageView);
        }
    }

}

CodePudding user response:

You can do it via Interface.

Step 1: Create a new interface class as a CustomClick

public interface CustomClick{
    void onClick(String text);
}

Step 2: Declare it in your ItemAdapter.

private List<Item> itemList;
private int row_index = -1;
// create variable
private CustomClick customClick;

// Create Constructor.
public ItemAdapter(List<Item> itemList, CustomClick customClick) {
    this.itemList = itemList;
    this.customClick = customClick;
}

Step 3: Add those lines in your adapter at click.

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           customClick.onClick(String.valueOf(item.getItemName()));
        }
    });

Step 4: Now in your MainActivity

 itemAdapter = new ItemAdapter(itemList, (text)->{
      // here you got text what you clicked in adapter...
      itemname.setText(text);
 });
 recyclerView.setAdapter(itemAdapter);

CodePudding user response:

Add to your adapter class a variable TextView textView.

Change the constructor to

ItemAdapter(List<Item> itemList, TextView textView) {
    this.itemList = itemList;
    this.textView = textView;
}

Instantiate your adapter with

new ItemAdapter(list, myTextView);

In the onClick handler

textView.setText(item.getItemName());

CodePudding user response:

You could hide all kind of other elements like recyclerview, other components you do not want to and show data with your element. Why do you use activities instead of fragments?

  • Related