美文网首页Android开发Android开发
横滑列表 中间项变大第一种效果

横滑列表 中间项变大第一种效果

作者: crossroads | 来源:发表于2017-02-20 10:48 被阅读73次

    前言

    这里只是一个小demo
    先上效果图吧

    源码如下:

    public class MiddleHorizontalScrollView extends HorizontalScrollView {
        int current = -1; //当前位于中间item的位置
        double halfScreenWidth; //屏幕宽度的一半
        LinearLayout linearLayout; //内容布局
        int itemCount;//item的个数
    
        public MiddleHorizontalScrollView(Context context) {
            super(context, null);
    
        }
    
        public MiddleHorizontalScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            halfScreenWidth =1.0* getScreenWidth(context)/2;
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            //初始化时候确认中间item放大
            if (getChildCount() > 0 && current == -1) {
                linearLayout = (LinearLayout) getChildAt(0);
                itemCount = linearLayout.getChildCount();
                for (int i = 0; i < itemCount - 1; i++) {
                    if (linearLayout.getChildAt(i).getX() - l < halfScreenWidth && linearLayout.getChildAt(i + 1).getX() - l >= halfScreenWidth) {
                        TextView currentTxt = (TextView) linearLayout.getChildAt(i);
                        currentTxt.setTextSize(40);
                        currentTxt.setTextColor(Color.GREEN);
                        current = i;
                        break;
                    }
                }
            }
        }
    
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            if (current > 0 && linearLayout != null){
                TextView currentTxt = (TextView) linearLayout.getChildAt(current);
                //如果该item仍位于屏幕中间位置
                if (currentTxt.getX() - l < halfScreenWidth && linearLayout.getChildAt(current + 1).getX() - l >= halfScreenWidth) {
                    currentTxt.setTextSize(40);
                    currentTxt.setTextColor(Color.GREEN);
                }
                else {
                    currentTxt.setTextSize(20);
                    currentTxt.setTextColor(Color.RED);
                    //寻找位于屏幕中间的item
                    if (l > oldl) { //向右滑动
                        if (current < itemCount - 2)
                            for (int i = current + 1; i < itemCount - 1; i++) {
                                if (linearLayout.getChildAt(i).getX() - l < halfScreenWidth && linearLayout.getChildAt(i + 1).getX() - l >= halfScreenWidth) {
                                    TextView txt = (TextView) linearLayout.getChildAt(i);
                                    txt.setTextSize(40);
                                    txt.setTextColor(Color.GREEN);
                                    current = i;
                                    break;
                                }
                            }
                    }
                    else {//向左滑动
                        if (current > 1)
                            for (int i = current - 1; i > 1; i--) {
                                if (linearLayout.getChildAt(i).getX() - l < halfScreenWidth && linearLayout.getChildAt(i + 1).getX() - l >= halfScreenWidth) {
                                    TextView txt = (TextView) linearLayout.getChildAt(i);
                                    txt.setTextSize(40);
                                    txt.setTextColor(Color.GREEN);
                                    current = i;
                                    break;
                                }
                            }
                    }
                }
            }
        }
    
        /**
         * 获取屏幕宽度
         */
        public static int getScreenWidth(Context context) {
            Display display = ((WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            return display.getWidth();
        }
    
    }
    
       <com.demo.viewdemo.MiddleHorizontalScrollView
    
            android:id="@+id/horizontalScrollView"
    
            android:layout_width="fill_parent"
    
            android:layout_height="wrap_content"
    
            android:scrollbars="none" >
    
            <LinearLayout
    
                android:id="@+id/content"
    
                android:layout_width="fill_parent"
    
                android:layout_height="200dp"
    
                android:layout_centerVertical="true"
    
                android:orientation="horizontal"
    
                />
    
        </com.demo.viewdemo.MiddleHorizontalScrollView>
    
           contentLinear= (LinearLayout) findViewById(R.id.content);
            horizontalScrollView= (MiddleHorizontalScrollView) findViewById(R.id.horizontalScrollView);
            contentLinear.removeAllViews();
            for (int i = 0; i < 50; i++) {
                TextView child = new TextView(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(80, ViewGroup.LayoutParams.MATCH_PARENT);
                params.gravity=Gravity.CENTER;
                child.setTextSize(20);
                child.setTextColor(Color.RED);
                child.setGravity(Gravity.CENTER);
                child.setText(""+i);
                contentLinear.addView(child,params);
            }
    

    相关文章

      网友评论

        本文标题:横滑列表 中间项变大第一种效果

        本文链接:https://www.haomeiwen.com/subject/fmvswttx.html