Nestedscrollview设置最小高度或最大滚动限制

时间:2019-04-09 19:38:36

标签: android-collapsingtoolbarlayout android-nestedscrollview

我正在尝试实现自定义的折叠工具栏。我想要那个,如果用户 滚动列表视图,上面的内容随列表视图向上滚动。它的工作,但我想限制它,以便顶部栏保持特定的高度。

我试图将nestedscrollview与recyclerview一起使用。

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedShit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true">

            <ImageView
                android:id="@+id/top_seller"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@color/colorAccent"
                android:contentDescription="@string/app_name"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher_background"
                android:scaleType="fitXY"/>

            <android.support.v7.widget.RecyclerView
                android:layout_below="@+id/top_seller"
                android:id="@+id/product_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:scrollbars="none" />

        </RelativeLayout>



    </android.support.v4.widget.NestedScrollView>


</LinearLayout>







public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView bestRecyclerView = (RecyclerView)findViewById(R.id.product_list);
        GridLayoutManager mGrid = new GridLayoutManager(this, 2);
        bestRecyclerView.setLayoutManager(mGrid);
        bestRecyclerView.setHasFixedSize(true);
        bestRecyclerView.setNestedScrollingEnabled(false);
        ProductAdapter mAdapter = new ProductAdapter(MainActivity.this, getProductTestData());
        bestRecyclerView.setAdapter(mAdapter);

        final NestedScrollView nestedScrollView = findViewById(R.id.nestedShit);

        final ImageView imageView = findViewById(R.id.top_seller);



        imageView.post(new Runnable() {
            @Override
            public void run() {

                final float bottomImage = imageView.getBottom();

                nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(NestedScrollView nestedScrollView, int i, int i1, int i2, int i3)
                    {
                        float perc = (float) i1 / bottomImage;
                        Log.i(getClass().getName(), "PERCENTAGE: " + perc);
                    }
                });
            }
        });




    }

    private List<ProductObject> getProductTestData() {
        List<ProductObject> featuredProducts = new ArrayList<ProductObject>();
        featuredProducts.add(new ProductObject("Gig Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Mash Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Blue Dot Lapto", "lap1"));
        featuredProducts.add(new ProductObject("Skin Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Dice Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Ingrate Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Mush Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        featuredProducts.add(new ProductObject("Stained Laptop", "lap1"));
        return featuredProducts;
    }

当达到50dp的限制时,Nestedscrollview应该停止滚动。因此,当用户滚动列表时,他总是会看到高度为50dp的顶部栏。

0 个答案:

没有答案