RecyclerView 23.2.x height = 0dp calls onBind calls all elements and scrollToPosition does not work

UPDATE I just updated the support library to 23.3.0, and this problem no longer occurs. I assume this was a version error 23.2.X. Well, this is not the first time I have spent hours on a strange problem, and then discovered that it was an Android SDK error. END UPDATE

In the next example I'm setting layout_height="0dp"and layout_weight="1"in view resayzera to fill the remaining space of the linear layout. It seems to me that layout_height is not needed because layout_weight is set, but the exception of this attribute is considered an error, and the examples that I found in other questions about stack overflow suggest using "0dp".

The strange thing is that if I set layout_height = "0dp", onBindViewHolder () is called for all elements at once. That is, the debug output is similar to

onBindViewHolder(0)
...
onBindViewHolder(99)

If I set layout_height = "1dp", onBindViewHolder () is called only for the first few items before scrolling. That is, the debug output is similar to

onBindViewHolder(0)
...
onBindViewHolder(8)

This does not happen with RecyclerView version 23.1.1, but it does happen with versions 23.2.0 and 23.2.1 (for example, com.android.support:recyclerview-v7:23.2.1), and software scrolling does not work either. Why is this so? Am I supposed to use "1dp" in this case?

All source codes are listed below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@android:color/holo_green_dark"
    />

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="#ff111111"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:orientation="vertical">

    <FrameLayout
        android:background="@android:color/holo_orange_light"
        android:layout_width="match_parent"
        android:layout_height="50dp">

    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mylist"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_weight="1"
        >

    </android.support.v7.widget.RecyclerView>

    <FrameLayout
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    </FrameLayout>
</LinearLayout>

my_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:clickable="true"
          android:focusable="true"
          android:id="@+id/itemRect"
          android:background="?android:attr/selectableItemBackground"
          android:layout_height="wrap_content">

<ImageView
    android:src="@android:drawable/ic_menu_camera"
    android:layout_width="48sp"
    android:layout_height="48sp"/>
<TextView
    tools:text="Hello world"
    android:id="@+id/name"
    android:gravity="center_vertical"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="48sp"/>

MainActivity.java

public class MainActivity extends AppCompatActivity
{
String TAG = this.getClass().getName();
RecyclerView mylist;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mylist = (RecyclerView)findViewById(R.id.mylist);

    ArrayList<String> source = new ArrayList<>();
    for(int i =0; i< 100; i++)
    {
        source.add("List item " + i);
    }

    LinearLayoutManager lay = new LinearLayoutManager(mylist.getContext());
    mylist.setLayoutManager(lay);
    MyAdapter adap = new MyAdapter(source);
    mylist.setAdapter(adap);
}


class MyViewHolder extends RecyclerView.ViewHolder
{
    TextView name;
    public MyViewHolder(View itemView)
    {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
    }

    public void bind(String item)
    {
        name.setText(item);
    }
}
class MyAdapter extends RecyclerView.Adapter<MyViewHolder>
{
    List<String> items;
    public MyAdapter(List<String> items)
    {
        this.items = items;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_item, null);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        Log.d(TAG, "onBindViewHolder(" + position + ")");
        holder.bind(items.get(position));
    }

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

enter image description here

+4
source share

All Articles