ScrollView监听滑动到哪个子控件

android 专栏收录该内容
1 篇文章 0 订阅


<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">情景:在一个scrollview里面有多个可以播放视频的item,可以上下滑动,当item可以见的时候视频自动开始播放。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">一共3个文件</span></span>

show code


package com.example.beteng.scrollview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class MainActivity extends AppCompatActivity {

    private boolean isScoll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ScrollView myScrollview = (ScrollView) findViewById(R.id.scrollView);


        myScrollview.setOnTouchListener(new View.OnTouchListener() {
            private int lastY = 0;
            private int touchEventId = -9983761;
            Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    View scroller = (View) msg.obj;

                    if (msg.what == touchEventId) {
                        if (lastY == scroller.getScrollY()) {
                            //停止了

                            double i = DensityUtils.px2dp(MainActivity.this, lastY);

                            int itemShow = 0;
                            if ((i<=50)) {
                                itemShow = 1;
                                Log.e("MainActivity", "显示了第" + 1 + "个");
                            }else if (i>50&&i<=250) {
                                itemShow = 2;
                                Log.e("MainActivity", "显示了第" + 2 + "个");
                            }else if (i>250&&i<=450) {
                                itemShow = 3;
                                Log.e("MainActivity", "显示了第" + 3 + "个");
                            }else {
                                itemShow = (int) (i / (200+8))+2;

                            }

                            Log.e("MainActivity", "lastY:" + i);

                            Log.e("MainActivity", "显示了第" + itemShow + "个");


                        } else {
                            handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 1);
                            lastY = scroller.getScrollY();
                        }
                    }
                }
            };


            @Override
            public boolean onTouch(View v, MotionEvent event) {
                isScoll = false;
                int eventAction = event.getAction();
                int y = (int) event.getRawY();
                switch (eventAction) {
                    case MotionEvent.ACTION_UP:

                        handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);

                        break;
                    default:
                        break;
                }
                return false;
            }



        });
    }
}

package com.example.beteng.scrollview;

import android.content.Context;
import android.util.TypedValue;

/**
 * 常用单位转换的辅助类
 *
 *
 *
 */
public class DensityUtils
{
    private DensityUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * dp转px
     *
     * @param context
     * @param val
     * @return
     */
    public static int dp2px(Context context, float dpVal)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     *
     * @param context
     * @param val
     * @return
     */
    public static int sp2px(Context context, float spVal)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, context.getResources().getDisplayMetrics());
    }

    /**
     * px转dp
     *
     * @param context
     * @param pxVal
     * @return
     */
    public static float px2dp(Context context, float pxVal)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    /**
     * px转sp
     *
     * @param fontScale
     * @param pxVal
     * @return
     */
    public static float px2sp(Context context, float pxVal)
    {
        return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
    }

}

源码下载地址:https://github.com/yogkin/Scrollview.git



  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值