首页 > 系统 > Android > 正文

Android StickListView实现悬停效果

2019-12-12 06:14:26
字体:
来源:转载
供稿:网友

先看看效果图:

实现思路

监听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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表