文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

如何在Android应用中实现一个列表悬浮效果

2023-05-31 05:26

关注

这期内容当中小编将会给大家带来有关如何在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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯