如何使两个回收器视图垂直滚动。其中一个是水平的而另一个是垂直的

时间:2017-03-09 20:12:54

标签: android android-recyclerview

此布局包含两个回收站视图,其中一个是水平的,另一个是垂直的。现在,当我滚动垂直回收器视图时,整个屏幕应垂直滚动。怎么做?

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_horizontal"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:background="#FFF" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/recycler_home"
        android:clipToPadding="false"
        android:scrollbars="vertical"
        android:background="#FFF">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将垂直回收器视图的第一行设为水平滚动RecyclerView。

这可以通过添加基于位置的不同ViewType来完成。

检查此骨架适配器。

public class Adapter_HeaderView extends RecyclerView.Adapter<Adapter_HeaderView.ViewHolder> {
    private final int VIEW_TYPE_HEADER = 1, VIEW_TYPE_CONTENT = 2;
    private Context context;
    private List<Object> list;

    public Adapter_HeaderView(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int layoutID;
        switch (viewType) {
            case VIEW_TYPE_CONTENT:
                // content row layout
                layoutID = R.layout.row_list;
                break;
            case VIEW_TYPE_HEADER:
                // header row layout
                layoutID = R.layout.row_list_header;
                break;
            default:
                layoutID = R.layout.row_list;
                break;

        }
        return new ViewHolder(LayoutInflater.from(context).inflate(layoutID, parent, false));
    }
// override the getItemViewType to return position based on position
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_HEADER;
        } else
            return VIEW_TYPE_CONTENT;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (getItemViewType(position) != VIEW_TYPE_HEADER) {
          // load content layout here
          // access all items of list as list.get(position-1); as dummy item added as the first element
        } else {
        // load header layout components here. 
          }
    }

    @Override
    public int getItemCount() {
        // add a dummy item in item count which will be the recycler view header. 
        return list.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

在此处,将row_list_header设为recyclelerview。