先上效果图:
slide.gif项目地址:
https://github.com/crazyqiang/SlidePager
欢迎star!☺
直接上代码 :
MainActivity .java:
public class MainActivity extends AppCompatActivity {
public static int item_grid_num = 12;//每一页中GridView中item的数量
public static int number_columns = 4;//gridview一行展示的数目
private ViewPager view_pager;
private ViewPagerAdapter mAdapter;
private List<DataBean> dataList;
private List<GridView> gridList = new ArrayList<>();
private CirclePageIndicator indicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initViews() {
//初始化ViewPager
view_pager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new ViewPagerAdapter();
view_pager.setAdapter(mAdapter);
dataList = new ArrayList<>();
//圆点指示器
indicator = (CirclePageIndicator) findViewById(R.id.indicator);
indicator.setVisibility(View.VISIBLE);
indicator.setViewPager(view_pager);
}
private void initDatas() {
if (dataList.size() > 0) {
dataList.clear();
}
if (gridList.size() > 0) {
gridList.clear();
}
//初始化数据
for (int i = 0; i < 60; i++) {
DataBean bean = new DataBean();
bean.name = "第" + (i + 1) + "条数据";
dataList.add(bean);
}
//计算viewpager一共显示几页
int pageSize = dataList.size() % item_grid_num == 0
? dataList.size() / item_grid_num
: dataList.size() / item_grid_num + 1;
for (int i = 0; i < pageSize; i++) {
GridView gridView = new GridView(this);
GridViewAdapter adapter = new GridViewAdapter(dataList, i);
gridView.setNumColumns(number_columns);
gridView.setAdapter(adapter);
gridList.add(gridView);
}
mAdapter.add(gridList);
}
首先是确定显示几页的数据,用总条数dataList.size() 和每页item的数目item_grid_num取余数,若能除尽,dataList.size() / item_grid_num的结果就是总页数;若除不尽,则总页数+1,即dataList.size() / item_grid_num+1.
new GridViewAdapter(dataList, i)初始化将总数据dataList的引用和要显示第几页通过构造方法传过去.
ViewPagerAdapter.java
class ViewPagerAdapter extends PagerAdapter {
private List<GridView> gridList;
ViewPagerAdapter() {
gridList = new ArrayList<>();
}
void add(List<GridView> datas) {
if (gridList.size() > 0) {
gridList.clear();
}
gridList.addAll(datas);
notifyDataSetChanged();
}
@Override
public int getCount() {
return gridList.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(gridList.get(position));
return gridList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
GridViewAdapter.java
class GridViewAdapter extends BaseAdapter {
private List<DataBean> dataList;
GridViewAdapter(List<DataBean> datas, int page) {
dataList = new ArrayList<>();
//start end分别代表要显示的数组在总数据List中的开始和结束位置
int start = page * MainActivity.item_grid_num;
int end = start + MainActivity.item_grid_num;
while ((start < datas.size()) && (start < end)) {
dataList.add(datas.get(start));
start++;
}
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int i) {
return dataList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View itemView, ViewGroup viewGroup) {
ViewHolder mHolder;
if (itemView == null) {
mHolder = new ViewHolder();
itemView=LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_gridview, viewGroup, false);
mHolder.iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
mHolder.tv_text = (TextView) itemView.findViewById(R.id.tv_text);
itemView.setTag(mHolder);
}
else {
mHolder = (ViewHolder) itemView.getTag();
}
DataBean bean = dataList.get(i);
if (bean != null) {
mHolder.iv_img.setImageResource(R.mipmap.group_icon);
mHolder.tv_text.setText(bean.name);
}
return itemView;
}
private class ViewHolder {
private ImageView iv_img;
private TextView tv_text;
}
}
通过start end确定要显示的数组在总数据List中的开始和结束位置 ,然后while循环在总数据List中拿到在start和end之间的数据,并把数据添加到新创建的ArrayList<DataBean> dataList中即可.
如有错误,欢迎批评指正!觉得文章对您有帮助,随手点个赞呗!
最后贴下项目地址:
https://github.com/crazyqiang/SlidePager
欢迎star!☺
网友评论