当我向下滚动时,两个recyclerview,第一个滚动器也要滚动,怎么样?

时间:2017-08-15 07:45:52

标签: android

我希望有2个网格图像,一个垂直和第二个水平(recyclerview1和recyclerview2),第一个我希望它总是顶部并且左右滚动,但是当我滚动第二个网格视图时,我想要首先要滚动,因为它们在一个卷轴中

示例:

enter image description here

当我在第二个Recyclerview(recyclelerview2)中向下滚动时,我想要滚动第一个回收器。怎么做 ? 结果:

enter image description here

这是我的代码:

主要活动代码:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       // initToolbars();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
        recylerViewLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(recylerViewLayoutManager);
        recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, subjects,images);
        recyclerView.setAdapter(recyclerViewAdapter);


        MainGridRecView = (RecyclerView) findViewById(R.id.maingridrc);
        MainGridRecViewLayoutManager = new GridLayoutManager(context,2);
        MainGridRecView.setLayoutManager(MainGridRecViewLayoutManager);
        MainGridRecViewAdapter = new MainGridRecyclerViewAdapter(MainActivity.this, subjects,images);
        MainGridRecView.setAdapter(MainGridRecViewAdapter);

活动xml:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/liner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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


    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_above="@+id/recyclerview1"
        android:gravity="center"
        android:background="#10bcc9"
        android:textColor="@android:color/white"
        android:text="New Games." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="wrap_content"
        android:layout_height="140dp"
        android:scrollbars="horizontal" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_below="@+id/recyclerview1"
        android:gravity="center"
        android:background="#10bcc9"
        android:textColor="@android:color/white"
        android:text="Used Games." />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/maingridrc"
        android:layout_width="wrap_content"
        android:layout_height="330dp"
        android:layout_marginTop="5dp"
        android:scrollbars="vertical" />

  <!--  <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />-->
</LinearLayout>

适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<Viewholder>{

    String[] SubjectValues;
    String[] imageValues;
    Context mContext;
    View view1;
    ViewHolder viewHolder1;
  // TextView textView;

    public RecyclerViewAdapter(Context context,String[] SubjectValues1, String[] images){

        SubjectValues = SubjectValues1;
        imageValues= images;
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View v){

            super(v);

        }
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType){

        view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items,parent,false);

      //  viewHolder1 = new Viewholder(view1);

        Viewholder vh = new Viewholder(view1, new Viewholder.IMyViewHolderClicks() {
            public void onPotato(View caller) { Log.d("VEGETABLES", "Poh-tah-tos");
                Intent intent = new Intent(mContext,SingleObjectActivity.class);
                mContext.startActivity(intent);
            };
            public void onTomato(ImageView callerImage) {
                Log.d("VEGETABLES", "To-m8-tohs");

                //  v1    CategoryList mDataset = new CategoryList(getOrder(), getId(), item.getUrl(), item.getUserName(), item.getLikes());

                Intent intent = new Intent(mContext,SingleObjectActivity.class);
                mContext.startActivity(intent);
            }
        });

        return vh;
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position){

        holder.txtViewTitle.setText(SubjectValues[position]);
        holder.imgViewIcon.setImageResource(R.drawable.ghost_recon);
    }

    @Override
    public int getItemCount(){

        return SubjectValues.length;
    }
}

recycler xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imagecateg"
        android:layout_width="180dp"
        android:layout_height="160dp"
        android:layout_alignParentTop="true"
        />

    <TextView
        android:id="@+id/txtview"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imagecateg"
        android:paddingLeft="35dp"
        android:textSize="15dp"
        android:textStyle="bold"
        android:background="#f4e04c"
        android:textColor="@color/colorPrimary"
        />
    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

如果要在滚动第二个回收站视图时滚动整个布局 - >您可以将第一个回收站视图添加为第二个回收站视图的第一项,在这种情况下,它们的行为就像一个回收站视图。

我能够实现一个快速的例子,代码并不精彩,但它反映了一般的想法,pushed to github

这是第一个RecyclerView适配器中带有逻辑的main file

答案 1 :(得分:0)

在你的布局文件中做这样的事情。

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


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


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

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


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

这是java代码。

    CustomAdapterHorizenatol horizontalAdapter = new 
    CustomAdapterHorizenatol(horizontalList);
    LinearLayoutManager horizontalLayoutManagaer
            = new LinearLayoutManager(MainActivity.this, 
    LinearLayoutManager.HORIZONTAL, false);
    horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
    horizontal_recycler_view.setAdapter(horizontalAdapter);


    CustomAdapterVertical vertical = new 
    CustomAdapterVertical(horizontalList);
    verticalScrollView.setNestedScrollingEnabled(false);
    verticalScrollView.setHasFixedSize(false);
    LinearLayoutManager verticallLayoutManagaer
            = new LinearLayoutManager(MainActivity.this, 
    LinearLayoutManager.VERTICAL, false);
    verticalScrollView.setLayoutManager(verticallLayoutManagaer);
    verticalScrollView.setAdapter(vertical);