NestedScrollView复杂布局,可同时包含横竖RecyclerView(淘宝)

内容:NestedScrollView布局是继承了ScrollView,同时又解决了ScrollView嵌套listView/RecyclerView只显示一条的问题。使用NestedScrollView可以进行复杂的布局,上图布局就是NestedScrollView嵌套了一个纵向滑动的RecyclerView、一个TextView和一个横向滑动的RecyclerView。

布局如下:

aitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.leixiansheng.nestedscrollviewtest.MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

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

            <TextView
                android:id="@+id/tv_load"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="加载更多"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:textSize="16sp"
                android:padding="5dp"
                android:textColor="#ffffffff"
                android:background="#4f000000"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/ry_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <Button
        android:id="@+id/btn_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="20dp"
        android:text="Top"/>
</RelativeLayout>

item_person.xml

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

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name"
            android:layout_marginTop="5dp"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher"
            android:layout_toEndOf="@+id/tv_name"
            android:layout_marginLeft="20dp"/>
    </RelativeLayout>
</RelativeLayout>

item_person2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_name"
            android:layout_marginTop="5dp"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher"
            android:layout_toEndOf="@+id/tv_name"
            android:layout_marginLeft="20dp"/>
    </RelativeLayout>
</RelativeLayout>

实现代码:

package com.example.leixiansheng.nestedscrollviewtest;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.leixiansheng.nestedscrollviewtest.adapter.PersonAdapter;
import com.example.leixiansheng.nestedscrollviewtest.bean.PersonBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

	@BindView(R.id.ry_horizontal)
	RecyclerView mRyHorizontal;
	@BindView(R.id.ry_vertical)
	RecyclerView mRyVertical;
	@BindView(R.id.nested_scroll)
	NestedScrollView mNestedScroll;
	@BindView(R.id.btn_top)
	Button mBtnTop;
	@BindView(R.id.tv_load)
	TextView mTvLoad;

	private PersonAdapter hzAdapter;
	private PersonAdapter vtAdapter;
	private List<PersonBean> mHzList = new ArrayList<>();
	private List<PersonBean> mVtList = new ArrayList<>();
	private int mNum = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);
		initData();
		initView();
		initEvent();
	}

	private void initData() {
		for (; mNum < 5; mNum++) {
			mVtList.add(new PersonBean("name:" + mNum, mNum, R.mipmap.ic_launcher));
		}
		for (int i = 0; i < 15; i++) {
			mHzList.add(new PersonBean("name:" + i, i, R.mipmap.ic_launcher));
		}
	}

	private void initView() {
		mRyHorizontal.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
		mRyVertical.setLayoutManager(new GridLayoutManager(this, 2));
		hzAdapter = new PersonAdapter(R.layout.item_person, mHzList);
		vtAdapter = new PersonAdapter(R.layout.item_person2, mVtList);
		mRyVertical.setAdapter(vtAdapter);
		mRyHorizontal.setAdapter(hzAdapter);
		mRyVertical.setNestedScrollingEnabled(false);        //消除滑动不灵敏
	}

	private void initEvent() {
		mBtnTop.setOnClickListener(this);
		mTvLoad.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_top:
				mNestedScroll.scrollTo(0, 0);        //回到顶部
				break;
			case R.id.tv_load:
				int i = mNum + 5;
				for (; mNum < i; mNum++) {
					mVtList.add(new PersonBean("name:" + mNum, mNum, R.mipmap.ic_launcher));
					vtAdapter.notifyDataSetChanged();
				}
		}
	}
}

Adapter

package com.example.leixiansheng.nestedscrollviewtest.adapter;

import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.leixiansheng.nestedscrollviewtest.R;
import com.example.leixiansheng.nestedscrollviewtest.bean.PersonBean;

import java.util.List;

/**
 * Created by Leixiansheng on 2018/7/11.
 */

public class PersonAdapter extends BaseQuickAdapter<PersonBean,BaseViewHolder> {

	public PersonAdapter(int layoutResId, List<PersonBean> data) {
		super(layoutResId, data);
	}

	@Override
	protected void convert(BaseViewHolder holder, final PersonBean item) {
		holder.setText(R.id.tv_name, item.getName())
				.setText(R.id.tv_age, "age:" + item.getAge())
				.setImageResource(R.id.image_view, item.getImageId());
	}
}

引入三方如下:

 // butterknife(快速finViewById)
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    //recyclerview
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.github.baserecycleradapter:library:1.1.0'
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'

BaseRecyclerViewAdapterHelper需要在工程gradle加上下面一句话 maven { url "https://jitpack.io"

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

猜你喜欢

转载自blog.csdn.net/Mr_Leixiansheng/article/details/81030868