How to center items in RecyclerView

A very simple Item layout, I just need to arrange it from top to bottom with the text centered
<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>


Then the code is written like this: Very standard usage, right?
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...);


The problem is, no matter how you set the layout_width of each element in the item to match_parent, the text cannot be centered, why?

This problem has to start with android's LayoutInflater.from(context).inflate(...) source code.

When inflater inflates an xml, it needs to know the type of parent to generate the corresponding LayoutParams, and then it can convert the attrs of the xml root node (such as layout_width) read in, the code is as follows:
// 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);
        }
    }

    ...

}

If the parent passed in is null, the LayoutParams of the generated View is null. When RecyclerView.addView finds that the LayoutParams is null, the default LayoutParams will be generated.
// android.view.ViewGroup
protected LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

So no matter how you write it, the width of the outermost LinearLayout is WRAP_CONTENT. If the width of the three points is 6dp, then the width of the entire View is also 6dp, so it cannot be centered.

So to solve this problem, you need to pass the parent in when inflate, such as:
result = new DividerHolder(mInflater.inflate(R.layout.divider, parent, false));

At the same time, the last parameter is set to false. If this parameter is not filled in, an exception will be thrown, saying that removeAllViews() must first


derive 1. Why is the ListView added in MATCH_PARENT?
Because the generateDefaultLayoutParams method overridden by AbsListView is
// android.widget.AbsListView
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}

Derivative 2, why the height can only be controlled with minHeight?
Similarly, the attrs attribute of the root node of layout.xml is not written to LayoutParams! So using minHeight to control the height is ridiculous! All you have to do is pass parent in when inflate!

RecyclerView customizes LayoutManager to create irregular layout
http://blog.csdn.net/qibin0506/article/details/52676670

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326840640&siteId=291194637