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

前言

这里只是一个小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);
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,163评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,422评论 4 61
  • 在一个慵懒的清晨,我开始了难忘的迪拜之行的第二天。醒来后,洗漱完毕,我继续去健身房锻炼。你也许会问:为什么不...
    点石陈金阅读 487评论 0 4
  • 决明子减肥茶一 材料:荷叶3g,决明子6g,制大黄3g,首乌3g,扁豆3g,玳玳花3g。 做法:以上药材冲洗干净,...
    不思凉阅读 584评论 0 1
  • 那些我们没谈过的事 《那些我们没谈过的事》是马克.李维写的,第一次看他的书还是两年半前,那时我高三。记得当时是熬夜...
    溪暮阅读 1,052评论 5 1

友情链接更多精彩内容