0

I want to use GridLayout in RecyclerView and set onlickListener to every views to set texts and expand its height. The listener works but when I click one view, a view next to it is also set its text and changed its height.

When I change the column count number from 2 to 4 and then click a view, the click action changes all 4 views height and set their text.

card_item.xml.java

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/top_image_bottom_text_card"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:drawableTopCompat="@drawable/cherry_tomatoes_card_icon" />

TestFragment.java

public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);

        RecyclerView mRecyclerView = rootView.findViewById(R.id.recycler_view);
        CustomAdapter mCustomAdapter = new CustomAdapter();
        mRecyclerView.setAdapter(mCustomAdapter);

        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(mLayoutManager);

        return rootView;
    }

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textViewContainingImageOnTop;

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

            textViewContainingImageOnTop = itemView.findViewById(R.id.top_image_bottom_text_card);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View cardItem = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item, parent, false);

        return new ViewHolder(cardItem);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.textViewContainingImageOnTop.setOnClickListener(getOnClickListener());
        holder.textViewContainingImageOnTop.setText("position: " + position);
    }

    private View.OnClickListener getOnClickListener() {
        return view -> {
            final int additionalHeight = 50;
            int viewHeight = view.getHeight();
            ViewGroup.LayoutParams viewLayoutParams = view.getLayoutParams();
            viewLayoutParams.height = viewHeight + additionalHeight;
            view.setLayoutParams(viewLayoutParams);
        };
    }

    @Override
    public int getItemCount() {
        return 20;
    }
}

I found this but it doesn't help me. I hope someone help me.

UPDATE

I put FrameLayout in card_item.xml like below and then when I clicked a view, it set text to the view only but it expanded itself and a view next to it both.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/top_image_bottom_text_card"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:drawableTopCompat="@drawable/cherry_tomatoes_card_icon" />
</FrameLayout>
vegcale
  • 33
  • 4
  • You say you want to _"set onlickListener to every views to set texts and expand its height"_, but I see where you change the height but not where you change the text. Am I reading your question incorrectly? – Cheticamp Jan 20 '23 at 00:40
  • Thank you for replying. Sorry. I edited the question. I want to set text and it works but it also set text to a view next to a view I clicked. – vegcale Jan 20 '23 at 08:49
  • Maybe[StaggeredGridLayout](https://stackoverflow.com/a/34217143/6287910) will better serve your needs. – Cheticamp Jan 20 '23 at 14:30
  • That's what I wanted to do!! Thank you!! – vegcale Jan 21 '23 at 01:41

0 Answers0