当前位置: 移动互联网学院 > Android开发 > Android轮播图的简单实现
Android轮播图的简单实现 时间:2017-04-17     来源:Android开发学习网

今天为大家分享一下Android中轮播图的实现方法,主要的思想就采用ViewPager和ScrollGater实现,图片加载用的Imageloader,也可以换其他的,比如Glide.具体封装的组件源码,这里只说下用法,首先上布局文件。

Android轮播图的简单实现

<LinearLayout xmlns:tools="//schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

<LinearLayout

android:id="@+id/root"

android:layout_width="match_parent"

android:layout_height="180dp"

android:orientation="vertical" >

<com.example.shuffviewdemo.ShufflingView

android:id="@+id/shuffling_view"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

</com.example.shuffviewdemo.ShufflingView>

</LinearLayout>

</LinearLayout>

初始化ShufflingView,设置des可见,轮播的指示器在底部。设置点击事件监听。

mShufflingView = (ShufflingView) findViewById(R.id.shuffling_view);

mShufflingView.setContentDesVisibility(View.VISIBLE);

mShufflingView.setDotGravity(Gravity.BOTTOM);

mShufflingView.setOnItemClickListener(mOnShufflingItemClickListener);

bannerList = new ArrayList();

loadShuffingViewData();

设置shufflingView 的beans接收的是一个arraylist对象。

ShufflingItemBean shufflingItemBean=new ShufflingItemBean();

shufflingItemBean.setImg("//img3.imgtn.bdimg.com/it/u=4227020988,3565099621&fm=21&gp=0.jpg");

shufflingItemBean.setUrl("www.baidu.com");//点击跳转到webview

bannerList.add(shufflingItemBean);

bannerList.add(shufflingItemBean);

bannerList.add(shufflingItemBean);

mShufflingView.updateDatas(bannerList);1234567812345678

shufflingView 的监听,具体的跳转和类型,根据需求组件设置。

private void actionFromType(Context context, ShufflingItemBean item) {

if (item.getType().equals("-1")) {

return;

} else if (item.getType().equals("1")) {// 交易贴

} else if (item.getType().equals("2")) {// 外部链接

} else if (item.getType().equals("3")) {// 资讯贴

} else if (item.getType().equals("4")) {// 应援贴

} else if (item.getType().equals("5")) {//

//Intent intent = new Intent(context, WebViewActivity.class);

//intent.putExtra("url",

//item.getUrl());

//intent.putExtra("type", 1);

//context.startActivity(intent);

} else {// 其他

}

}
以上就是一个简单的android轮播图实现方法,更多Android开发技术文章,请持续关注我们。