使用FlexboxLayoutManager将RecyclerView项目居中

时间:2018-05-15 00:18:06

标签: android android-layout android-recyclerview android-flexboxlayout

如何使用RecyclerView将所有FlexboxLayoutManager项目居中? 我需要像这样对待物品:

Correctly centered items

我尝试没有成功: My attempt, incorrect

我设置布局管理器的代码:

val layoutManager = FlexboxLayoutManager(this)
        layoutManager.setFlexWrap(FlexWrap.WRAP)
        layoutManager.setFlexDirection(FlexDirection.ROW)
        layoutManager.setJustifyContent(JustifyContent.FLEX_START)
        layoutManager.setAlignItems(AlignItems.FLEX_START)

        val adapter = TagAdapter(tags)
        tagRecyclerView.adapter = adapter
        tagRecyclerView.layoutManager = layoutManager

(我试图将layoutManager.setAlignItems(AlignItems.FLEX_START)设置为layoutManager.setAlignItems(AlignItems.CENTER),但它不起作用...

2 个答案:

答案 0 :(得分:8)

试试这个

  

活动代码

public class TestActivity extends AppCompatActivity {


    RecyclerView recyclerView;
    ArrayList<String> arrayList = new ArrayList<>();
    FlexboxAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);


        recyclerView = findViewById(R.id.recyclerView);
        initArray();
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.CENTER);
        layoutManager.setAlignItems(AlignItems.CENTER);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FlexboxAdapter(this, arrayList);
        recyclerView.setAdapter(adapter);

    }

    private void initArray() {

        arrayList.add("Nileshfgfdgfdgfdggfgfgfdgvcb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nileshfgfdgfdgfdggfgcvbcvb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nileshfgfdgfdgfdggfgfdgdfgcvb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nileshfgfdgfdgfdggfgcvb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nileshfgfdgfdgfdggfgdfgdfgcvb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");
        arrayList.add("Nileshfgfdgfdgfdggfgdfgcvb");
        arrayList.add("Nilesh");
        arrayList.add("Nilesh");


    }
}
  

适应代码

public class FlexboxAdapter extends RecyclerView.Adapter<FlexboxAdapter.ViewHolder> {

    Context context;
    ArrayList<String> arrayList = new ArrayList<>();

    public FlexboxAdapter(Context context, ArrayList<String> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public FlexboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FlexboxAdapter.ViewHolder holder, int position) {

        holder.title.setText(arrayList.get(position));


    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tvTitle);
        }
    }
}
  

CUSTOM LAYOUT

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


    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="5dp"
        android:text="Nilesh"
        android:textColor="#000"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>
  

ACTIVITY LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TestActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>
  

<强>输出

enter image description here

答案 1 :(得分:1)

简短回答

您正在使用layoutManager.setAlignItems(AlignItems.FLEX_START)。这导致开始对齐。

您应该使用符合您要求的以下两种对齐方式之一。

layoutManager.setAlignItems(AlignItems.CENTER)

layoutManager.setAlignItems(AlignItems.SPACE_AROUND)

注意:

因为你说过这个

  

@MarcEstrada是的,不要工作

保持项目布局宽度wrap_content。 (所有父母的孩子都应该wrap_content宽。),例如android:layout_width="wrap_content"

如果您使用match_parent宽度,那么您将无法获得居中的项目。