标签:android style blog http color os io 使用 java
需求:滑动展示页,能够使用本地数据,及获取服务器数据进行刷新操作,并实现页面自动切换;
效果图:
实现分析:
1.目录结构:
代码实现:
1.PosterView.java
package com.jjc.demo; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; public class PosterView extends FrameLayout{ private Context context; private ViewPager viewPager; private LinearLayout container; private PosterAdapter adapter; private List<Integer> posterList = new ArrayList<Integer>(); private ImageView imageView; // 底部小点的图片 private ImageView[] points; // 记录当前选中位置 private int currentIndex = 0; private int nextIndex; private int pageNum = 1; private Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { if (points!= null && points.length >0) { nextIndex = (currentIndex + 1) % points.length; // 设置当前页面 setCurDot(nextIndex); setCurView(nextIndex); handler.removeMessages(0); handler.sendEmptyMessageDelayed(0, 5000); } } }; public PosterView(Context context) { this(context, null); } public PosterView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater.from(context).inflate(R.layout.view_poster, this, true); initView(); } @Override protected void onDetachedFromWindow(){ handler.removeMessages(0); super.onDetachedFromWindow(); } private void initView(){ container = (LinearLayout) findViewById(R.id.container); viewPager = (ViewPager)findViewById(R.id.viewpager); viewPager.setOnPageChangeListener(new viewpagerListener()); adapter = new PosterAdapter(context, posterList); viewPager.setAdapter(adapter); } /** * 刷新海报页数据; * @param list */ public void refreshData(List<Integer> list){ if(list == null || list.isEmpty()){ return; } initPoint(list.size()); posterList.clear(); posterList.addAll(list); adapter.notifyDataSetChanged(); handler.removeMessages(0); handler.sendEmptyMessageDelayed(0, 5000); } private void initPoint(int num){ pageNum = num; container.removeAllViews(); if(num == 0){ return; } points = new ImageView[num]; for (int i = 0; i < num; i++) { imageView = new ImageView(context); imageView.setBackgroundResource(R.drawable.poster_point_selector); LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(12, 12); mLayoutParams.rightMargin = 8; mLayoutParams.leftMargin = 8; mLayoutParams.topMargin = 24; mLayoutParams.bottomMargin = 28; points[i] = imageView; // 设置位置tag,方便取出与当前位置对应 points[i].setTag(i); // 默认都设为灰色 points[i].setEnabled(false); // 给每个小点设置监听 points[i].setOnClickListener(new pointListener()); container.addView(points[i], mLayoutParams); } // 设置为白色,即选中状态 points[currentIndex].setEnabled(true); } private class viewpagerListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { // 设置底部小点选中状态 setCurDot(position); } } private class pointListener implements OnClickListener { @Override public void onClick(View v) { int position = (Integer) v.getTag(); setCurView(position); setCurDot(position); } } /** * 设置当前页面的位置 */ private void setCurView(int position) { if (position < 0 || position >= pageNum) { return; } viewPager.setCurrentItem(position); } /** * 设置当前的小点的位置 */ private void setCurDot(int positon) { if (positon < 0 || positon > pageNum - 1 || currentIndex == positon) { return; } points[positon].setEnabled(true); points[currentIndex].setEnabled(false); currentIndex = positon; } }
项目总结:
对于页面动态切换,采用handle进行切换;
代码:http://pan.baidu.com/s/1jJH1O
标签:android style blog http color os io 使用 java
原文地址:http://www.cnblogs.com/sishuiliuyun/p/3958954.html