在开发中有时会遇到需要在fragment使用viewPager滑动,使用tablayout + viewPager也好,RecyclerView + ViewPager也好,达到目的就行。
我这里使用的是RecyclerView + ViewPager,达到在fragment中滑动浏览页面新闻,开撸
newFragment布局
准备CenterLayoutManager,这是RecyclerView在点击item时会滑动到中心位置
public class CenterLayoutManager extends LinearLayoutManager {
static int lastPosition = 0;
static int targetPosition = 0;
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastPosition, int position) {
CenterLayoutManager.lastPosition = lastPosition;
CenterLayoutManager.targetPosition = position;
smoothScrollToPosition(recyclerView, state, position);
}
public static class CenterSmoothScroller extends LinearSmoothScroller {
private static float duration = 200f;
public CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
float newDuration = duration / (Math.abs(targetPosition - lastPosition));
return newDuration / displayMetrics.densityDpi;
}
@Override
protected int calculateTimeForScrolling(int dx) {
return super.calculateTimeForScrolling(dx);
}
}
}
WrapLinearLayoutManager,这个具体是RecyclerView 界面在展示的时候出现下标越界的问题,但具体真的没什么用,RecyclerView出现下标越界的问题时主要是list.clear()后直接list.addAll,暂时找到的解决方法是list.clear之后也需要adapternotifyDataSetChanged
public class WrapLinearLayoutManager extends LinearLayoutManager {
public WrapLinearLayoutManager(Context context) {
super(context);
}
public WrapLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public WrapLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
再准备一个继承View的类,作为添加到viewPager中的内容
public class HomeNewPagerDataView extends LinearLayout {
private Context context;
private LayoutInflater inflater;
private View view;
private RecyclerView homePagerRecView;
private List
写HomeNewPagerAdapter
public class HomeNewPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
private List viewList;
public HomeNewPagerAdapter(List viewList) {
this.viewList = viewList;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override //从当前container中删除指定位置(position)的View;
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(viewList.get(position));
}
@NonNull
@Override //第一:将当前视图添加到container中,第二:返回当前View
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
}
newFragment页面代码,关于Base可以参考以前写的,毕竟使用dataBinding真的很方便
public class HomeFragmentNew extends BaseDataBindingFragment{ private List
基本撸完,在使用中学习,并记录下来,大家一起进步