自定义viewpager显示内容的风格,viewpager定时循环

今天实现的效果图:


对,这是我们经常开发用的viewpager,但在平时开发中,可能不是很注意,viewpager还可以这样玩法,可以在viewpager那里自定义自己项目中想要的效果。

思路:

1、自定义自己要实现视图view

2、在viewpager数据源中直接使用自己自定义的视图view


具体看代码,这里贴出核心代码:

自定义视图抽象类:

/**
* Created by user on 2016/4/29.
* 自定义一个抽象类(主要是模拟activity生命周期来实现)
*/
public abstract class BaseView {
protected Context mContext;
protected Activity mActivity;
protected View view;
protected boolean isAdded;

public BaseView() {
}

public BaseView(Context mContext, Activity activity) {
this.mContext = mContext;
this.mActivity = activity;
isAdded = false;
findViewById();
setListener();
init();
}


public View getView() {
if (view.getLayoutParams() == null) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout
.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
view.setLayoutParams(params);
}
return view;
}

public View findViewById(int id) {
return view.findViewById(id);
}

protected abstract void findViewById();

protected abstract void setListener();

protected abstract void init();

public abstract void onResume();

public abstract void onPause();

public void show() {
view.setVisibility(View.VISIBLE);
onResume();
}

public void hide() {
view.setVisibility(View.INVISIBLE);
onPause();
}

public boolean isAdded() {
return mActivity != null && isAdded;
}

public void setAdded() {
isAdded = true;
}

}

这个抽象类主要是模拟activity生命周期来定义的,供之后可以像activity生命周期那样使用


核心代码,自定义实现抽象类的实现类

/**
* Created by hjz on 2016/4/29.
* 实现自己想要的效果view
*/
public class CustomContentViewImp extends BaseView{
private GridView gridView;

private List<BusinessBean> businessBeanList = new ArrayList<>();
private int currentPage;

public CustomContentViewImp(Context context, Activity activity, List<BusinessBean> businessBeanList, int currentPage) {
//调用,开始自定义view的生命周期
super(context,activity);
this.businessBeanList = businessBeanList;
this.currentPage = currentPage;
loadData();
}

private void loadData() {
GridViewAdapter GVadapter = new GridViewAdapter(mContext,mActivity,
businessBeanList.subList((currentPage-1)*6,(businessBeanList.size()>currentPage*6)?currentPage*6:businessBeanList.size()));
gridView.setAdapter(GVadapter);
}

@Override
protected void findViewById() {
view = View.inflate(mContext, R.layout.custom_content_view,null);
gridView= (GridView) findViewById(R.id.gv_content);
}

@Override
protected void setListener() {

}

@Override
protected void init() {

}

@Override
public void onResume() {

}

@Override
public void onPause() {

}
}

自定义核心类主要是实现自己想要的效果图,它类似activity生命周期那样提我们使用


主activity

/**
* Created by hjz on 2016/4/29.
*
* 思路:
* 在自定义视图view 中实现我们想要的效果图
* 数据源中使用一个自定义的视图View
* 具体看代码分析
*
*/
public class MainActivity extends Activity {
/**
* 控件区域
*/
private ViewPager mViewPager;
private LinearLayout mPointGroup;
private ViewPagerAdapter viewPagerAdapter;

/**
* 数据区域
*/
//在数据源中使用自定义view
private List<BaseView> dataList = new ArrayList<BaseView>();
private List<BusinessBean> businessBeanList = new ArrayList<BusinessBean>(); //储存自己业务数据list
private int lastPostion; //记录上个页面的位置
private int pageTotal; //总页数
private boolean isRunning = false; //是否启动viewpager自动切换(true自动,false手动)


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
setLister();
initData();
}

private void findView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mPointGroup = (LinearLayout) findViewById(R.id.point_group);
}

private void setLister() {
//也可以将这个滑动监听事件写在adapter里面
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

/**
* 页面正在滑动时回调
* @param position
* @param positionOffset
* @param positionOffsetPixels
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

/**
* 页面切换后回调
* @param position 新页面的位置
*/
@Override
public void onPageSelected(int position) {
position = position%pageTotal;

//改变指示点的状态,把当前点enable状态设置为true
mPointGroup.getChildAt(position).setEnabled(true);
//把上一个点enable状态设置为false
mPointGroup.getChildAt(lastPostion).setEnabled(false);
lastPostion = position;
}

/**
* 页面状态发生变化时回调
* @param state
*/
@Override
public void onPageScrollStateChanged(int state) {

}
});
}

private void initData() {
BusinessBean bean = null;
//构造一些 业务中的数据
for (int i=0; i<20; i++){
bean = new BusinessBean();
bean.setName("内容"+i);
businessBeanList.add(bean);
}

//这里设置每个viewpager显示6组数据
int pageSize = 6;
//根据 业务list 计算出总viewpager 页数
int busBeanListSize = businessBeanList.size();
pageTotal = (busBeanListSize%pageSize==0) ? busBeanListSize/pageSize :
(busBeanListSize/pageSize)+1;

//动态添加底部指示点 和 实例化对应自定义效果viw
BaseView baseView = null;
for (int i=0; i<pageTotal; i++){
//实例化 自定义内容view
baseView = new CustomContentViewImp(this,MainActivity.this,businessBeanList,(i+1));
dataList.add(baseView);

//动态添加指示点
ImageView pointIv = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//设置指示点间距
params.rightMargin = 20;
pointIv.setLayoutParams(params);
pointIv.setBackgroundResource(R.drawable.point_bg);
//一开始默认第一个指示点 为选中
if (i==0){
pointIv.setEnabled(true);
}else{
pointIv.setEnabled(false);
}
mPointGroup.addView(pointIv);
}

viewPagerAdapter = new ViewPagerAdapter (this,dataList,pageTotal);
mViewPager.setAdapter(viewPagerAdapter);

//自动循环切换
isRunning = true;
//定时器
handler.sendEmptyMessageDelayed(0,2000);
}

private Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {
//viewpager 滑动到下一页
mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
if (isRunning){
handler.sendEmptyMessageDelayed(0,2000);
}
}
};

@Override
protected void onDestroy() {
super.onDestroy();
isRunning = false;
}
}

这里我们就比较熟悉了,主要实现将自定义的视图view添加到adapter(适配器)中,然后将这个适配器设置到viewpager中,最后加一个定时滑动功能,就实现上面的效果。


源码下载





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值