标签:android style blog http color os io 使用 java
需求:滑动展示页,能够使用本地数据,及获取服务器数据进行刷新操作,当滑动到最后一页时,结束当前activity,进入下一个activity;
效果图:
实现分析:
1.目录结构:
代码实现:
1.PosterView.java
package com.jjc.demo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; 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; import android.widget.Toast; 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; 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(){ 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(); } 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 { int pageState = 100; @Override public void onPageScrollStateChanged(int arg0) { /** * arg0:什么都没做 * arg1:正在滑动 * arg2:滑动完毕 */ pageState = arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { if(!posterList.isEmpty() && arg0 == (posterList.size()-1) && pageState == 1){ Toast.makeText(context, "最后一页", Toast.LENGTH_SHORT).show(); ((Activity)context).finish(); } } @Override public void onPageSelected(int state) { setCurDot(state); } } 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; } }
项目总结:
ViewPager的onPageChangeListener监听获取,当前的滑动状态;
http://www.cnblogs.com/sishuiliuyun/p/3962347.html
代码:http://pan.baidu.com/s/1gdBZTGv
标签:android style blog http color os io 使用 java
原文地址:http://www.cnblogs.com/sishuiliuyun/p/3962362.html