效果
单个item样式
多个item样式
横向item
自定义viewpager——可开关切换动画
public class NoAnimationViewPager extends ViewPager {public NoAnimationViewPager(Context context) {super(context);}public NoAnimationViewPager(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic void setCurrentItem(int item, boolean smoothScroll) {super.setCurrentItem(item, smoothScroll);}@Overridepublic void setCurrentItem(int item) {//去除页面切换时的滑动翻页效果super.setCurrentItem(item, true);}
}
viewpager的adapter
/*** 作者:created by meixi* 邮箱:1085220040@qq.com* 日期:2020/1/6 15:40*/
public class ViewPagerAdapter extends PagerAdapter {private List<GridView> gridList;public ViewPagerAdapter() {gridList = new ArrayList<>();}public void add(List<GridView> datas) {if (gridList.size() > 0) {gridList.clear();}gridList.addAll(datas);notifyDataSetChanged();}@Overridepublic int getCount() {return gridList.size();}@Overridepublic int getItemPosition(Object object) {return POSITION_NONE;}@Overridepublic boolean isViewFromObject(View view, Object object) {return view == object;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(gridList.get(position));return gridList.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}
}
activity调用即可
private NoAnimationViewPager vp;
private ViewPagerAdapter mAdapter;
private List<GridView> gridList = new ArrayList<>();
private int WHAT_AUTO_PLAY = 1000;
private int autoPlayDuration = 2000;//刷新间隔时间
private int num = 0;
private boolean ifauto = true;
private List<GongXuEntity> mGongXuEntities = new ArrayList<>();
protected Handler mHandler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(Message msg) {if (msg.what == WHAT_AUTO_PLAY) {Log.e("lgq", "==33==" + ifauto);if (ifauto){num++;if (num==0){vp.setCurrentItem(num,false);}else {vp.setCurrentItem(num,true);}}if (mGongXuEntities.size() - 1 == num) {num = -1;}mHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration);}return false;}
});
mAdapter = new ViewPagerAdapter();
vp.setAdapter(mAdapter);
getdata();
showHostsj();
vp.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {ifauto = false;}if (event.getAction() == MotionEvent.ACTION_UP) {ifauto = true;}return false;}});
//圆点指示器mHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, autoPlayDuration);
public void getdata() {for (int a = 0; a < 6; a++) {GongXuEntity entity = new GongXuEntity();entity.setCity("城市" + a);if (a % 2 == 0) {entity.setSupplyDemand("0");} else {entity.setSupplyDemand("1");}entity.setContent("领道风味" + a);mGongXuEntities.add(entity);}
// mGongXuEntities.add(.......省略);}public void showHostsj() {for (int j = 0; j < mGongXuEntities.size(); j++) {GridView gridView = new GridView(this);HomeGridViewAdapter adapter = new HomeGridViewAdapter(mGongXuEntities, j);gridView.setTag(j);gridView.setNumColumns(1);gridView.setAdapter(adapter);gridList.add(gridView);gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {int index = Integer.parseInt(parent.getTag().toString()) * 1 + position;Log.i("lgq", "ii====" + index);//todo}});}mAdapter.add(gridList);}