您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android中如何使用九宮格可分頁加載控件,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
基本思路是viewpager+gridview,每一頁viewpager視圖有一個gridview,gridview加載九格。所以要保證數據的準確性。數據以List< List < T>>的形式。控件實現了高度自適應,根據控件的個數,計算控件的高度。內部的viewpager和gridview視圖都以動態創建,無需新建一個xml文件,整個控件只需要一個item的布局文件。
使用方法
使用時可以先請求一個接口,需初始化數據,數據初始化需要根據總個數初始化所有數據,在每次分頁加載的時候,再通過請求當頁的數據,更改數據,刷新頁面。
viewPager = findViewById(R.id.ninePages); //數據初始化 List<List<TestEntity>> lists = new ArrayList<>(); for (int i = 0; i < 6; i++) {//頁數 List<TestEntity> list1 = new ArrayList<>(); for (int j = 0; j < 9; j++) {//每頁個數 TestEntity entity = new TestEntity(); entity.setText("j=" + j + "/i=" + i); list1.add(entity); } lists.add(list1); } //設置不每頁重復加載 viewPager.setEveryRequest(false); //設置分頁加載 viewPager.setPagingLoad(true); viewPager.setListener(new RequestDataListener<TestEntity>() { @Override public void request(int position) {//分頁加載 List<TestEntity> changeList =new ArrayList<>(); for (int i = 0; i < 9; i++) { TestEntity testEntity = new TestEntity(); testEntity.setText("change" + i); changeList.add(testEntity); } //數據刷新 viewPager.dataInvalidate(position,changeList); } @Override public int gridViewColumn() {//gridview列數 return 3; } @Override public View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent, List<TestEntity> list) { ViewHolder viewHolder = null; if(convertView == null){ convertView = inflater.inflate(R.layout.item_gridview,parent,false); viewHolder = new ViewHolder(); viewHolder.textView = convertView.findViewById(R.id.textview); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView.setText(list.get(position).getText()); return convertView; } }); viewPager.setList(lists);class ViewHolder{ private TextView textView; }
注意:viewPager.setList(lists);要在最后調用
視圖控件:
public class NinePagesPagination<T> extends LinearLayout { private View mView; private ViewPager viewPager; private Context context; private List<List<T>> list = new ArrayList<>(); //已經請求過的,不在重復請求 private List<Integer> hasAdded = new ArrayList<>(); //是否每次請求 private boolean isEveryRequest; //是否進行分頁加載 private boolean isPagingLoad; private RequestDataListener listener; private MyViewPagerAdapter adapter; private ViewPager.OnPageChangeListener pageChangeListener; public NinePagesPagination(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; viewPager = new ViewPager(context); LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); viewPager.setLayoutParams(params); mView = viewPager; addView(mView); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(listener!=null && listener.gridViewColumn() != 0){ int column = listener.gridViewColumn(); int row = (int) Math.ceil((double) list.get(0).size()/column); int heightMeasureSpec2 = MeasureSpec.makeMeasureSpec(row*getWidth()/column, MeasureSpec.AT_MOST); setMeasuredDimension(widthMeasureSpec,heightMeasureSpec2); } } public void setListener(RequestDataListener listener) { this.listener = listener; } public void setPageChangeListener(ViewPager.OnPageChangeListener pageChangeListener) { this.pageChangeListener = pageChangeListener; } public void setEveryRequest(boolean everyRequest) { isEveryRequest = everyRequest; } public void setPagingLoad(boolean pagingLoad) { isPagingLoad = pagingLoad; } public void setList(List<List<T>> list) { this.list = list; initView(); invalidate(); } private void initView() { adapter = new MyViewPagerAdapter(context, list,listener); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(pageChangeListener != null){ pageChangeListener.onPageScrolled(position,positionOffset,positionOffsetPixels); } } @Override public void onPageSelected(int position) { if(pageChangeListener != null){ pageChangeListener.onPageSelected(position); } if (isPagingLoad) {//分頁加載 if (isEveryRequest) {//每次都請求 adapter.setCurrentPosition(position); listener.request(position); } else {//請求過一次將不在重復請求 if (!hasAdded.contains(position)) { hasAdded.add(position); adapter.setCurrentPosition(position); listener.request(position); } } } } @Override public void onPageScrollStateChanged(int state) { if(pageChangeListener != null){ pageChangeListener.onPageScrollStateChanged(state); } } }); } //數據刷新 public void dataInvalidate(int position,List<T> changeList){ list.set(position, changeList); adapter.notifyDataSetChanged(); }}
viewpager適配器:
public class MyViewPagerAdapter<T> extends PagerAdapter { private Context context; private List<List<T>> list; private LayoutInflater inflater; private MyGridViewAdapter adapter; private int currentPosition; private RequestDataListener listener; public MyViewPagerAdapter(Context context, List<List<T>> list,RequestDataListener listener) { this.context = context; this.list = list; this.listener = listener; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { GridView view = new GridView(context); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); view.setLayoutParams(params); view.setTag(position); if(listener!=null && listener.gridViewColumn() != 0){ view.setNumColumns(listener.gridViewColumn()); } adapter = new MyGridViewAdapter(context, list.get(position),listener); view.setAdapter(adapter); container.addView(view); return view; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @Override public int getItemPosition(@NonNull Object object) { //viewpager立即刷新頁面 if (list != null && list.size() == 0) { return POSITION_NONE; } View view = (View) object; if (currentPosition == (Integer) view.getTag()) { return POSITION_NONE; } else { return POSITION_UNCHANGED; } } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); if(adapter != null){ adapter.notifyDataSetChanged(); } } public void setCurrentPosition(int currentPosition) { this.currentPosition = currentPosition; }}
gridview適配器
public class MyGridViewAdapter<T> extends BaseAdapter{ private Context context; private List<T> list; private LayoutInflater inflater; private RequestDataListener listener; public MyGridViewAdapter(Context context, List<T> list,RequestDataListener listener){ this.context = context; this.list = list; this.listener = listener; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = listener.getView(inflater,position,convertView,parent,list); int width; //計算item的寬 if(listener != null &listener.gridViewColumn() != 0){ width = parent.getWidth()/listener.gridViewColumn(); }else{ width = parent.getWidth()/3; } //設置item的寬高相等 convertView.setLayoutParams(new AbsListView.LayoutParams(width,width)); return convertView; }}
暴露的接口
public interface RequestDataListener<T> { void request(int position); //gridView列數 int gridViewColumn(); View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent, List<T> list);}
關于“android中如何使用九宮格可分頁加載控件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。