使用 FlexboxLayoutManager 将 RecyclerView 项目居中 [英] Centering RecyclerView items with FlexboxLayoutManager
本文介绍了使用 FlexboxLayoutManager 将 RecyclerView 项目居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 FlexboxLayoutManager
将所有 RecyclerView
项目居中?我需要项目像这样居中:
我试过没有成功:
我设置布局管理器的代码:
val layoutManager = FlexboxLayoutManager(this)layoutManager.setFlexWrap(FlexWrap.WRAP)layoutManager.setFlexDirection(FlexDirection.ROW)layoutManager.setJustifyContent(JustifyContent.FLEX_START)layoutManager.setAlignItems(AlignItems.FLEX_START)val 适配器 = TagAdapter(tags)tagRecyclerView.adapter = 适配器tagRecyclerView.layoutManager = layoutManager
(我尝试将 layoutManager.setAlignItems(AlignItems.FLEX_START
) 设置为 layoutManager.setAlignItems(AlignItems.CENTER)
但它没有用...
解决方案
试试这个
<块引用>活动代码
public class TestActivity extends AppCompatActivity {RecyclerView recyclerView;ArrayListarrayList = 新的 ArrayList<>();FlexboxAdapter 适配器;@覆盖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);适配器 = 新的 FlexboxAdapter(this, arrayList);recyclerView.setAdapter(适配器);}私有无效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{上下文上下文;ArrayListarrayList = 新的 ArrayList<>();公共 FlexboxAdapter(上下文上下文,ArrayList arrayList){this.context = 上下文;this.arrayList = arrayList;}@覆盖公共 FlexboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {视图视图 = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);返回新的 ViewHolder(view);}@覆盖public void onBindViewHolder(FlexboxAdapter.ViewHolder holder, int position) {holder.title.setText(arrayList.get(position));}@覆盖公共 int getItemCount() {返回 arrayList.size();}公共类 ViewHolder 扩展 RecyclerView.ViewHolder {文本视图标题;公共 ViewHolder(查看 itemView){超级(项目视图);标题 = itemView.findViewById(R.id.tvTitle);}}}
<块引用>
自定义布局
<文本视图android:id="@+id/tvTitle"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_gravity="中心"机器人:重力=中心"机器人:填充=5dp"机器人:文本=尼罗河"android:textColor="#000"android:textSize="20sp"android:textStyle="粗体"/></LinearLayout>
<块引用>
活动布局
<android.support.v7.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
<块引用>
输出
How do I center all my RecyclerView
items using the FlexboxLayoutManager
?
I need the items to be centered like this:
I tried without success:
My code where I set the layout manager:
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
(I tried to set layoutManager.setAlignItems(AlignItems.FLEX_START
) to layoutManager.setAlignItems(AlignItems.CENTER)
however it did not work...
解决方案
Try this
ACTIVITY CODE
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");
}
}
ADAPTER CODE
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>
OUTPUT
这篇关于使用 FlexboxLayoutManager 将 RecyclerView 项目居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文