项目
Conflict_project
图片实例
解决几个冲突
- 上面RecyclerView的高度扩宽
- 头部和下面ViewPager的同时滑动
- 头部隐藏后,ViewPager中RecyclerView继续滑动
解决步骤
1. 扩宽头部RecyclerView
布局加入这个,自适应高度
android:layout_width="match_parent"
android:layout_height="wrap_content"
2. 解决冲突
public class NestedScrollLayout extends NestedScrollView {
private View topView; //头部的View
private ViewGroup contentView; // ViewPager中的ReyclerView
private static final String TAG = "NestedScrollLayout"; //TAG
public NestedScrollLayout(Context context) {
this(context, null);
init();
}
public NestedScrollLayout(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
init();
}
public NestedScrollLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
init();
}
public NestedScrollLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
init();
}
private FlingHelper mFlingHelper; // veltociy和distance的转换
int totalDy = 0;
/**
* 用于判断RecyclerView是否在fling
*/
boolean isStartFling = false;
/**
* 记录当前滑动的y轴加速度
*/
private int velocityY = 0;
private void init() {
mFlingHelper = new FlingHelper(getContext());
setOnScrollChangeListener(new View.OnScrollChangeListener() { //监听自己(NestedScrollView)滑动
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (isStartFling) {
totalDy = 0;
isStartFling = false;
}
if (scrollY == 0) { // 到达顶部的时候
Log.i(TAG, "TOP SCROLL");
// refreshLayout.setEnabled(true);
}
// topView完全消失了,该子View处理了
if (scrollY == (getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
dispatchChildFling();
}
//在RecyclerView fling情况下,记录当前RecyclerView在y轴的偏移
totalDy += scrollY - oldScrollY;
}
});
}
private void dispatchChildFling() {
if (velocityY != 0) { // 滑动的速度,和distance可以相互转换
// 转换成距离
Double splineFlingDistance = mFlingHelper.getSplineFlingDistance(velocityY);
if (splineFlingDistance > totalDy) {
// 转换成velocityY
childFling(mFlingHelper.getVelocityByDistance(splineFlingDistance - Double.valueOf(totalDy)));
}
}
// 处理完之后,恢复默认值
totalDy = 0;
velocityY = 0;
}
private void childFling(int velY) {
RecyclerView childRecyclerView = getChildRecyclerView(contentView); // 找到子View(RecyclerView)
if (childRecyclerView != null) {
childRecyclerView.fling(0, velY); //fling事件传出去
}
}
@Override
public void fling(int velocityY) { // 自己的fling
super.fling(velocityY);
if (velocityY 0 && getScrollY()
工具类
FlingHelper
public class FlingHelper {
private static float DECELERATION_RATE = ((float) (Math.log(0.78d) / Math.log(0.9d)));
private static float mFlingFriction = ViewConfiguration.getScrollFriction();
private static float mPhysicalCoeff;
public FlingHelper(Context context) {
mPhysicalCoeff = context.getResources().getDisplayMetrics().density * 160.0f * 386.0878f * 0.84f;
}
private double getSplineDeceleration(int i) {
return Math.log((double) ((0.35f * ((float) Math.abs(i))) / (mFlingFriction * mPhysicalCoeff)));
}
private double getSplineDecelerationByDistance(double d) {
return ((((double) DECELERATION_RATE) - 1.0d) * Math.log(d / ((double) (mFlingFriction * mPhysicalCoeff)))) / ((double) DECELERATION_RATE);
}
public double getSplineFlingDistance(int i) {
return Math.exp(getSplineDeceleration(i) * (((double) DECELERATION_RATE) / (((double) DECELERATION_RATE) - 1.0d))) * ((double) (mFlingFriction * mPhysicalCoeff));
}
public int getVelocityByDistance(double d) {
return Math.abs((int) (((Math.exp(getSplineDecelerationByDistance(d)) * ((double) mFlingFriction)) * ((double) mPhysicalCoeff)) / 0.3499999940395355d));
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
合肥市是安徽省的省会城市,区域面积较大,现辖庐阳区、瑶海区、蜀山区、包河区、滨湖新区、经济技术开发区、高新技术产业开发区、新站综合开发试验区、肥东县和肥西县等10个行政区划。 以下是各个区的发展特点: 庐阳区:庐阳区是合肥市的历史文化区,也是合肥市的政治、文化…