这期内容当中小编将会给大家带来有关如何在Android应用中实现一个列表悬浮效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
具体方法如下:
package com.xiaos.view;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnGroupClickListener;public class PinnedHeaderExpandableListView extends ExpandableListView implements OnScrollListener,OnGroupClickListener { public PinnedHeaderExpandableListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); registerListener(); } public PinnedHeaderExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); registerListener(); } public PinnedHeaderExpandableListView(Context context) { super(context); registerListener(); } public interface HeaderAdapter { public static final int PINNED_HEADER_GONE = 0; public static final int PINNED_HEADER_VISIBLE = 1; public static final int PINNED_HEADER_PUSHED_UP = 2; int getHeaderState(int groupPosition, int childPosition); void configureHeader(View header, int groupPosition,int childPosition, int alpha); void setGroupClickStatus(int groupPosition, int status); int getGroupClickStatus(int groupPosition); } private static final int MAX_ALPHA = 255; private HeaderAdapter mAdapter; private View mHeaderView; private boolean mHeaderViewVisible; private int mHeaderViewWidth; private int mHeaderViewHeight; public void setHeaderView(View view) { mHeaderView = view; AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); view.setLayoutParams(lp); if (mHeaderView != null) { setFadingEdgeLength(0); } requestLayout(); } private void registerListener() { setOnScrollListener(this); setOnGroupClickListener(this); } private void headerViewClick() { long packedPosition = getExpandableListPosition(this.getFirstVisiblePosition()); int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition); if (mAdapter.getGroupClickStatus(groupPosition) == 1) { this.collapseGroup(groupPosition); mAdapter.setGroupClickStatus(groupPosition, 0); } else{ this.expandGroup(groupPosition); mAdapter.setGroupClickStatus(groupPosition, 1); } this.setSelectedGroup(groupPosition); } private float mDownX; private float mDownY; @Override public boolean onTouchEvent(MotionEvent ev) { if (mHeaderViewVisible) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDownX = ev.getX(); mDownY = ev.getY(); if (mDownX <= mHeaderViewWidth && mDownY <= mHeaderViewHeight) { return true; } break; case MotionEvent.ACTION_UP: float x = ev.getX(); float y = ev.getY(); float offsetX = Math.abs(x - mDownX); float offsetY = Math.abs(y - mDownY); // 假设 HeaderView 是可见的 , 点击在 HeaderView 内 , 那么触发 headerClick() if (x <= mHeaderViewWidth && y <= mHeaderViewHeight && offsetX <= mHeaderViewWidth && offsetY <= mHeaderViewHeight) { if (mHeaderView != null) { headerViewClick(); } return true; } break; default: break; } } return super.onTouchEvent(ev); } @Override public void setAdapter(ExpandableListAdapter adapter) { super.setAdapter(adapter); mAdapter = (HeaderAdapter) adapter; } @Override public boolean onGroupClick(ExpandableListView parent,View v,int groupPosition,long id) { if (mAdapter.getGroupClickStatus(groupPosition) == 0) { mAdapter.setGroupClickStatus(groupPosition, 1); parent.expandGroup(groupPosition); //Header自己主动置顶 //parent.setSelectedGroup(groupPosition); } else if (mAdapter.getGroupClickStatus(groupPosition) == 1) { mAdapter.setGroupClickStatus(groupPosition, 0); parent.collapseGroup(groupPosition); } // 返回 true 才干够弹回第一行 , 不知道为什么 return true; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mHeaderView != null) { measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); mHeaderViewWidth = mHeaderView.getMeasuredWidth(); mHeaderViewHeight = mHeaderView.getMeasuredHeight(); } } private int mOldState = -1; @Override protected void onLayout(boolean changed, int left, int top, int right,int bottom) { super.onLayout(changed, left, top, right, bottom); final long flatPostion = getExpandableListPosition(getFirstVisiblePosition()); final int groupPos = ExpandableListView.getPackedPositionGroup(flatPostion); final int childPos = ExpandableListView.getPackedPositionChild(flatPostion); int state = mAdapter.getHeaderState(groupPos, childPos); if (mHeaderView != null && mAdapter != null && state != mOldState) { mOldState = state; mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); } configureHeaderView(groupPos, childPos); } public void configureHeaderView(int groupPosition, int childPosition) { if (mHeaderView == null || mAdapter == null || ((ExpandableListAdapter) mAdapter).getGroupCount() == 0) { return; } int state = mAdapter.getHeaderState(groupPosition, childPosition); switch (state) { case HeaderAdapter.PINNED_HEADER_GONE: { mHeaderViewVisible = false; break; } case HeaderAdapter.PINNED_HEADER_VISIBLE: { mAdapter.configureHeader(mHeaderView, groupPosition,childPosition, MAX_ALPHA); if (mHeaderView.getTop() != 0){ mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); } mHeaderViewVisible = true; break; } case HeaderAdapter.PINNED_HEADER_PUSHED_UP: { View firstView = getChildAt(0); int bottom = firstView.getBottom(); // intitemHeight = firstView.getHeight(); int headerHeight = mHeaderView.getHeight(); int y; int alpha; if (bottom < headerHeight) { y = (bottom - headerHeight); alpha = MAX_ALPHA * (headerHeight + y) / headerHeight; } else { y = 0; alpha = MAX_ALPHA; } mAdapter.configureHeader(mHeaderView, groupPosition,childPosition, alpha); if (mHeaderView.getTop() != y) { mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y); } mHeaderViewVisible = true; break; } } } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mHeaderViewVisible) { //分组栏是直接绘制到界面中。而不是增加到ViewGroup中 drawChild(canvas, mHeaderView, getDrawingTime()); } } @Override public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) { final long flatPos = getExpandableListPosition(firstVisibleItem); int groupPosition = ExpandableListView.getPackedPositionGroup(flatPos); int childPosition = ExpandableListView.getPackedPositionChild(flatPos); configureHeaderView(groupPosition, childPosition); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- 如何判断 Java 数组中是否存在重复元素?(Java怎么判断数组是否有重复元素)
- Java 中魔法值究竟是什么含义?(java魔法值是什么意思)
- 在 Java 开发中,Javase 究竟扮演着怎样的角色?(Javase在Java开发中扮演什么角色)
- 在 Java 中如何实现 base64 到 blob 的转换?(Java中base64转blob怎么实现)
- Java 中 clazz 类创建的方式有哪些?(java clazz类创建的方式是什么)
- 如何正确使用 Java PersistenceContext 类?实例详解!(Java PersistenceContext类使用实例)
- Java 中 Quartz 框架究竟是什么?(java中quartz是什么框架)
- PHP数据类型转换对存储方式的影响
- Java House 有哪些具体的方法?(Java House的方法有哪些)
- Java 如何读取 txt 文件内容并进行处理?(java怎么读取txt文件内容并处理)
猜你喜欢
AI推送时光机如何在Android应用中实现一个列表悬浮效果
后端开发2023-05-31
如何在Android应用中使用ScrollView实现悬浮效果
后端开发2023-05-31
怎么在android应用中实现一个RecyclerView悬浮吸顶效果
后端开发2023-05-31
android开发中如何利用listview实现一个悬浮topBar效果
后端开发2023-05-31
Android项目中项目实现一个控件悬浮效果
后端开发2023-05-31
如何在android中利用listview实现一个列表展示效果
后端开发2023-05-31
如何在Android中利用ScrollView实现一个顶部悬停效果
后端开发2023-05-31
如何在Android应用中实现一个Gallery画廊效果
后端开发2023-05-31
在Android开发中使用WindowManager实现一个悬浮按钮
后端开发2023-05-31
如何在Android应用中利用Dialog实现一个动画效果
后端开发2023-05-31
Android应用怎么实现一个浮动状态栏效果
后端开发2023-05-31
如何使用Android实现一个悬浮在软键盘上的输入栏
后端开发2023-06-29
如何在Android应用中利用Spinner实现一个下拉列表功能
后端开发2023-05-31
如何在Android应用中实现一个沉浸式状态栏效果
后端开发2023-05-31
怎么在Android应用中实现一个动画效果
后端开发2023-05-31
如何在Android应用中实现一个动态searchview搜索栏效果
后端开发2023-05-31
怎么在Android应用中实现一个抽屉效果
后端开发2023-05-31
Layui如何实现数据表格中鼠标悬浮图片放大效果
后端开发2024-04-02
咦!没有更多了?去看看其它编程学习网 内容吧