首先是布局
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Test">
<com.example.caicheng.test.FScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fillViewport="true">
<RelativeLayout
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.recker.flybanner.FlyBanner
android:id="@+id/mFlyBanner"
android:layout_width="match_parent"
android:layout_height="180dp"/>
<android.support.v7.widget.RecyclerView
android:layout_below="@+id/mFlyBanner"
android:layout_weight="1"
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
</com.example.caicheng.test.FScrollView>
</LinearLayout>
自定义FScrollView类
public class FScrollView extends ScrollView {
private float mLastXIntercept = 0f;
private float mLastYIntercept = 0f;
public FScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
float x = ev.getX();
float y = ev.getY();
int action = ev.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN: {
intercepted = false;
//初始化mActivePointerId
super.onInterceptTouchEvent(ev);
break;
}
case MotionEvent.ACTION_MOVE: {
//横坐标位移增量
float deltaX = x - mLastXIntercept;
//纵坐标位移增量
float deltaY = y - mLastYIntercept;
if (Math.abs(deltaX) < Math.abs(deltaY)) {
intercepted = true;
} else {
intercepted = false;
}
break;
}
case MotionEvent.ACTION_UP: {
intercepted = false;
break;
}
}
mLastXIntercept = x;
mLastYIntercept = y;
return intercepted;
}
}
主要代码
public class Test extends AppCompatActivity {
private List<Bean> mList;
private List<Integer> mBannerList;
private RecyclerView mRecyclerView;
private FlyBanner mFlyBanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initData();
initAdapter();
}
private void initView() {
mList=new ArrayList<>();
mBannerList=new ArrayList<>();
mRecyclerView = findViewById(R.id.mRecyclerView);
mFlyBanner = findViewById(R.id.mFlyBanner);
}
private void initData() {
for(int i=0;i<=100;i++){
mList.add(new Bean("测试"+i));
}
mBannerList.add(R.mipmap.ic_launcher);
mBannerList.add(R.mipmap.ic_launcher);
mBannerList.add(R.mipmap.ic_launcher);
mFlyBanner.setImages(mBannerList);
}
private void initAdapter() {
mRecyclerView.setHasFixedSize(true);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,mList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
};
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setNestedScrollingEnabled(false); //禁用自身RecyclerView滚动
mRecyclerView.setAdapter(adapter);
}
}