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 stub
            onScrollchanged(myScrollView.getScrollY());

        }
    });
}
@Override
public void onScrollchanged(int t) {
    // TODO Auto-generated method stub
    int 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工具请告知小弟,谢谢!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值