效果图
主页面布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include layout="@layout/layout_sticky_header_view"/>
</FrameLayout>
activity 代码
public class MainActivity extends AppCompatActivity {
private RelativeLayout targert;
private TextView tvStickyHeaderView;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle);
tvStickyHeaderView = (TextView) findViewById(R.id.tv_sticky_header_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new StickyExampleAdapter(this,getData()));
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
View stickview = recyclerView.findChildViewUnder(0 , 0);
if(stickview!=null&&stickview.getContentDescription()!=null){
if(!TextUtils.equals(tvStickyHeaderView.getText(),String.valueOf(stickview.getContentDescription()))){
tvStickyHeaderView.setText(String.valueOf(stickview.getContentDescription()));
}
}
final View transInfoView= recyclerView.findChildViewUnder(
0, tvStickyHeaderView.getHeight()+1 );
transInfoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("BBB","点击="+transInfoView.getTop());
}
});
if (transInfoView != null && transInfoView.getTag() != null) {
int transViewStatus = (int) transInfoView.getTag();
int top = transInfoView.getTop();
if (transViewStatus == StickyExampleAdapter.HAS_STICKY_VIEW) {
if (top > 0) {
int dealtY = top- tvStickyHeaderView.getMeasuredHeight();
tvStickyHeaderView.setTranslationY(dealtY);
Log.d("BBB","transInfoView++++="+transInfoView.getTop());
} else {
tvStickyHeaderView.setTranslationY(0);
}
} else if (transViewStatus == StickyExampleAdapter.NONE_STICKY_VIEW) {
tvStickyHeaderView.setTranslationY(0);
}
}
}
});
}
public List<StickyBean> getData() {
List<StickyBean> stickyExampleModels = new ArrayList<>();
for (int index = 0; index < 100; index++) {
if (index < 15) {
stickyExampleModels.add(new StickyBean(
"吸顶文本1", "name" + index, "gender" + index));
} else if (index < 25) {
stickyExampleModels.add(new StickyBean(
"吸顶文本2", "name" + index, "gender" + index));
} else if (index < 35) {
stickyExampleModels.add(new StickyBean(
"吸顶文本3", "name" + index, "gender" + index));
} else {
stickyExampleModels.add(new StickyBean(
"吸顶文本4", "name" + index, "gender" + index));
}
}
return stickyExampleModels;
}
}
Adapter代码
public class StickyExampleAdapter extends RecyclerView.Adapter<StickyExampleAdapter.RecyclerViewHolder> {
//第一个吸顶
public static final int FIRST_STICKY_VIEW = 1;
//别的吸顶
public static final int HAS_STICKY_VIEW = 2;
//正常View
public static final int NONE_STICKY_VIEW = 3;
private final LayoutInflater mInflate;
private final Context context;
private final List<StickyBean> datas;
private StickyBean stickyBean;
public StickyExampleAdapter(Context context, List<StickyBean> datas){
mInflate = LayoutInflater.from(context);
this.context = context;
this.datas = datas;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = mInflate.inflate(R.layout.item_ui, parent, false);
return new RecyclerViewHolder(inflate);
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
stickyBean = datas.get(position);
holder.tvName.setText(stickyBean.name);
holder.tvGender.setText(stickyBean.autor);
if (position == 0) {
holder.tvStickyHeader.setVisibility(View.VISIBLE);
holder.tvStickyHeader.setText(stickyBean.sticky);
holder.itemView.setTag(FIRST_STICKY_VIEW);
} else {
if (!TextUtils.equals(stickyBean.sticky, datas.get(position - 1).sticky)) {
holder.tvStickyHeader.setVisibility(View.VISIBLE);
holder.tvStickyHeader.setText(stickyBean.sticky);
holder.itemView.setTag(HAS_STICKY_VIEW);
} else {
holder.tvStickyHeader.setVisibility(View.GONE);
holder.itemView.setTag(NONE_STICKY_VIEW);
}
}
//通过此处设置ContentDescription,作为内容描述,可以通过getContentDescription取出,功效跟setTag差不多。
holder.itemView.setContentDescription(stickyBean.sticky);
}
@Override
public int getItemCount() {
return datas == null ? 0 : datas.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView tvStickyHeader;
RelativeLayout rlContentWrapper;
TextView tvName;
TextView tvGender;
RecyclerViewHolder(View itemView) {
super(itemView);
tvStickyHeader = (TextView) itemView.findViewById(R.id.tv_sticky_header_view);
rlContentWrapper = (RelativeLayout) itemView.findViewById(R.id.rl_content_wrapper);
tvName = (TextView) itemView.findViewById(R.id.named);
tvGender = (TextView) itemView.findViewById(R.id.auto);
}
}
}