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

根据这篇博文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;

	@Override
	protected 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() {

			@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) { // 下滑 显示titleBar
						showHideTitlebar(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();
	}

	@Override
	public 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;

	@Override
	protected 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);
	}

	@Override
	public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值