Android ScrollView水平自动滚动

自定义ScrollView和Adapter

public class MyHorizontalScrollView extends HorizontalScrollView {

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setSmoothScrollingEnabled(true);
    }

    public void setAdapter(Context context, MyHorizontalScrollViewAdapter mAdapter) {
        try {
            fillAdapter(mAdapter);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    private void fillAdapter(MyHorizontalScrollViewAdapter mAdapter) throws Exception {
        if (getChildCount() == 0) {
            throw new Exception("CmbHorizontalScrollView 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));
        }
    }
}
public class MyHorizontalScrollViewAdapter extends BaseAdapter {

    private ArrayList<ArrayList<Integer>> list;
    private Context context;

    public MyHorizontalScrollViewAdapter(Context context,ArrayList<ArrayList<Integer>> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();

    }

    @Override
    public ArrayList<Integer> getItem(int position) {
        return list.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.scroll_item_view, null);
            holder = new ViewHolder();
            holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
            holder.image2 = (ImageView) convertView.findViewById(R.id.image2);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.image1.setImageResource(list.get(position).get(0));
        holder.image2.setImageResource(list.get(position).get(1));

        return convertView;

    }

    class ViewHolder{
        ImageView image1;
        ImageView image2;
    }

}

item布局是垂直的两个imageView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:padding="5dp">

    <ImageView 
        android:id="@+id/image1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:src="@drawable/a"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_marginBottom="2dp"
        />
    <ImageView 
        android:id="@+id/image2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:src="@drawable/b"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_marginTop="2dp"
        />

</LinearLayout>

在布局中添加自定义的scrollview:

 <com.yunmo.scrolldemo.MyHorizontalScrollView
                android:id="@+id/gallery_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="21dip"
                android:scrollbars="none" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </LinearLayout>
            </com.yunmo.scrolldemo.MyHorizontalScrollView>

主要逻辑函数

public class MainActivity extends Activity {


    private TimerTask scrollerSchedule;
    private Timer scrollTimer = null;
    private int scrollPos = 0;
    /* gallery视图* */
    MyHorizontalScrollView galleryhl;
    MyHorizontalScrollViewAdapter adapter;
    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> listItem = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        galleryhl = (MyHorizontalScrollView) this
                .findViewById(R.id.gallery_view);
        int[] idS = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.b_top,R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.b_top,R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.b_top};
        for (int i = 0; i < idS.length; i+=2) {
            listItem.add(idS[i]);
            listItem.add(idS[i+1]);
            list.add(listItem);
        }
        adapter = new MyHorizontalScrollViewAdapter(this, list);
        galleryhl.setAdapter(this, adapter);
        adapter.notifyDataSetChanged();
        startAutoScrolling();
    }

    private void startAutoScrolling() {
        if (scrollTimer == null) {
            scrollTimer = new Timer();
            if (scrollerSchedule != null) {
                scrollerSchedule.cancel();
                scrollerSchedule = null;
            }
            scrollerSchedule = new TimerTask() {

                @Override
                public void run() {
                    moveScrollView();
                }
            };
            scrollTimer.schedule(scrollerSchedule, 30, 42);
        }
    }
    private void moveScrollView() {
        if (scrollPos > galleryhl.getScrollX() && (scrollPos != 1)) {
            galleryhl.smoothScrollTo(0, 0);
            scrollPos = 0;
        } else {
            scrollPos = (int) (galleryhl.getScrollX() + 1.0);
            galleryhl.smoothScrollTo(scrollPos, 0);
        }
    }

    /**
     * 取消滚动
     */
    private void cancelAutoScroll() {
        if (null != scrollTimer) {
            scrollTimer.cancel();
            scrollTimer = null;
        }
        if (null != scrollerSchedule) {
            scrollerSchedule.cancel();
            scrollerSchedule = null;
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值