实现滑动隐藏标题栏布局ListView所碰到的问题

来源:互联网 发布:写jquery必须会js么 编辑:程序博客网 时间:2024/05/21 06:49

根据这篇博文http://blog.csdn.net/soul_code/article/details/50674528实现效果


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:android_custom="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffff"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/titlebar"        android:layout_width="match_parent"        android:layout_height="45dp"        android:background="#e6e6e6"        android:orientation="vertical" >        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:text="标题"            android:textColor="#000000"            android:textSize="32sp" >        </TextView>    </LinearLayout>    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </ListView></LinearLayout>



public class MainActivity extends Activity implements OnClickListener {LinearLayout titlebar;ListView listview;private float mFirstY;private float mCurrentY;protected float mTouchSlop;private ObjectAnimator mAnimatorTitle;private ObjectAnimator mAnimatorContent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_listviewa);listview = (ListView) findViewById(R.id.listview);titlebar = (LinearLayout) findViewById(R.id.titlebar);mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(new ViewConfiguration());listview.setOnTouchListener(new View.OnTouchListener() {@Overridepublic 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) { // 下滑 显示titleBarshowHideTitlebar(true);} else if (mFirstY - mCurrentY > mTouchSlop) {showHideTitlebar(false);}break;case MotionEvent.ACTION_UP:break;default:break;}return false;}});List<String> datas = new ArrayList<String>();for (int i = 0; i < 30; i++) {datas.add("data" + i);}ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);listview.setAdapter(adapter);}protected void showHideTitlebar(boolean tag) {if (mAnimatorTitle != null && mAnimatorTitle.isRunning()) {            mAnimatorTitle.cancel();        }        if (mAnimatorContent != null && mAnimatorContent.isRunning()) {            mAnimatorContent.cancel();        }        if (tag) {            mAnimatorTitle = ObjectAnimator.ofFloat(titlebar, "translationY", titlebar.getTranslationY(), 0);            float y = listview.getTranslationY();            int height = titlebar.getHeight();            mAnimatorContent = ObjectAnimator.ofFloat(listview, "translationY", listview.getTranslationY(), height);            float theY = listview.getY();            System.out.println(theY);        } else {            mAnimatorTitle = ObjectAnimator.ofFloat(titlebar, "translationY", titlebar.getTranslationY(), -titlebar.getHeight());            mAnimatorContent = ObjectAnimator.ofFloat(listview, "translationY", listview.getTranslationY(),0);            float theY = listview.getY();            System.out.println(theY);        }        mAnimatorTitle.start();        mAnimatorContent.start();}@Overridepublic void onClick(View v) {}}

在自己实现的过程中发现了几个问题,实现的效果跟原博主的效果有些差异。

(1)布局上有些差异,当把这里的LinearLayout换成RelativeLayout之后,就可以达到效果了。


这里的属性动画有几点启示,这里特别注意,这里修改的是translationY的值,修改了这个值启示也会影响View的getY()的值。

另一个例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:android_custom="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffff"    android:orientation="vertical" >    <LinearLayout         android:id="@+id/btn"        android:layout_width="match_parent"        android:background="#969696"        android:orientation="vertical"        android:clickable="true"        android:layout_height="200dp"></LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#e6e6e6"        android:orientation="vertical" >        <TextView            android:id="@+id/txt"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:padding="5dp"            android:background="#ff0000"            android:gravity="center"            android:text="标题"            android:textColor="#000000"            android:textSize="32sp" >        </TextView>        <TextView            android:id="@+id/txt2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:padding="5dp"            android:background="#0000ff"            android:gravity="center"            android:text="标题2"            android:textColor="#000000"            android:textSize="32sp" >        </TextView>    </LinearLayout></LinearLayout>


public class AnimActivity extends Activity implements OnClickListener {LinearLayout btn;TextView txt;TextView txt2;protected float mTouchSlop;private ObjectAnimator mAnimator;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_anim_test);txt = (TextView) findViewById(R.id.txt);txt2 = (TextView) findViewById(R.id.txt2);btn = (LinearLayout) findViewById(R.id.btn);btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {mAnimator = ObjectAnimator.ofFloat(txt2, "translationY", txt2.getTranslationY(), -txt2.getY());mAnimator.start();}}


这里要特别注意一点,translationY也是相对于父布局的,但是要注意。比如这里的“标题2”的translationY开始的时候为0。


(针对标题2)

启示可以这么理解,translationY就是移动的值,那么一开始根本就没有移动,那么translationY的值肯定为0。

而不要理解为View.getY(),因为此时View.getY()相对于Parent布局来说并不为0,这点不要搞混淆了。


点击btn一次:



点击btn第二次:


0 0
原创粉丝点击