1.布局 layout >cm_item_classify.xml
android:id="@+id/rlay_index"
style="@style/ItemThird"
android:gravity="center_vertical">
android:id="@+id/tv_index"
android:layout_width="match_parent"
android:layout_height="28dp"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/margin_18"
android:text="索引"
android:textColor="?attr/txt_second"
android:textSize="13dp" />
2.布局 layout>mt_acti_test_history
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/four">
android:id="@+id/rlayt_history"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/rv_history"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:id="@+id/fly_index"
android:layout_width="match_parent"
android:layout_height="wrap_content">
3. Activity 的initView方法
//这个是引用布局
final TextView tvIndexView = (TextView) findViewById(R.id.tv_index);
final FrameLayout flyIndex=(FrameLayout)findViewById(R.id.fly_index);
//此处RecycleView
rvHistory.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//以下为悬停索引赋值逻辑
View indexTitle = recyclerView.findChildViewUnder(tvIndexView.getMeasuredWidth() / 2, 5);
if (indexTitle != null && indexTitle.getContentDescription() != null) {
tvIndexView.setText(indexTitle.getContentDescription());
}
View transInfoView = recyclerView.findChildViewUnder(tvIndexView.getMeasuredWidth() / 2, tvIndexView.getMeasuredHeight() + 1);
if (transInfoView != null && transInfoView.getTag() != null) {
int transViewStatus = (int) transInfoView.getTag();
int dealtY = transInfoView.getTop() - tvIndexView.getMeasuredHeight();
if (transViewStatus == MeetingHistoryAdapter.HAS_STICKY_VIEW) {
if (transInfoView.getTop() > 0) {
flyIndex.setTranslationY(dealtY);
} else {
flyIndex.setTranslationY(0);
}
} else if (transViewStatus == MeetingHistoryAdapter.NONE_STICKY_VIEW) {
flyIndex.setTranslationY(0);
}
}
}
});
4. Adapter
public static final int FIRST_STICKY_VIEW = 1;
public static final int HAS_STICKY_VIEW = 2;
public static final int NONE_STICKY_VIEW = 3;
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
if(position==0){
holder.itemView.setTag(FIRST_STICKY_VIEW);
}else{
if(holder.rlayIndex.getVisibility()==View.GONE){
holder.itemView.setTag(NONE_STICKY_VIEW);
}else{
holder.itemView.setTag(HAS_STICKY_VIEW);
}
}
holder.itemView.setContentDescription(itemContent.getItemTitle());
}