android+recyclerview悬停动画,Android RecyclerView 悬浮索引效果

这篇博客详细介绍了如何在Android中实现悬浮索引功能,结合RecycleView的滚动监听,动态更新索引并平滑移动悬浮框。通过布局文件、Activity的initView方法和Adapter的设置,展示了如何在滚动时保持索引视图与内容的对应关系。
摘要由CSDN通过智能技术生成

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());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值