• android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法


    正常使用ScrollView控件的一些api详解。

    package com.example.lenovo.mydemoapp.scrollViewDemo;
    
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    
    import com.example.lenovo.mydemoapp.GuidePage;
    import com.example.lenovo.mydemoapp.R;
    
    public class MyScrollViewDemo extends AppCompatActivity {
        private final String TAG = "MyScrollViewDemo";
        private ScrollView mScrollView;
        private LinearLayout ll;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_scroll_view_demo);
            mScrollView = (ScrollView)findViewById(R.id.ScrollView);
           // View view1 =   mScrollView.getChildAt(0);
           // FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view1.getLayoutParams();
           // Log.e(TAG, "Child is weght:" +lp.width);
            //布局参数是 根据当前布局的父类布局,
            // 例如当前的ScrollView的父类布局实际上是LinearLayout所以写成: LinearLayout.LayoutParams
            //另外布局参数的参数其实是高度和宽度的大小
            LinearLayout.LayoutParams mSlp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置高度和宽度
            mSlp.width = 300;
            mSlp.height = 500;
            //设置居中
            mSlp.gravity = Gravity.CENTER_HORIZONTAL;
            //显示水平滚动条
            //mScrollView.setHorizontalScrollBarEnabled(false);
            //显示垂直滚动条
            mScrollView.setVerticalScrollBarEnabled(false);
            //配置布局参数
            mScrollView.setLayoutParams(mSlp);
            //滚动条大小
            //mScrollView.setScrollBarSize(10);
            //滚动条音效
            //mScrollView.setSoundEffectsEnabled(false);
            //滚动条样式
            //mScrollView.setScrollBarStyle();
            //在淡入淡出前设置滚动条默认延迟
            //mScrollView.setScrollBarDefaultDelayBeforeFade();
            //水平淡入淡入效果
            //mScrollView.setHorizontalFadingEdgeEnabled();
           //垂直淡入淡入效果
            mScrollView.setVerticalFadingEdgeEnabled(true);
            //淡入淡出边缘长度
            mScrollView.setFadingEdgeLength(100);
            //允许ScrollView的子控件填满ScrollView控件
            mScrollView.setFillViewport(true);
            //箭头按键是否可以执行滚动
            //mScrollView.setSmoothScrollingEnabled(false);
            ll = (LinearLayout)findViewById(R.id.ScrollView_layout);
            //因为ScrollView 布局实际上是继承FrameLayout 布局的 所以布局参数也是设置为FrameLayout
            FrameLayout.LayoutParams mLp = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            mLp.gravity = Gravity.CENTER_HORIZONTAL;//设置横向居中
            //设置高度和宽度
            mLp.width = 200;
            mLp.height = 500;
            ll.setLayoutParams(mLp);
            //设置外边距
            mLp.setMargins(10,50,10,0);
            //设置内边距
            ll.setPadding(10,10,10,10);
            int i=0;
            do{
                TextView textView = new TextView(this);
                textView.setTextSize(40f);
                textView.setGravity(Gravity.CENTER);
                if(i==0) {
                    textView.setText(" ");
                }else if(i==21){
                    textView.setText(" ");
                }else {
                    textView.setText("" + i);
                }
                i++;
                Log.e(TAG, "i="+i );
                ll.addView(textView);
    
            }while (i<22);
    
            mScrollView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                        /*
                            ACTION_MOVE 移动
                            ACTION_MASK 隐藏
                            ACTION_DOWN 按下
                            ACTION_UP 按起
                            ACTION_CANCEL 取消
                            ACTION_OUTSIDE 外面
                            ACTION_POINTER_DOWN 指向下
                            ACTION_POINTER_UP 指向上
                            ACTION_HOVER_MOVE 来回移动
                            ACTION_SCROLL 滚动
                            ACTION_HOVER_ENTER 来回输入
                            ACTION_HOVER_EXIT 来回输出
                            ACTION_BUTTON_PRESS 按钮按下
                            ACTION_BUTTON_RELEASE 按键释放
                            ACTION_POINTER_INDEX_MASK 指针数值隐藏
                            ACTION_POINTER_INDEX_SHIFT  指针数值变化
                         */
                        switch (event.getAction()){
                            case MotionEvent.ACTION_MOVE:
                                Log.e(TAG, "开始滚动");
                                Log.e(TAG, "当前getScaleY:"+mScrollView.getScaleY());
                                Log.e(TAG, "当前getPivotY:"+mScrollView.getPivotY());
                                break;
                            case MotionEvent.ACTION_MASK:
                                Log.e(TAG, "停止滚动");
                                Log.e(TAG, "当前getPivotY:"+mScrollView.getPivotY());
                                break;
                            default:
                                break;
    
                        }
    
                    return false;
                }
            });
    
        }
    }

    效果图:

     

    自定义重写ScrollView的class,里面将onScrollChanged的坐标数据回调出去

    package com.example.lenovo.mydemo.myScrollViewDemo;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ScrollView;
    
    /**
     * Created by lenovo on 2018/6/26.
     */
    
    public class MyScrollView extends ScrollView {
        public MyScrollView(Context context) {
            super(context);
        }
    
        public MyScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            if(this.onScrollChangedListener != null){
                onScrollChangedListener.onScrollChanged(t,oldt);
    
            }
        }
        private OnScrollChangedListener onScrollChangedListener;
        public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener){
            this.onScrollChangedListener = onScrollChangedListener;
    
        }
        public abstract interface OnScrollChangedListener{
            public abstract void  onScrollChanged (int t,int oldt);
    
        }
    }
     

    使用这个重写的ScrollView

    package com.example.lenovo.mydemo.myScrollViewDemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    import com.example.lenovo.mydemo.R;
    
    public class MyScroll extends AppCompatActivity {
        private String TAG = "MyScroll";
        private MyScrollView myScrollView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_scroll);
            myScrollView = (MyScrollView)findViewById(R.id.scroll_1);
            myScrollView.setOnScrollChangedListener(new MyScrollView.OnScrollChangedListener() {
                @Override
                public void onScrollChanged(int t, int oldt) {
                    Log.e(TAG, "控件布局高度:"+myScrollView.getHandler());
                    //getMeasuredHeight 和 getHandler 不能在onCreate 重写方法中使用,否则会返回0或者null
                    Log.e(TAG, "控件布局实际高度:"+myScrollView.getChildAt(0).getMeasuredHeight());
                    Log.e(TAG, "new Y = "+t);
                    Log.e(TAG, "old Y = "+oldt );
                }
            });
    
        }
    }

    效果图:

    效果图2:

    接口回调例子2:

    //注意 接口了按键监听
    public class ChildListAdapter extends RecyclerView.Adapter<ChildListAdapter.ViewHolder> implements View.OnClickListener{
        private final String TAG = "HomePageRecyclerAdapter";
        private List<ChildListData> mList;
        private Context mContext;
        public ChildListAdapter(Context context, List<ChildListData> list){
            this.mContext = context;
            this.mList = list;
        }
    
        @Override
        public ChildListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_child_list,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);
            view.setOnClickListener(this);//添加按键监听
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ChildListAdapter.ViewHolder holder, int position) {
            ChildListData childListData = mList.get(position);
            //传入整组数据
            holder.itemView.setTag(childListData);
            holder.name.setText(childListData.getName());
            Bitmap bt = BitmapFactory.decodeFile(childListData.getFilePath());
            if(bt != null){
                Drawable drawable = new BitmapDrawable(mContext.getResources(),bt);
                holder.imageView.setImageDrawable(drawable);
            }else {
                Log.e(TAG, "没有获取到头像,使用默认头像");
                holder.imageView.setImageResource(R.mipmap.default_avatar);
            }
    
        }
    
        @Override
        public int getItemCount() {
            return mList.size();
        }
    
        @Override
        public void onClick(View v) {
            if(mOnItemClickListener != null){
                mOnItemClickListener.onItemClick(v, (ChildListData)v.getTag());//回调数据
            }
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView name;
            public ViewHolder(View itemView) {
                super(itemView);
                imageView = (ImageView)itemView.findViewById(R.id.HomePage_row_image);
                name =  (TextView)itemView.findViewById(R.id.HomePage_row_name);
            }
        }
        public static interface OnRecyclerViewItemClickListener{
            void onItemClick(View view, ChildListData childListData);
        }
    
        private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    
        public void setOnItemClickListener(OnRecyclerViewItemClickListener listener){
            this.mOnItemClickListener = listener;
    
        }
    }
  • 相关阅读:
    AOP面向切面编程
    java中与运算,或运算,异或运算,取反运算
    Java中replace和replaceall的区别
    Satisfying memory ordering requirements between partial reads and non-snoop accesses
    JS判断字符串中是否存在某个字符
    PHP 简介
    PHP 是什么
    mysql substr() 函数
    MySQL 数学函数
    MySQL 字符串截取SUBSTRING()函数
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708606.html
Copyright © 2020-2023  润新知