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>