最新消息:文章中包含代码时,请遵守代码高亮规范!

Non-Scrolling RecyclerView inside a ScrollView

Android 贾 松 114浏览 0评论

There may come a point in developing an android app where you may find it necessary to place a RecyclerView inside of a ScrollView along with other elements. Upon doing so you may find that the RecyclerView will try to occupy whatever remaining space is on the screen and will then display it’s own scrollbar to allow the user to scroll through (and thereby be able to see all the child elements). The diagram below shows the “expectation” (left) vs actual result (right).

 

In the diagram above the “expectation” (as seen on the left) is for the RecyclerView to have all it’s child elements to “overflow” without scrolling, however what may actually happen (as seen on the right) is that the RecyclerView shortens itself to fix on the screen and have the child elements scroll within in it. This creates an unwanted “double scrolling”. Additionally the RecyclerView may shorten itself to the point where a small gap/space (as denoted by “S” in the diagram) appears below it .

 

In order to fix this issue, the RecyclerView is placed in a “container”, which in this case was a LinearLayout, and it is resized to allow all the child elements of the RecyclerView to fix inside. The codes below show how this is accomplished.

 

XML

 

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

    <!--其他的元素在这里(注意:这些元素是固定的)-->

    <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content">

        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">

            <!--其他的元素在这里(注意:这些元素不是固定的)-->

            <LinearLayout
                android:id="@+id/recylerViewContainer"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

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

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

 

Java (Activity)

public class YourActivity extends AppCompactActivity {
    //其他代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //其他代码
        setContentView(R.layout.your_activity);
        //其他的代码
        //设计RecyclerView
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewId);
        recyclerView.setAdapter(new yourAdapter(params));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        //找出来包含RecyclerView的LinearView而设计它的高度
        int recyclerItemHeight = 162; //这个整数你要自己定义,我用的公式是 (( ItemHeight * 3 ) - 3 )
        //其中 ItemHeight是Recycler的Adapter里面的Layout的高度
        findViewById(R.id.recyclerViewContainer).getLayoutParams().height = yourAdapter.getItemCount() * recyclerItemHeight;
    }
}

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/21657.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者jason的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!