本文主要是介绍ScrollView的Holder效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
**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 stubonScrollchanged(myScrollView.getScrollY());}});
}
@Override
public void onScrollchanged(int t) {// TODO Auto-generated method stubint translation = Math.max(t,holderLayout.getTop());topLayout.setTranslationY(translation);
}
@Override
public void onTouchUp() {// TODO Auto-generated method stub}
@Override
public 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效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!