Android自定义组合控件实现动态轮播图
2016-08-28 16:56
375 查看
动态轮播图,我们不管在哪里,都是能够看到的,像淘宝首页,饿了么,美团,百度外卖。所以,今天给大家带来Android动态轮播图的实现,废话不多说,直接进入主题。
先来说一下本篇文章实现的原理。主要通过google提供的v4包下的控件ViewPager实现的,具体实现看代码吧,有注解。那么,首先我们创建一个项目,命名为ViewPager。修改MainActivity中的代码。
然后修改activity_main.xml中的代码
然后在drawable中,创建selector_bg_point.xml
在drawable中,创建shape_bg_point_disable.xml
在drawable中,创建shape_bg_point_enable.xml
这样轮播图片就实现好了,具体源码下载请点击
先来说一下本篇文章实现的原理。主要通过google提供的v4包下的控件ViewPager实现的,具体实现看代码吧,有注解。那么,首先我们创建一个项目,命名为ViewPager。修改MainActivity中的代码。
public class MainActivity extends Activity implements OnPageChangeListener{ private ViewPager viewPager; private int[] imageResIds; private ArrayList<ImageView> imageViewList; private LinearLayout ll_point_container; private String[] contentDescs; private TextView tv_desc; private int previousSelectedPosition = 0; boolean isRunning = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化布局 View视图 initViews(); // Model数据 initData(); // Controller 控制器 initAdapter(); // 开启轮询 new Thread(){ public void run() { isRunning = true; while(isRunning){ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // 往下跳一位 runOnUiThread(new Runnable() { @Override public void run() { System.out.println("设置当前位置: " + viewPager.getCurrentItem()); viewPager.setCurrentItem(viewPager.getCurrentItem()+1); } }); } }; }.start(); } @Override protected void onDestroy() { super.onDestroy(); isRunning = false; } private void initViews() { viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setOnPageChangeListener(this);// 设置页面更新监听 // viewPager.setOffscreenPageLimit(1);// 左右各保留几个对象 ll_point_container = (LinearLayout) findViewById(R.id.ll_point_container); tv_desc = (TextView) findViewById(R.id.tv_desc); } private void initData() { // 初始化要显示的数据 // 图片资源id数组 imageResIds = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e}; // 文本描述 contentDescs = new String[]{ "巩俐不低俗,我就不能低俗", "扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" }; // 初始化要展示的5个ImageView imageViewList = new ArrayList<ImageView>(); ImageView imageView; View pointView; LayoutParams layoutParams; for (int i = 0; i < imageResIds.length; i++) { // 初始化要显示的图片对象 imageView = new ImageView(this); imageView.setBackgroundResource(imageResIds[i]); imageViewList.add(imageView); // 加小白点, 指示器 pointView = new View(this); pointView.setBackgroundResource(R.drawable.selector_bg_point); layoutParams = new LayoutParams(5, 5); if(i != 0) layoutParams.leftMargin = 10; // 设置默认所有都不可用 pointView.setEnabled(false); ll_point_container.addView(pointView, layoutParams); } } private void initAdapter() { ll_point_container.getChildAt(0).setEnabled(true); tv_desc.setText(contentDescs[0]); previousSelectedPosition = 0; // 设置适配器 viewPager.setAdapter(new MyAdapter()); // 默认设置到中间的某个位置 int pos = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % imageViewList.size()); // 2147483647 / 2 = 1073741823 - (1073741823 % 5) viewPager.setCurrentItem(5000000); // 设置到某个位置 } class MyAdapter extends PagerAdapter{ @Override public int getCount() { return Integer.MAX_VALUE; } // 3. 指定复用的判断逻辑, 固定写法 @Override public boolean isViewFromObject(View view, Object object) { // System.out.println("isViewFromObject: "+(view == object)); // 当划到新的条目, 又返回来, view是否可以被复用. // 返回判断规则 return view == object; } // 1. 返回要显示的条目内容, 创建条目 @Override public Object instantiateItem(ViewGroup container, int position) { System.out.println("instantiateItem初始化: " + position); // container: 容器: ViewPager // position: 当前要显示条目的位置 0 -> 4 // newPosition = position % 5 int newPosition = position % imageViewList.size(); ImageView imageView = imageViewList.get(newPosition); // a. 把View对象添加到container中 container.addView(imageView); // b. 把View对象返回给框架, 适配器 return imageView; // 必须重写, 否则报异常 } // 2. 销毁条目 @Override public void destroyItem(ViewGroup container, int position, Object object) { // object 要销毁的对象 System.out.println("destroyItem销毁: " + position); container.removeView((View)object); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 滚动时调用 } @Override public void onPageSelected(int position) { // 新的条目被选中时调用 System.out.println("onPageSelected: " + position); int newPosition = position % imageViewList.size(); //设置文本 tv_desc.setText(contentDescs[newPosition]); // for (int i = 0; i < ll_point_container.getChildCount(); i++) { // View childAt = ll_point_container.getChildAt(position); // childAt.setEnabled(position == i); // } // 把之前的禁用, 把最新的启用, 更新指示器 ll_point_container.getChildAt(previousSelectedPosition).setEnabled(false); ll_point_container.getChildAt(newPosition).setEnabled(true); // 记录之前的位置 previousSelectedPosition = newPosition; } @Override public void onPageScrollStateChanged(int state) { // 滚动状态变化时调用 } }
然后修改activity_main.xml中的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="160dp" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:padding="5dp" android:orientation="vertical" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:background="#66000000" > <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:singleLine="true" android:text="天王盖地虎, 天王盖地虎, 天王盖地虎, " /> <LinearLayout android:id="@+id/ll_point_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" ></LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
然后在drawable中,创建selector_bg_point.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable"/> <item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable"/> </selector>
在drawable中,创建shape_bg_point_disable.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="5dp" /> <solid android:color="@android:color/darker_gray" /> </shape>
在drawable中,创建shape_bg_point_enable.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="5dp" /> <solid android:color="#FFFFFF" /> </shape>
这样轮播图片就实现好了,具体源码下载请点击
相关文章推荐
- Android实现自定义轮播图片控件示例
- Android:自定义组合控件之动态删减编辑框
- Android进阶——自定义View之重写ViewGroup组合系统控件实现自定义ToolBar模板
- Android自定义View之组合控件实现类似电商app顶部栏
- Android实现自定义轮播图片控件详解
- android自定义组合控件图片轮播+小圆点+点击跳转广告页面
- 自定义组合控件,实现无限轮播
- android自定义组合控件图片轮播+小圆点+点击跳转广告页面
- Android进阶——自定义View之组合系统控件实现水珠形状的ItemView
- Android自定义组合控件的实现
- Android自定义组合控件之实现CheckBox变化
- android自定义控件(五) 自定义组合控件
- android UI进阶之自定义组合控件
- Android高手进阶教程(二十七)之---基于ViewFlipper实现的自定义新手指引控件.
- android - 自定义(组合)控件 + 自定义控件外观
- android - 自定义(组合)控件 + 自定义控件外观
- android自定义组合控件
- android UI进阶之自定义组合控件 推荐
- 自定义实现类似android主界面的滑屏换屏控件
- 【Android游戏开发二十三】自定义ListView【通用】适配器并实现监听控件!