欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法

 更新時(shí)間:2016年09月18日 14:35:43   作者:妖久  
這篇文章主要介紹了Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

借助View的OnTouchListener接口來(lái)監(jiān)聽listView的滑動(dòng),通過(guò)比較與上次坐標(biāo)的大小,判斷滑動(dòng)方向,并通過(guò)滑動(dòng)方向來(lái)判斷是否需顯示或者隱藏對(duì)應(yīng)的布局,并且?guī)в袆?dòng)畫效果。

1.自動(dòng)顯示隱藏Toolbar

首先給listView增加一個(gè)HeaderView,避免第一個(gè)Item被Toolbar遮擋。

View header=new View(this);
header.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
(int)getResources().getDimension(R.dimen.abc_action_bar_default_height_material)));
mListView.addHeaderView(header); 
//R.dimen.abc_action_bar_default_height_material為系統(tǒng)ActionBar的高度

定義一個(gè)mTouchSlop變量,獲取系統(tǒng)認(rèn)為的最低滑動(dòng)距離

mTouchSlop=ViewConfiguration.get(this).getScaledTouchSlop();//系統(tǒng)認(rèn)為的最低滑動(dòng)距離 

判斷滑動(dòng)事件

bbsListView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) 
{
case MotionEvent.ACTION_DOWN:
mFirstY=event.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurrentY=event.getY();
if(mCurrentY-mFirstY>mTouchSlop)
direction=0; //listView向下滑動(dòng)
else if(mFirstY-mCurrentY>mTouchSlop)
direction=1; //listView向上滑動(dòng)
if(direction==1)
{
if(mShow)
{
toolbarAnim(1); //隱藏上方的view
mShow=!mShow;
}
}
else if(direction==0)
{
if(!mShow)
{
toolbarAnim(0); //展示上方的view
mShow=!mShow;
}
}
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}

屬性動(dòng)畫

protected void toolbarAnim(int flag) 
{
if(set!=null && set.isRunning())
{
set.cancel();
}
if(flag==0)
{
mAnimator1=ObjectAnimator.ofFloat(mToolbar, 
"translationY", linearView.getTranslationY(),0);
mAnimator2=ObjectAnimator.ofFloat(mToolbar, "alpha", 0f,1f);
}
else if(flag==1)
{
mAnimator1=ObjectAnimator.ofFloat(mToolbar, 
"translationY", linearView.getTranslationY(),-linearView.getHeight());
mAnimator2=ObjectAnimator.ofFloat(mToolbar, "alpha", 1f,0f);
}
set=new AnimatorSet();
set.playTogether(mAnimator1,mAnimator2);
set.start();
}
//上面為位移還有透明度屬性動(dòng)畫

使用的時(shí)候theme要用NoActionBar的,不然會(huì)引起沖突。同時(shí)引入編譯

dependencies{
compile fileTree(include:['*.jar'],dir:'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
}

2.當(dāng)要隱藏和顯示的組件不是toolbar,而是我們自定義的布局myView時(shí),需要注意一些點(diǎn),

(1) 布局要用相對(duì)布局,讓我們自定義的布局懸浮在listView上方。

(2)避免第一個(gè)Item被myView遮擋,給listView增加一個(gè)HeaderView,此時(shí)需要測(cè)量myView的高度,要用下面這種方法,把任務(wù)post到UI線程中,不然執(zhí)行會(huì)出錯(cuò)。

final View header=new View(this); //給listView增加一個(gè)headView,避免第一個(gè)item被遮擋 header.post(new Runnable() {
public void run() {
header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, myView.getHeight()));
}
});

其他的與toolbar一樣

以上所述是小編給大家介紹的Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論