RecyclerView 中的 item 如何居中问题

一个很简单的Item布局,我只要让它由上而下排列,文字居中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    >

    <TextView
        android:id="@+id/item_0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:textColor="@color/grayDark" 
        android:textSize="@dimen/font_2xbig"
        />

</LinearLayout>

然后代码这样写:很标准的使用方式吧?


recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
		recyclerView.setHasFixedSize(true);
		 recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
//		GridLayoutManager manager = new GridLayoutManager(this, 2);
		recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter...);

问题来了,无论你怎么设置item中各元素的layout_width是match_parent,都无法让文字居中,为什么?

这个问题还得从android的LayoutInflater.from(context).inflate(…)源码下手,

inflater在inflate一个xml时,需要知道parent的类型,才能生成对应的LayoutParams,才可以把xml根节点的attrs(如layout_width)读进去,代码如下:


// android.view.LayoutInflater
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
    
    ...

    // Temp is the root view that was found in the xml
    final View temp = createViewFromTag(root, name, inflaterContext, attrs);

    ViewGroup.LayoutParams params = null;

    if (root != null) {
        if (DEBUG) {
            System.out.println("Creating params from root: " +
                    root);
        }
        // Create layout params that match root, if supplied
        params = root.generateLayoutParams(attrs);
        if (!attachToRoot) {
            // Set the layout params for temp if we are not
            // attaching. (If we are, we use addView, below)
            temp.setLayoutParams(params);
        }
    }

    ...

}

如果parent传进去为null,生成的View的LayoutParams为null,在RecyclerView.addView时,发现LayoutParams为null,则生成默认的LayoutParams,


// android.view.ViewGroup
protected LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

所以无论无论你怎么写,最外层的LinearLayout宽度为WRAP_CONTENT,如果那三个点的宽度为6dp,那么整个View的宽度也为6dp,所以无法居中。

所以要解决照这个问题需要在inflate的时候将parent传进去,如:


result = new DividerHolder(mInflater.inflate(R.layout.divider, parent, false));

同时,最后一个参数设置成false,如果不填该参数则抛异常,说先要removeAllViews()

衍生1,为何ListView加进去就是MATCH_PARENT的?

因为AbsListView重写的generateDefaultLayoutParams方法为


// android.widget.AbsListView
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}

衍生2,为何高度只能用minHeight控制?

同理,layout.xml根节点的attrs属性没被写到LayoutParams中!所以使用minHeight来控制高度的做法是可笑的!你所要做的是在inflate时把parent传进去!

RecyclerView自定义LayoutManager,打造不规则布局

http://blog.csdn.net/qibin0506/article/details/52676670

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/gundumw100/article/details/69999878
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞