ScrollView水平滑动条选中条目的居中显示

创建选择器实现圆角矩形
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <corners android:radius="5dip"/>
        <solid android:color="#33ff0000"/>
    </shape>


创建选择器实现字体颜色改变
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

        <item android:state_selected="true" android:color="@android:color/white"></item>
        <item android:state_pressed="true" android:color="@android:color/white"></item>
        <item android:color="@android:color/darker_gray"></item>
    </selector>


创建选择器实现背景颜色改变
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

        <item android:state_selected="true" android:drawable="@drawable/bgshape_shape"></item>
        <item android:state_pressed="true" android:drawable="@drawable/bgshape_shape"></item>
    </selector>


布局文件引用
    <TextView
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@drawable/textcolor_selector"
        android:background="@drawable/bgcolor_selector"
        android:padding="5dip"
        android:text="热门" />


MainActivity后台代码实现
    private int screenWitdth;
    private HorizontalScrollView hsv;
    private LinearLayout ll;

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

        //获得屏幕宽度
        screenWitdth = getResources().getDisplayMetrics().widthPixels;
        //初始化视图
        initView();
        //子控件点击事件
        initEvent();
    }

    //初始化视图
    private void initView(){
        hsv = (HorizontalScrollView) findViewById(R.id.hsv);
        ll = (LinearLayout) findViewById(R.id.ll);
        //默认选中第一个子控件
        hsv.getChildAt(0).setSelected(true);
    }

    //定义子控件点击事件
    private void initEvent(){
        //获得水平滑动控件中子控件——水平线性布局内共有多少子控件
        int childrenCount=hsv.getChildCount();

        //对子控件循环监听
        for(int i=0; i<childrenCount; i++){
            final int currentIndex=i;
            //获得当前子控件
            View childView=hsv.getChildAt(i);

            //对当前子控件设置点击监听事件
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置当前子控件诶选中
                    selectChildView(currentIndex);
                }
            });
        }
    }

    //对点击选中的子控件进行设置
    private void selectChildView(int position){
        int childrenCount=hsv.getChildCount();
        for(int i=0; i<childrenCount; i++){
            //获得当前子控件对象,并设置为选中状态
            View child=hsv.getChildAt(i);
            child.setSelected(true);
        }

        //设置选中条目居中
        View currentView=hsv.getChildAt(position);
        int left=currentView.getLeft();     //获取点击控件与父控件左侧的距离
        int width=currentView.getMeasuredWidth();   //获得控件本身宽度
        int toX=left+width/2-screenWitdth/2;
        //使条目移动到居中显示
        hsv.smoothScrollTo(toX, 0);
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值