仿京东分类Recycleview实现两边联动
今天是个好日子 2020-8-18 好久没写博客 该出来练练了 话不多说 上代码
先看看效果
首先上布局
这是主布局
<?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"
android:background="@color/white"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="@color/color_F0F1F5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rcy_title"
android:layout_width="0dp"
android:background="@color/color_F8F8F8"
android:layout_height="match_parent"
android:layout_weight="2" />
<android.support.v7.widget.RecyclerView
android:paddingLeft="12dp"
android:id="@+id/rcy_icon"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_marginBottom="20dp"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
第一个adapter布局
<?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="80dp"
android:gravity="center"
android:id="@+id/tv"
android:background="@color/color_F8F8F8"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/line"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/color_F05557"/>
<TextView
android:layout_centerInParent="true"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物\n特权"
android:textColor="@color/color_333333"
android:textSize="16sp"
/>
</RelativeLayout>
</LinearLayout>
第二个子布局
<?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="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_marginTop="12dp"
android:layout_marginBottom="10dp"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="会员特权"
android:textColor="#ff333333"
android:textSize="18sp"
/>
<TextView
android:layout_marginLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="权益持续更新,整年享用"
android:textColor="#ff999999"
android:textSize="14sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<View
android:layout_width="4dp"
android:layout_height="16dp"
android:background="@color/color_F05557" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="购物特权"
android:textColor="@color/color_333333"
android:textSize="16sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
两个recycleview就OK了
然后请求数据获取到数据分别加入两个adapter
这是主界面代码
/**
*会员权益
*/
public class MembershipFragment extends BaseMainFragmeng {
private RecyclerView rcy_title;
private RecyclerView rcy_icon;
private MembershipAdapter1 adapter1;
private MembershipAdapter2 adapter2;
private LinearLayoutManager manager;
/**
* 是否来自点击
*/
private boolean mIsFromClick = false;
public static MembershipFragment newInstance() {
MembershipFragment fragment = new MembershipFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_membership, container, false);
initView(view);
getData();
getStudyRank();
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
return view;
}
private void getStudyRank() {
}
private void getData() {
getVipRightship(this)
.subscribe(new DataObserver<List<VipBean>>(false) {
@Override
protected void onDataListEmpty() {
onActivityFailure();
}
@Override
protected void onDataNull() {
onActivityFailure();
}
@Override
protected void onError(String errorMsg, String errCode) {
onActivityFailure();
}
@Override
protected void onSuccess(List<VipBean> data) {
onGetDoorGodCategory(data);
}
});
}
private void onGetDoorGodCategory(List<VipBean> data) {
adapter1.setData(data);
adapter2.setData(data);
}
private void onActivityFailure() {
}
private void getVipRightsData(VipBean data) {
}
private void initView(View view) {
Bundle arguments = getArguments();
if (arguments != null) {
}
rcy_title = view.findViewById(R.id.rcy_title);
rcy_icon=view.findViewById(R.id.rcy_icon);
LinearLayoutManager layoutManager1=new LinearLayoutManager(getActivity());
adapter1=new MembershipAdapter1(getActivity());
rcy_title.setLayoutManager(layoutManager1);
rcy_title.setAdapter(adapter1);
adapter2=new MembershipAdapter2(getActivity());
// LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter2);
rcy_icon.setAdapter(adapter2);
manager = new LinearLayoutManager(getActivity());
rcy_icon.setLayoutManager(manager);
// View header = LayoutInflater.from(getActivity()).inflate(R.layout.item_vip_head_ship,null, false);
// mLRecyclerViewAdapter.addHeaderView(header);
// TextView title = header.findViewById(R.id.title);
// title.getPaint().setFakeBoldText(true);
rcy_icon.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (mIsFromClick) {//防止来回调
return;
}
changePosition();
}
});
adapter1.setOnAddClickListener(new MembershipAdapter1.OnAddClickListener() {
@Override
public void onShareClick(int postion) {
adapter1.setmPosition(postion);
mIsFromClick = true;//不走onScrolled,防止来回调
manager.scrollToPositionWithOffset(postion,0);
mIsFromClick = false;//放开
/*
*
* LinearSmoothScroller topScroller = new LinearSmoothScroller(getActivity()) {
@Override
protected int getHorizontalSnapPreference() {
return SNAP_TO_START;//具体见源码注释
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;//具体见源码注释
}
@Override
protected void onStop() {
super.onStop();
//发送一个延时handler
//因为onStop时还有惯性动画
TimeUtil.mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mIsFromClick = false;
}
}, 500);
}
};
topScroller.setTargetPosition(postion);
manager.startSmoothScroll(topScroller);
*/
}
});
}
private void changePosition() {
int firstPosition = manager.findFirstVisibleItemPosition();
if (adapter1.mCheckedPosition != firstPosition) {
adapter1.mCheckedPosition = firstPosition;
adapter1.notifyDataSetChanged();
//此方法无置顶效果
rcy_title.scrollToPosition(adapter1.mCheckedPosition);
}
}
//获取会员权益
public Observable<BaseResponse<List<VipBean>>> getVipRightship(RxFragment fragment) {
RequestBaseOs requestBannerBean = new RequestBaseOs();
requestBannerBean.setOs(1);
return RxHttp.getInstance().getSysteService().getVipRights(requestBannerBean)
.compose(RxUtils.<BaseResponse<List<VipBean>>>switchSchedulers())
.compose(fragment.<BaseResponse<List<VipBean>>>bindToLifecycle());
}
@Subscribe //订阅事件
public void onEventMainThread(MessageEvent event) {
if (event.getAction().equals(EventBusAction.ACTION_REFRSH)) {
getData();
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
下面是adapter
/*
* 会员权益title
* */
public class MembershipAdapter1 extends RecyclerView.Adapter<MembershipAdapter1.ViewHolder> {
private Context context;
private List<VipBean> list = new ArrayList<>();
public int mCheckedPosition = 0;
public MembershipAdapter1(Context context) {
this.context = context;
}
public int getmPosition() {
return mCheckedPosition;
}
public void setmPosition(int mPosition) {
this.mCheckedPosition = mPosition;
}
public void setData(List<VipBean> data) {
if (data != null) {
list.clear();
list.addAll(data);
notifyDataSetChanged();
}
}
public void addData(List<VipBean> data) {
if (data != null) {
list.addAll(data);
notifyItemRangeChanged(0, data.size());
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.itme_membership1, parent, false);
ViewHolder holder = new ViewHolder(inflate);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
String categoryTitle = list.get(position).getCategoryTitle();
if (!TextUtils.isEmpty(categoryTitle)){
StringBuffer buffer=new StringBuffer(categoryTitle);
buffer.insert(2,"\n");
holder.title.setText(buffer.toString());
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemAddClick.onShareClick(position);
notifyDataSetChanged();
}
});
if (position== mCheckedPosition){
holder.title.setTextColor(Color.parseColor("#F05557"));
holder.tv.setBackgroundColor( Color.parseColor("#FFFFFF"));//选中灰色,不选择白色
holder.line.setVisibility(View.VISIBLE);
}else{
holder.tv.setBackgroundColor( Color.parseColor("#F8F8F8"));//选中灰色,不选择白色
holder.line.setVisibility(View.GONE);
holder.title.setTextColor(Color.parseColor("#333333"));
}
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private LinearLayout tv;
private View line;
public ViewHolder(View itemView) {
super(itemView);
tv=itemView.findViewById(R.id.tv);
title = itemView.findViewById(R.id.title);//标题
line=itemView.findViewById(R.id.line);
}
}
public static interface OnAddClickListener {
public void onShareClick(int postion);
}
// add click callback
OnAddClickListener onItemAddClick;
public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
this.onItemAddClick = onItemAddClick;
}
}
public class MembershipAdapter2 extends RecyclerView.Adapter<MembershipAdapter2.ViewHolder> {
private Context context;
private List<VipBean> list = new ArrayList<>();
public MembershipAdapter2(Context context) {
this.context = context;
}
public void setData(List<VipBean> data) {
if (data != null) {
list.clear();
list.addAll(data);
notifyDataSetChanged();
}
}
public void addData(List<VipBean> data) {
if (data != null) {
list.addAll(data);
notifyItemRangeChanged(0, data.size());
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.itme_membership2, parent, false);
ViewHolder holder = new ViewHolder(inflate);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// List<VipBean.VoBean> info = list.get(position).getCategoryTitle();
String categoryTitle = list.get(position).getCategoryTitle();
if (!TextUtils.isEmpty(categoryTitle)) {
holder.title.setText(categoryTitle);
holder.title.getPaint().setFakeBoldText(true);
}
MembershipAdapter3 adapter3 = new MembershipAdapter3(context);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
holder.recycleview.setLayoutManager(gridLayoutManager);
holder.recycleview.setAdapter(adapter3);
holder.recycleview.setNestedScrollingEnabled(false);
adapter3.setData(list.get(position).getVo());
if (position == 0) {
holder.ll.setVisibility(View.VISIBLE);
}else{
holder.ll.setVisibility(View.GONE);
}
holder.title2.getPaint().setFakeBoldText(true);
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView title, title2;
private RecyclerView recycleview;
private LinearLayout ll;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);//标题
title2 = itemView.findViewById(R.id.title2);//标题
recycleview = itemView.findViewById(R.id.recycleview);//icon
ll=itemView.findViewById(R.id.ll);//
}
}
public static interface OnAddClickListener {
public void onShareClick();
}
// add click callback
OnAddClickListener onItemAddClick;
public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
this.onItemAddClick = onItemAddClick;
}
}
这就大公告层了
只要按照步骤来 一切都OK