ScrollView的Holder效果
来源:互联网 发布:淘宝转化率1.39 编辑:程序博客网 时间:2024/05/21 18:35
**ScrollView的Holder效果**
现在scrollview的holder效果非常常见,今天我们就来学习下怎么自己实现一个holder效果。
首先我们要自己得到scrolly这个值就必须继承srollview这个类,并重写她的srollchanged方法,并利用回调接口得到这个值。代码如下:
public class MyScrollView extends ScrollView{ private CallBacks callback; void setcallback(CallBacks callBacks){ this.callback=callBacks; } public MyScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } //在这里得到y值 protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); callback.onScrollchanged(t); } //回调接口 public interface CallBacks{ public void onScrollchanged(int t); public void onTouchUp(); public void onTouchDown(); }}
然后就是利用我们自己写的srollview控件来实现我们的holder效果
布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent_layout" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.myscrollview.MyScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iamge" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/feng" android:scaleType="centerCrop" /> <include android:id="@+id/holder" layout="@layout/buy_layout" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/girl1" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/girl3" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/girl1" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/girl3" android:scaleType="centerCrop" /> </LinearLayout> <include android:id="@+id/top" layout="@layout/button_layout" /> </FrameLayout> </com.example.myscrollview.MyScrollView></LinearLayout>
最后就是我们的Mainactivity了:
public class MainActivity extends Activity implements CallBacks {
private MyScrollView myScrollView;
private View holderbuylaLayout;
private View topbuylaLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myScrollView=(MyScrollView) findViewById(R.id.scrollView);
holderLayout=findViewById(R.id.holder);
topLayout=findViewById(R.id.top);
myScrollView.setcallback(this);
myScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { // TODO Auto-generated method stub onScrollchanged(myScrollView.getScrollY()); } });}@Overridepublic void onScrollchanged(int t) { // TODO Auto-generated method stub int translation = Math.max(t,holderLayout.getTop()); topLayout.setTranslationY(translation); }@Overridepublic void onTouchUp() { // TODO Auto-generated method stub}@Overridepublic void onTouchDown() { // TODO Auto-generated method stub}
在布局完成绘制的时候调用myScrollView.getViewTreeObserver().addOnGlobalLayoutListen这个方法,得到holderbuylaLayout.getTop(),
在这里toplaLayout.setTranslationY(translation)的意思就是在Y轴移动的距离。有2个BUTTON,其中一个在顶部,一个在正常的位置,我们调用这个setTranslationY方法使他们2个重合了。
在当滑到顶部的时候 在这里做了一个判断
int translation = Math.max(t,holderbuylaLayout.getTop());
如果滑动距离大于了这个holderbuylaLayout.getTop(),那么就表示到顶了。
运行一下:
图片质量不好,见谅了!!!!!!!!!!!!!哪位大哥有好的GIFj工具请告知小弟,谢谢!!!!
- ScrollView的Holder效果
- ScrollView的反弹效果
- Holder的使用
- holder.js的使用
- TextView 的ScrollView滚动效果
- ScrollView反弹效果的实现
- 去除scrollview的阻尼效果
- TextView 的ScrollView滚动效果
- ScrollView反弹效果的实现
- ScrollView反弹效果的实现
- ListView适应ScrollView的效果
- 带阻尼效果的ScrollView
- android MVP的holder变异
- Recycler的适配器和holder
- ScrollView的反弹效果的实现
- ScrollView实现TextView一行一行的滚动效果
- Android ScrollView反弹效果的实现
- Android ScrollView反弹效果的实现
- FreeRadius Version3.0.7文件目录
- 进程控制块PCB
- javaweb
- Bsidesiowa 2015 Track2: Secure Process Isolation With Docker By Greg Rice
- 二叉树的文本模式输出
- ScrollView的Holder效果
- 第7讲-项目三-求工资
- Eclipse插件7
- vb.net 操作Access数据库帮助类
- 二叉树
- 怎么解决tomcat占用8080端口问题
- CreateThread与_beginthreadex和_beginthread
- 欢迎使用CSDN-markdown编辑器
- Top 10 Algorithms for Coding Interview