实现滑动隐藏标题栏布局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
- 实现滑动隐藏标题栏布局ListView所碰到的问题
- 滑动隐藏标题栏布局ListView
- 简易:ListView滑动隐藏标题栏
- 标题栏的滑动隐藏和显示是怎么实现的?
- 实现ScrollView滑动时标题栏的隐藏效果
- 实现ScrollView滑动时标题栏的隐藏效果
- ListView实现标题栏的透明度的问题
- 简单代码实现向上滑动隐藏标题栏
- PullToRefreshListView滑动布局隐藏问题
- android ListView向上滑动隐藏标题,下拉显示标题栏
- android ListView向上滑动隐藏标题,下拉显示标题栏
- android ListView向上滑动隐藏标题,下拉显示标题栏
- android ListView向上滑动隐藏标题,下拉显示标题栏
- android ListView向上滑动隐藏标题,下拉显示标题栏
- 滑动隐藏标题栏
- fragment中实现listView的滑动隐藏toolbar
- 炫酷:一句代码实现标题栏、导航栏滑动隐藏。ByeBurger库的使用和实现
- 一句代码实现标题栏、导航栏滑动隐藏。ByeBurger库的使用和实现
- 有备无患 打造UEFI应急启动盘
- openstack新手之命令行映射到执行函数
- Python2.7 with MySQL Connector/Python Sample(Windows OS)
- <intent-filter>隐式intent的用法
- hdu 1028 Ignatius and the Princess III
- 实现滑动隐藏标题栏布局ListView所碰到的问题
- Kill -HUP详解
- linux利用sendmail发送邮件的方法
- 这是一个秒杀系统,即大量用户抢有限的商品,先到先得 用户并发访问流量非常大,需要分布式的机器集群处理请求 系统实现使用Java
- JAVA基础(16) Servlet/Filter/Listener/Interceptor区别与联系
- spring的AOP和事务
- 今天是我在CSDN安家的第一天
- IIS上传和下载文件的大小限制
- Apple Pay教程(附示例代码)(1)