Horizontal RecyclerView inside CoordinatorLayout

时间:2015-06-15 14:58:58

标签: android android-recyclerview

I'm new To Android development, I'm working with CoordinatorLayout in android lollipop device, The problem is I had a recyclerview with Horizontal scrolling to be collapsed on scrolling a recyclerview with vertical scrolling effect. It working good if I don't scroll the horizontal recyclerview, if I scrolled the horizontal scrollview once its not collapsing.

My Code is 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>

My MainActivity.java is

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);
    }
}

2 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:4)

您还可以通过以下属性在xml文件中进行设置。

android:nestedScrollingEnabled="false"