android 水平滑动的控件

本文介绍了一个实现水平滑动的自定义控件CenterLockHorizontalScrollview,通过设置Adapter来填充视图,使得在Android中添加数据更加灵活。此控件需要有一个子视图,并且在添加数据时会抛出异常,如果试图添加的数据为零。
项目中需要一个水平移动的控件 ,不知道是不是由于冲突,用HorizontalListview的时候 出现很卡的情况 ,并且 好像 和外层的刷新控件冲突 ,然后出现 不能滑动 ,然后看见还有个HorizontalScrollview 这个控件 ,感觉滑动 的时候 比HorizontalListview连贯 ,没出现卡的情况,但是有个问题 是添加数据的时候 感觉比较不好,要么是直接手动添加布局,看网上比价好的做法是     <com.krish.horizontalscrollview.CenterLockHorizontalScrollview
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

    </com.krish.horizontalscrollview.CenterLockHorizontalScrollview>



public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.setSmoothScrollingEnabled(true);

    }

    public void setAdapter(Context context, CustomListAdapter mAdapter) {

        try {
            fillViewWithAdapter(mAdapter);
        } catch (ZeroChildException e) {

            e.printStackTrace();
        }
    }

    private void fillViewWithAdapter(CustomListAdapter mAdapter)
            throws ZeroChildException {
        if (getChildCount() == 0) {
            throw new ZeroChildException(
                    "CenterLockHorizontalScrollView must have one child");
        }
        if (getChildCount() == 0 || mAdapter == null)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        parent.removeAllViews();

        for (int i = 0; i < mAdapter.getCount(); i++) {
            parent.addView(mAdapter.getView(i, null, parent));
        }

    }


这个做法是比较灵活的  ,  添加数据的时候直接设置一个adapter添加入进去 ,更觉又学到了 ,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值