RecyclerView内的RecyclerView向同一方向滚动

时间:2016-05-18 16:07:25

标签: android android-recyclerview

我有2个回收站视图,然后调用recycleler1和recycler2(紫色背景)。具有黄色背景的正方形是recycler2的元素,其在recycler1的每个元素内部具有实例。我想要实现的是它应该在紫色区域投掷和在它外面的recycler1滚动recycler2的元素。我使用水平滚动(非常容易),但不能垂直。我尝试设置和重置嵌套滚动和行为以及任何但不能使其工作的内容。

enter image description here

代码下面(很多代码):

InnerItem布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="50dp"
         android:layout_width="60dp"
         android:background="#38f"
         android:padding="4dp"
         xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:background="#ffff00"
    android:id="@+id/tag_text_view"
    android:text="Some Content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>

嵌套回收商项目

    <?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:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#cfcfcf"
        >
        <TextView
            android:id="@+id/first_name_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="New Text"
            android:background="#1ad"
            />
        <TextView
            android:id="@+id/last_name_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:gravity="center"
            android:layout_margin="10dp"
            android:textSize="16sp"
            android:padding="10dp"
            android:background="#d65"
            />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/nested_recycler"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#659"
                >
            </android.support.v7.widget.RecyclerView>
    </LinearLayout>

活动

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

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

    </RelativeLayout>

InnerItem

package com.yrs.androidltx.features.adapterviews.nestedrecycler;
public class InnerItem {
    String tag;
    public InnerItem(String tag) {
        this.tag = tag;
    }
}

InnerItem适配器

package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yrs.androidltx.R;
import java.util.List;

public class InnerItemAdapter extends    RecyclerView.Adapter<InnerItemViewHolder> {
    List<InnerItem> innerItemList;

    public InnerItemAdapter(List<InnerItem> innerItemList) {
        this.innerItemList = innerItemList;
    }

    @Override
    public InnerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.inner_item, parent, false);
        InnerItemViewHolder viewHolder = new InnerItemViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(InnerItemViewHolder holder, int position){
        holder.tagTextVIew.setText(innerItemList.get(position).tag);

    }

    @Override
    public int getItemCount() {
        return innerItemList.size();
    }
}

InnerItem ViewHolder

    package com.yrs.androidltx.features.adapterviews.nestedrecycler;

    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.TextView;

    import com.yrs.androidltx.R;


    public class InnerItemViewHolder extends RecyclerView.ViewHolder {

        TextView tagTextVIew;

        public InnerItemViewHolder(View itemView) {
            super(itemView);
            tagTextVIew = (TextView) itemView.findViewById(R.id.tag_text_view);
        }
    }

    package com.yrs.androidltx.features.adapterviews.nestedrecycler;


    public class Person {
        private String firstName;
        private String lastName;


        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }

Person ViewHolder

    package com.yrs.androidltx.features.adapterviews.nestedrecycler;

    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.TextView;

    import com.yrs.androidltx.R;

    public class PersonViewHolder extends RecyclerView.ViewHolder {

        //Keep package visibility
        TextView firstNameTextView;
        TextView lastNameTextView;
        RecyclerView nestedRecyclerView;

        public PersonViewHolder(View itemView) {
            super(itemView);

            firstNameTextView = (TextView) itemView.findViewById(R.id.first_name_textView);
            lastNameTextView = (TextView) itemView.findViewById(R.id.last_name_textView);
            nestedRecyclerView = (RecyclerView) itemView.findViewById(R.id.nested_recycler);
        }
    }

人员适配器

    package com.yrs.androidltx.features.adapterviews.nestedrecycler;

    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.yrs.androidltx.R;

    import java.util.ArrayList;
    import java.util.List;


    public class PersonAdapter extends RecyclerView.Adapter<PersonViewHolder> {

        List<Person> personList;

        public PersonAdapter(List<Person> personList) {
            this.personList = personList;
        }

        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater
                    .from(parent.getContext())
                    .inflate(R.layout.nested_recycler_item, parent, false);
            PersonViewHolder personViewHolder = new PersonViewHolder(view);
            return personViewHolder;
        }

        @Override
        public void onBindViewHolder(PersonViewHolder holder, int position) {
            holder.firstNameTextView.setText(personList.get(position).getFirstName());
            holder.lastNameTextView.setText(personList.get(position).getLastName());
            holder.nestedRecyclerView.setLayoutManager(new GridLayoutManager(holder.nestedRecyclerView.getContext(), 3, GridLayoutManager.VERTICAL, false ));
            holder.nestedRecyclerView.setAdapter(new InnerItemAdapter(initTagList()));
        }

        @Override
        public int getItemCount() {
            return personList.size();
        }

        private List<InnerItem> initTagList() {
            List<InnerItem> tagList = new ArrayList<>();

            tagList.add(new InnerItem("android"));
            tagList.add(new InnerItem("iOS"));
            tagList.add(new InnerItem("windows"));
            tagList.add(new InnerItem("linux"));
            tagList.add(new InnerItem("NetBSD"));
            tagList.add(new InnerItem("NetWare"));

            //repeat few times...
            return  tagList;
        }
    }

主要活动

    package com.yrs.androidltx.features.adapterviews.nestedrecycler;

    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;

    import com.yrs.androidltx.R;
    import com.yrs.androidltx.activities.base.BaseActivity;

    import java.util.ArrayList;
    import java.util.List;

    public class WithNestedRecyclerActivity extends BaseActivity {

        private RecyclerView recyclerView;
        private PersonAdapter personAdapter;
        private RecyclerView.LayoutManager layoutManager;

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

            recyclerView = (RecyclerView) findViewById(R.id.recycler);
            recyclerView.setHasFixedSize(true);

            layoutManager = new LinearLayoutManager(this);
            personAdapter = new PersonAdapter(initDataSet());

            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(personAdapter);

        }

        private List<Person> initDataSet() {
            List<Person> personList = new ArrayList<>();

            personList.add(new Person("Ned","Flanders"));
            personList.add(new Person("Homer","Simpson"));
            personList.add(new Person("Bart","Simpson"));
            personList.add(new Person("Mr","Burns"));

            //... repeat few times

            return personList;
        }
    }

0 个答案:

没有答案