头部:
package com.example.colormaterial.adapter;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.colormaterial.R;
import com.example.colormaterial.interfaces.OnClickListenerSelect;
import com.example.colormaterial.net.model.EditModel;
import java.util.List;
/**
* author : jian11058
* time : 5/21/21
* description:
*/
public class TitleSelectAdapter extends BaseQuickAdapter<EditModel,BaseViewHolder> {
private OnClickListenerSelect onClickListenerSelect;
public TitleSelectAdapter( @Nullable List<EditModel> data) {
super(R.layout.item_title_select, data);
}
@Override
protected void convert(@NonNull BaseViewHolder helper, EditModel item) {
int adapterPosition = helper.getAdapterPosition();
TextView tv=helper.getView(R.id.item_detail_title_tv);
ImageView img=helper.getView(R.id.item_detail_title_img);
tv.setText(item.title);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setSelect();
item.isVip=true;
notifyDataSetChanged();
if (onClickListenerSelect!=null){
onClickListenerSelect.select(adapterPosition);
}
}
});
if (item.isVip){
img.setVisibility(View.VISIBLE);
}else {
img.setVisibility(View.INVISIBLE);
}
tv.setSelected(item.isVip);
}
private void setSelect(){
for (EditModel datum : getData()) {
datum.isVip=false;
}
}
public void setSelectPosition(int position){
if (position<1){
position=0;
}
setSelect();
getData().get(position).isVip=true;
notifyDataSetChanged();
}
public void setOnclickListener(OnClickListenerSelect onclickListener){
onClickListenerSelect=onclickListener;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/item_detail_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/detail_title_example"
android:textSize="@dimen/sp_14"
android:textColor="@drawable/select_blue1_grey_color"
android:paddingLeft="@dimen/dp_4"
android:paddingRight="@dimen/dp_4"
android:paddingBottom="@dimen/dp_10"/>
<ImageView
android:id="@+id/item_detail_title_img"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_2"
android:background="@color/blue_1"/>
</LinearLayout>
activity中监听事件做处理:
private void initRecycleview() {
LinearLayoutManager manager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(manager);
adapter=new DetailAdapter();
recyclerView.setAdapter(adapter);
LinearLayoutManager managerTitle = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false);
recyclerView_title.setLayoutManager(managerTitle);
List<EditModel> list=new ArrayList<>();
String[] titles = getResources().getStringArray(R.array.detail_title);
for (int i = 0; i < titles.length; i++) {
EditModel editModel=new EditModel();
editModel.title=titles[i];
if (i==0){
editModel.isVip=true;
}
list.add(editModel);
}
adapter_title=new TitleSelectAdapter(list);
recyclerView_title.setAdapter(adapter_title);
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//关键代码
if (postion != manager_detail.findFirstVisibleItemPosition()) {
postion = manager_detail.findFirstVisibleItemPosition();
adapter_title.setSelectPosition(postion);
}
}
});
adapter_title.setOnclickListener(new OnClickListenerSelect() {
@Override
public void select(int position) {
//这个是跳转到尾部,如果是个长列表,就会让列表的尾部出现在屏幕上
manager.scrollToPosition(postion);
//这个是跳转到首位,0是与底部的距离,默认写0
manager_detail.scrollToPositionWithOffset(position,0);
}
});
}