Recycler view item fill up entire recycler view he

2019-01-31 17:11发布

Previously, I'm using the following old support libraries "23.1.1".

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:preference-v7:23.1.1'
compile 'com.android.support:preference-v14:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

It works pretty well. Here's how my RecyclerView looks like

enter image description here

Now, I wish to migrate to "23.2.1", due to some bug fixes done.

compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:preference-v7:23.2.1'
compile 'com.android.support:preference-v14:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'

However, suddenly, all my RecyclerView items, seem to fill up the RecyclerView entire height.

enter image description here

enter image description here

Here's the code snippet of my layout file : https://gist.github.com/yccheok/241a0d38d56305a1be24d09b54eb1600

What really puzzle me is that, although I'm using "wrap_content" in my recycler view item layout, it doesn't work as expected.

I don't use any custom layout manager for my RecyclerView.

From http://developer.android.com/tools/support-library/index.html, I realize 23.2.1 makes quite a number of changes on RecyclerView this time.

  • Fixed bugs related to various measure-spec methods. (Issue 201856)
  • Reduced the lockdown period in which RecyclerView does not allow adapter changes while calculating a layout or scroll. (Issue 202046)
  • Fixed a crash when calling notifyItemChanged() on an item that is out of view. (Issue 202136)
  • Fixed a crash that occurs when RecyclerView.LayoutManager adds and removes a view in the same measurement pass. (Issue 193958)

What I suspect most is https://code.google.com/p/android/issues/detail?id=201856 , as it involves changing various measure-spec methods

So far, I try to reproduce the problem with a simple RecyclerView project, with 23.2.1 but failed! It doesn't have "item fills up the RecyclerView entire height" problem. My guess is that, my simple project doesn't simulate the complex layout structure of my production project. My production project is having the following layout

<Activity>
    <Fragment>
        <View Pager>
            <Fragment>
                <RecyclerView />
            </Fragment>
        </View Pager>
    </Fragment>
</Activity>

After debugging for few hours, I'm still cannot find root cause for such problem, any hint?

Thanks.

What I had tried

I had tried to change RecyclerView

from

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

to

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

It looks good initially. However, when you perform scrolling, thing doesn't work as expected : https://www.youtube.com/watch?v=U2EChFn6WkI

UPDATE: I finally figure out the root cause

Is mistake at my side! Since I need to have different margin for the last row item, here's my adapter code.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final List<TransactionSummary> transactionSummaries = buyArray.transactionSummaries;

    if (position == transactionSummaries.size() - 1) {
        holder.itemView.setLayoutParams(lastLayoutParams);
    } else {
        holder.itemView.setLayoutParams(normalLayoutParams);
    }

Unfortunately, lastLayoutParams and normalLayoutParams is being initialized as

    normalLayoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
    );

    lastLayoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
    );

Using LinearLayout.LayoutParams.WRAP_CONTENT solve the problem.

7条回答
成全新的幸福
2楼-- · 2019-01-31 17:28

Update

It appears that you are updating the LayoutParam for your View in your Adapter.

It is possible to tell this because your UI appears absolutely fine until you begin scrolling. This means that your XML is correct as it is defined in your XML layout file.

The fact that it changes after scrolling begins, means there is a logic error in your onBindViewHolder implementation. That is why the error appears when you scroll down, and then the error sticks when you scroll back up.

Old answer

Your issue is that your divider has gone rogue:

<View
    android:layout_width="1px"
    android:layout_height="match_parent"
    android:background="?attr/buyPortfolioSeperatorBackground"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp" />

For testing purposes, set it to:

<View
    android:layout_width="1px"
    android:layout_height="30dp"
    android:background="?attr/buyPortfolioSeperatorBackground"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp" />

Make sure you change both of them!

查看更多
看我几分像从前
3楼-- · 2019-01-31 17:30

The height of recycle view must be "wrap_content" only. The recycle view will handle height if the size of cell increases.

buy_portfolio_fragment.xml

 <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/buyPortfolioListViewBackground"
        android:requiresFadingEdge="none"
        android:scrollbars="vertical"
        android:paddingTop="@dimen/default_tab_layout_height"
        android:clipToPadding="false" />
查看更多
Root(大扎)
4楼-- · 2019-01-31 17:36

To fix this bug row_layout should have height fixed or wrap_content! I also had this problem and just realized that the height of row_layout was match_parent.

查看更多
来,给爷笑一个
5楼-- · 2019-01-31 17:37

I had a similar problem. It endend up being that the recycler was not the problem. Check that your CardView item measurements translate to something like this:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
/>

If you're not using CardView, ensure that the element you use in your Adapter for the view has android:layout_height="wrap_content" and not match_parent.

If that fails to work, you can add another attribute setting the minHeight or maxHeight for the view item.

查看更多
叼着烟拽天下
6楼-- · 2019-01-31 17:42

Just make row_layout height to wrap_content so it will take only row actual height space to all the items.

查看更多
别忘想泡老子
7楼-- · 2019-01-31 17:49

I believe this is the problematic line:

<View   android:layout_width="1px"
            android:layout_height="match_parent"    <!--change this to wrap_content-->
            android:background="?attr/buyPortfolioSeperatorBackground"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp" />

There are 2 places in your layout item that has layout_height="match_parent". You should change them both to wrap_content.

查看更多
登录 后发表回答