In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the pageable loading control in android. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The basic idea is viewpager+gridview, where each page of the viewpager view has a gridview,gridview loaded with nine frames. So it is necessary to ensure the accuracy of the data. Data in List
< List < T>The form of >. The control is highly adaptive and calculates the height of the control according to the number of controls. Internal viewpager and gridview views are created dynamically, there is no need to create a new xml file, the entire control only needs an item layout file.
Usage
When using it, you can first request an API to initialize the data. Data initialization needs to initialize all the data according to the total number. When each paging load, you can request the data of the current page to change the data and refresh the page.
ViewPager = findViewById (R.id.ninePages); / / data initialization List lists = new ArrayList (); for (int I = 0; I < 6; ipages +) {/ / pages List list1 = new ArrayList (); for (int j = 0; j < 9; jpages +) {/ / pages TestEntity entity = new TestEntity (); entity.setText ("j =" + j + "/ I =" + I); list1.add (entity);} lists.add (list1) } / / set not to load viewPager.setEveryRequest (false) repeatedly per page; / / set paged load viewPager.setPagingLoad (true); viewPager.setListener (new RequestDataListener () {@ Override public void request (int position) {/ / paged load List changeList = new ArrayList (); for (int I = 0; I < 9; iTunes +) {TestEntity testEntity = new TestEntity (); testEntity.setText ("change" + I); changeList.add (testEntity);} / / data refresh viewPager.dataInvalidate (position,changeList) } @ Override public int gridViewColumn () {/ / gridview number of columns return 3;} @ Override public View getView (LayoutInflater inflater, int position, View convertView, ViewGroup parent, List list) {ViewHolder viewHolder = null; if (convertView = = null) {convertView = inflater.inflate (R.layout.itemology gridviewparentdepartment 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;}
Note: viewPager.setList (lists); to call the
View controls:
Public class NinePagesPagination extends LinearLayout {private View mView; private ViewPager viewPager; private Context context; private List list = new ArrayList (); / / the request private List hasAdded = new ArrayList () is not repeated for each request; / / whether to load 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 (listeners) {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) {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) {/ / paged load if (isEveryRequest) {/ / request adapter.setCurrentPosition (position); listener.request (position);} else {/ / request once will not repeat request 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) });} / / data refresh public void dataInvalidate (int position,List changeList) {list.set (position, changeList); adapter.notifyDataSetChanged ();}}
Viewpager Adapter:
Public class MyViewPagerAdapter extends PagerAdapter {private Context context; private List list; private LayoutInflater inflater; private MyGridViewAdapter adapter; private int currentPosition; private RequestDataListener listener; public MyViewPagerAdapter (Context context, List 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 (listeners) {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 immediately refresh the page 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 adapter
Public class MyGridViewAdapter extends BaseAdapter {private Context context; private List list; private LayoutInflater inflater; private RequestDataListener listener; public MyGridViewAdapter (Context context, List 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; / / calculate the width if of item (listener! = null & listener.gridViewColumn ()! = 0) {width = parent.getWidth () / listener.gridViewColumn ();} else {width = parent.getWidth () / 3; / / set convertView.setLayoutParams (new AbsListView.LayoutParams (width,width)); return convertView;}
Exposed interface
Public interface RequestDataListener {void request (int position); / / number of gridView columns int gridViewColumn (); View getView (LayoutInflater inflater, int position, View convertView, ViewGroup parent, List list);}
This is the end of this article on "how to use the pageable loading control in android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.