Recently did a project needs to be done viewpager this one, where one needs to slide and then load the data that page, and later made out, put their ideas to share with you.
In fact, you can put viewpager as a listview like to do is load the data in the adapter, first set the style for each item of view. The main idea of this paper is to think of all the item into view a list views,
By instantiateItem (ViewGroup container, int position) method and then views the list each set to each item in the view, when slid into each page will be executed setPrimaryItem (View Container, int
position, Object Object) method, where Download Data.
This is mainly adaper constructor acquired context and pass some parameters
public ViewPagerAdapter(Context context,float size,AQuery aq,String categoryId){ mInflater = LayoutInflater.from(context); this.context = context; textSize = size; this.aq = new AQuery(this.context); this.categoryId = categoryId; }
Set in activity in adpaer
pagerAdapter = new ViewPagerAdapter(getActivity(),mTextSize,aq, categoryId); mPager.setAdapter(pagerAdapter); // all id list passed to the adapter pagerAdapter.setNewsId(newsIdList); // viewpager to display the first page itemposition mPager.setCurrentItem(itemPosition);
Below each item will be loaded into a pattern of view (ArrayList <View>) views of, (boolean []) positionValue will load situation daily news, a false start are expressed not loaded, the following key is views.add (setView ()); the rest do not care too much
/** * Set the number of news, called after NewsDetailsContentFragment set adpater * @param list */ public void setNewsId(ArrayList<String> list) { idlList = list; positionValue = new boolean[idlList.size()]; eachItemPicNum = new int[idlList.size()]; for (int i = 0; i < positionValue.length; i++) { positionValue[i] = false; eachItemPicNum[i] = 0; } for (int i = 0; i < idlList.size(); i++) { views.add(setView()); } shareStr = new String[views.size()]; shareImg = new String[views.size()]; notifyDataSetChanged(); }
Here is setview method returns view, we view it kept the view viewpagerHolder each item as a class to store some textview internal controls and gallery name
private View setView() { // TODO Auto-generated method stub final ViewPagerHolder holder = new ViewPagerHolder(); View view = mInflater.inflate(R.layout.news_datails_viewpager_item, null); holder.mTitle = (TextView)view.findViewById(R.id.text_news_detail_title); holder.mAuthor = (TextView)view.findViewById(R.id.text_news_detail_author); holder.mData = (TextView)view.findViewById(R.id.text_news_detail_data); holder.mGallery = (Gallery)view.findViewById(R.id.gallery); holder.mGallery.setVisibility(View.GONE); return view; }
Here is the crucial part
@Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public void destroyItem(View container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager)container).removeView(views.get(position)); } /** * Jump to methods each page must be executed */ @Override public void setPrimaryItem(View container, int position, Object object) { // this position assignment to a global variable, through this will know which page to a slide setPosition(position); // request only once to ensure that each news network, after the implementation of the method is not in execution getNewsData if (positionValue[position] == false) { // this is to get the data networking getNewsData(categoryId, position); if (Logs.IS_DEBUG) { Logs.v ( "guo", "network request ............." + itemPosttion); } positionValue[position] = true; } } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub ((ViewPager)container).addView(views.get(position)); // for each item of view is just to store the views of view return views.get(position); } @Override public void startUpdate(ViewGroup container) { // TODO Auto-generated method stub super.startUpdate(container); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; }
setPrimaryItem () This method is the key point, a slide will perform this method, by which the position can know which index to slide to, with this index can be a good solution to dynamically load the data. Here is the method of setting data
if (news.getTitle() != null) { ((TextView)views.get(itemPosttion).findViewById(R.id. text_news_detail_title)).setText(news.getTitle()); } //set author if (!isEmpty(news.getAuthor()) || !news.getAuthor().equals("null")) { ((TextView)views.get(itemPosttion).findViewById(R.id. . Text_news_detail_author)) setText ( "PRC"); }else { ((TextView)views.get(itemPosttion).findViewById(R.id. . Text_news_detail_author)) setText ( "PRC"); } // set data if (news.getPublishDate() != null) { String publishDate = setDataFormat(news.getPublishDate()); ((TextView)views.get(itemPosttion).findViewById(R.id. text_news_detail_data)).setText(publishDate); }