关闭

Android ScrollView水平自动滚动

标签: scrollview
470人阅读 评论(2) 收藏 举报
分类:

自定义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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:73693次
    • 积分:1664
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:23篇
    • 译文:7篇
    • 评论:67条
    友情链接