Scrollview嵌套RecyclerView

这个嵌套形式挺常见的,网上有好些解决办法,但我用着不是很好,就自己试了试,有一些注意的地方:
A…Scrollview的唯一子view必须是RelativeLayout,为什么用线性布局不行,我也不是很清楚,可能和布局绘制渲染有关系吧
B…Scrollview必须自定义,不然滑动会黏住,没有惯性滑动,尴尬症会犯的.
C…demo在最下面,这里只是贴了下代码,说一下注意的地方.

1.首先compile下需要的组件,放build.gradle(Module:app),

// Recyclerview
compile 'com.android.support:recyclerview-v7:25.3.1'
//代替findviewbyid的好东西
compile 'com.jakewharton:butterknife:7.0.1'

然后sync new

2.activity_main.xml
这里的一个注意的地方,Scrollview的唯一子view必须是RelativeLayout

<?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="match_parent">
    <!--自定义的scrollview,解决滑动惯性冲突,否则会滑动黏住,没有惯性-->
    <com.zhb.scrollviewrecyclerview.MyScrollview
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--这里必须是RelativeLayout-->
        <RelativeLayout
            android:background="#FFFFFF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:background="@color/colorPrimary"
                android:id="@+id/re"
                android:layout_width="match_parent"
                android:layout_height="200dp">

            </RelativeLayout>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/bb"
                android:layout_below="@id/re"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>
        </RelativeLayout>
    </com.zhb.scrollviewrecyclerview.MyScrollview>
</RelativeLayout>

3.MainActivity
这里butterknife组件的使用自己百度吧,挺好用的,不会就findviewbyid

package com.zhb.scrollviewrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends Activity{
	ArrayList<String> datas = new ArrayList<>();
	@Bind (R.id.re)
	RelativeLayout re;
	@Bind (R.id.bb)
	RecyclerView bb;

	NormalRecyclerViewAdapter normalRecyclerViewAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);
		//设置RecyclerView
		normalRecyclerViewAdapter = new NormalRecyclerViewAdapter(this, datas);
		bb.setLayoutManager(new LinearLayoutManager(this));
		bb.setAdapter(normalRecyclerViewAdapter);
		//设置Item增加、移除动画
		bb.setItemAnimator(new DefaultItemAnimator());

		//模拟加载数据,一般需要从后台获取的
		for(int i = 0; i < 50; i++) {
			datas.add("" + i);
		}
		normalRecyclerViewAdapter.setList(datas);
	}
}

4.适配器NormalRecyclerViewAdapter

package com.zhb.scrollviewrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by zhb on 2016/9/13.
 */

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.NormalTextViewHolder> {
	private final LayoutInflater mLayoutInflater;
	private final Context mContext;
	ArrayList<String> datas = new ArrayList<String>();

	public NormalRecyclerViewAdapter(Context context, ArrayList<String> datas) {
		this.datas = datas;
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
	}

	@Override
	public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
	}

	@Override
	public void onBindViewHolder(NormalTextViewHolder holder, int position) {
		holder.mTextView.setText(position + "");
	}

	public void setList(ArrayList<String> datas){
		this.datas = datas;
		notifyDataSetChanged();
	}

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

	public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
		TextView mTextView;

		NormalTextViewHolder(View view) {
			super(view);
			mTextView = (TextView) view.findViewById(R.id.text_view);

		}
	}
}

5.适配器布局item_text.xml
就简单一个TextView而已,还是贴一下好了。。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
	<TextView
	    android:background="@color/colorAccent"
	    android:textColor="#5f5"
	    android:textSize="15sp"
	    android:id="@+id/text_view"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    />
</LinearLayout>

6.自定义Scrollview,,MyScrollview解决滑动问题

package com.zhb.scrollviewrecyclerview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * Created by zhb on 2017/6/14.
 * 嵌套recyclerview,滑动惯性问题
 */

public class MyScrollview extends ScrollView {
	private int downX;
	private int downY;
	private int mTouchSlop;

	public MyScrollview(Context context) {
		super(context);
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
	}

	public MyScrollview(Context context, AttributeSet attrs) {
		super(context, attrs);
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
	}

	public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent e) {
		int action = e.getAction();
		switch (action) {
			case MotionEvent.ACTION_DOWN:
				downX = (int) e.getRawX();
				downY = (int) e.getRawY();
				break;
			case MotionEvent.ACTION_MOVE:
				int moveY = (int) e.getRawY();
				if (Math.abs(moveY - downY) > mTouchSlop) {
					return true;
				}
		}
		return super.onInterceptTouchEvent(e);
	}
}

7.demo
http://download.csdn.net/detail/u013370255/9871107

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值