How to center items of a recyclerview?

前端 未结 8 1738
夕颜
夕颜 2020-12-14 14:38

I need to center elements in each row so they will be like in this mockup. I\'ve been searching if there is any layout that works that way without look. items are centered i

相关标签:
8条回答
  • 2020-12-14 15:19

    you can add some layouts dynamically, for example:

    • adapter object can be a horizontal LinearLayout

    1- create a LinearLayout in java code and customize it (gravity,...)

    2- add some icons to linearLayout

    3- add linearLayout to your adapter

    4- repeat 1,2,3


        // : declare new horizontal linearLayout
        ImageView myIcons[nomOfIcons];
        // : add all icons to myIcons
        for(int i=1; i<=nomOfIcons; i++){
            linearLayout.addView(myIcons[i - 1]);
            if(i%numOfIconsInOneHorizontalLinearLayout==0) {
                results.add(linearLayout); // add linearLayout to adapter dataSet
                // : declare new horizontal linearLayout
            }
        }
        if(n%numOfIconsInOneHorizontalLinearLayout!=0) // add last linearLayout if not added in the loop
            results.add(linearLayout);
        mAdapter.notifyDataSetChanged(); // update adapter
    

    0 讨论(0)
  • 2020-12-14 15:23

    Take LinearLayout in your RecyclerView's item row layout then give android:layout_gravity="center" to LinearLayout.

    For each row of images you have to take different LinearLayout.

    Here is example code:

     <LinearLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:orientation="horizontal">
    
                <ImageView
                    android:id="@+id/image1"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:src="@drawable/image1" />
    
                <ImageView
                    android:id="@+id/image2"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:src="@drawable/image2" />
    
               <ImageView
                    android:id="@+id/image3"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:src="@drawable/image3" />
    
      </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题