N
N
Nikita2020-01-23 13:17:01
Android
Nikita, 2020-01-23 13:17:01

ConstraintLayout in LinearLayout in RecyclerView. group. Why is that?

All of the following is part of a pet project, no users were harmed.
item in RecyclerView is LinearLayout with two identical nested (include) ConstraintLayout with more views (let's call them sub_item1 and sub_item2).
In an attempt to get by without the ExpandableLayout, I took advantage of the Group and visibility settings.
The problem is that changing the visibility of the Group in sub_item1 "twitches" and sub_item2
(see gif). By the way, if you change the visibility of sub_item2 - sub_item1 does not move.
5e296ff8c6b47336753550.gif
Having decided that the problem is in the same id Group within one item, it was decided to merge both sub_items into one and designate each group with its own id.
But the problem remained.
What could be the reason?

Code here

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_2);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        lInf = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        
        //RecyclerView
        hSV = findViewById(R.id.hSV);

        DataAdapter adapter = new DataAdapter(this);
        hSV.setAdapter(adapter);
    }

    class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

        private LayoutInflater layInf2;
        private Context mCont;

        DataAdapter(Context context) {
            this.layInf2 = LayoutInflater.from(context);
            mCont = context;
        }

        @NonNull
        @Override
        public DataAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = layInf2.inflate(R.layout.item_main, parent,false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull DataAdapter.ViewHolder holder, int position) {
            holder.onBind(holder.ivArr);
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {
            private ImageView ivArr;
            private Group gr1;
            private boolean isExpand;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                //До слияния sub_item1 и sub_item2 в один
                ivArr = itemView.findViewById(R.id.sub_item1).findViewById(R.id.IV_arrow);

                gr1 = itemView.findViewById(R.id.sub_item1).findViewById(R.id.group1);

                isExpand = gr1.getVisibility() == View.VISIBLE;
            }

            public void onBind (ImageView iv) {
                iv.setOnClickListener(v -> {
                    if(isExpand) {
                        gr1.setVisibility(View.GONE);
                    } else {
                        gr1.setVisibility(View.VISIBLE);
                    }
                    isExpand = !isExpand;
                });
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-01-23
@Imustrunaway

It's not code, it's the markup that needs to be looked at. you have the position of the second item depends on the position of the first, when you fold the first item, it becomes narrower and its right edge shifts to the left, and the second item behind it pulls up. If you wanted it to be different, then you need to make sure that the sizes of the items are fixed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question