先看看效果图: 实现思路: 监听ListView的滑动,等目的项为列表第一个可见的ItemView时,添加一个一个的布局,产生悬停效果 实现代码: public class
先看看效果图:
实现思路:
监听ListView的滑动,等目的项为列表第一个可见的ItemView时,添加一个一个的布局,产生悬停效果
实现代码:
public class CustomViewAcyivity extends BaseActivity {
Toolbar toolbar;
WindowManager mWindowManager;
WindowManager.LayoutParams mWindowLayoutParams;
TextView mTv;
boolean isshowing;//是否正在显示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_view);
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
List<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("我是第" + (i + 1) + "个选择项");
}
toolbar = $(R.id.toolbar);
ListView listView = $(R.id.view_list);
listView.setAdapter(new ArrayAdapter<>(this, R.layout.item_text, list));
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > 8) {//滑动到目的项时,显示悬停布局
if (!isShowing)
show();
} else {
if (isShowing)
hide();
}
}
});
}
//显示悬停布局
public void show() {
isShowing = true;
mWindowLayoutParams = new WindowManager.LayoutParams();
mWindowLayoutParams.fORMat = PixelFormat.TRANSLUCENT; //图片之外的其他地方透明
mWindowLayoutParams.gravity = Gravity.TOP;
mWindowLayoutParams.y = toolbar.getHeight() - SystemUtil.getStatusHeight(this);//设置悬停布局显示的Y坐标
mWindowLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mWindowLayoutParams.height = UnitUtil.dp2px(this, 50);//设置悬停布局显示的高度
mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
//设置悬停布局,为了看起来是悬停效果,布局的内容要设置成与ItemView一致
mTv = new TextView(this);
mTv.setGravity(Gravity.CENTER);
mTv.setBackgroundColor(getResources().getColor(R.color.white));
mTv.setTextSize(UnitUtil.px2sp(this, UnitUtil.dp2px(this, 16)));
mTv.setText("我是第10个选择项");
//添加悬停布局
mWindowManager.addView(mTv, mWindowLayoutParams);
}
//隐藏悬停布局
public void hide() {
if (mTv != null) {
isShowing = false;
mWindowManager.removeView(mTv);
mTv = null;
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/view_toolbar" />
<ListView
android:id="@+id/view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
您可能感兴趣的文章:Android中ScrollView实现滑动距离监听器的方法Android中HorizontalScrollView使用方法详解Android ScrollView只能添加一个子控件问题解决方法Android编程开发ScrollView中ViewPager无法正常滑动问题解决方法Android控件之ScrollView用法实例分析Android中实现监听ScrollView滑动事件Android中使用ScrollView指定view的顶部悬停效果
--结束END--
本文标题: Android StickListView实现悬停效果
本文链接: https://lsjlt.com/news/24846.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0