【问题标题】:Horizontal RecyclerView inside CoordinatorLayoutCoordinatorLayout 内的水平 RecyclerView
【发布时间】:2015-06-15 14:58:57
【问题描述】:

我是 Android 开发新手,我正在使用 android 棒棒糖设备中的 CoordinatorLayout,问题是 我有一个水平滚动的 recyclerview,在滚动具有垂直滚动效果的 recyclerview 时折叠。 如果我不滚动水平回收器视图,如果我在水平滚动视图不折叠时滚动它,它工作得很好。

我的代码是 activity_main.xml

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">    

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recommendedFoodItemsHolder_part1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_collapseMode="parallax" />

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recommendedFoodItemsHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

我的 MainActivity.java 是

package in.venkatesh.www.testapp2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

    private RecyclerView mSimpleRecyclerView, recommendedRecyclerView;
    //    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager,recommendedLayoutManager;

    private String[] mRecyclerViewValues =
            new String[]{"Apple Pie", "Banana Bread", "Cupcake", "Donut", "Eclair", "Froyo",
                    "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat",
                    "Lollipop", "M preview"};
    private String[] mRecommendedFoodItems = new String[]{"Chicken Biryani",     "Mutton Biryani", "Veg Biryani","Veg Fried Rice"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Actionbar
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Link the views
        mSimpleRecyclerView = (RecyclerView) findViewById(R.id.recommendedFoodItemsHolder);
        recommendedRecyclerView = (RecyclerView) findViewById(R.id.recommendedFoodItemsHolder_part1);

        // Use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mSimpleRecyclerView.setHasFixedSize(true);

        // Use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        recommendedLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

        mSimpleRecyclerView.setLayoutManager(mLayoutManager);
        recommendedRecyclerView.setLayoutManager(recommendedLayoutManager);


        SimpleRecyclerViewAdapter mSimpleRecyclerViewAdapter = new     SimpleRecyclerViewAdapter(mRecyclerViewValues);
        SimpleRecyclerViewAdapter recommendedFoodRecyclerViewAdapter = new SimpleRecyclerViewAdapter(mRecommendedFoodItems);
        mSimpleRecyclerView.setAdapter(mSimpleRecyclerViewAdapter);                    recommendedRecyclerView.setAdapter(recommendedFoodRecyclerViewAdapter);
    }
}

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    尝试使用 recycler.setNestedScrollingEnabled(false);这将解决您的问题

    【讨论】:

    • 天啊!!!非常感谢人!我在 RecyclerView 中有一个 RecyclerView,这解决了我的问题(工具栏做了奇怪的事情)。我在内部(水平)RecyclerView 中调用了setNestedScrollingEnabled(false)
    • @Cortez 你能告诉我原因吗?我很想知道
    • 使用这个会排除recyclerview的所有功能。
    【解决方案2】:

    您也可以通过以下属性在xml文件中设置它。

    android:nestedScrollingEnabled="false"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-15
      • 2019-01-20
      • 2015-12-03
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多