Aandroid adds ListView and aandroidviewpager to ViewPager.
In a recent project, you have a page that needs to be slide horizontally and vertically. For example, you can use the option card to select the desired page, you can also achieve this through sliding the screen fingers, and the content of each page corresponds to a ListView that can slide up and down, so the final effect is that the page can slide up and down, slide left and right between pages.
There are two solutions for this effect. One is to use TabActivitiy to set the Tab to the TabActibitiy flag and load the required pages to the tabs, however, this method will re-create a new Activity instance every time you switch the tab, so the switching will be slow when there are many interfaces, at the same time, this method cannot achieve screen finger sliding to switch the interface.
The other is what we will talk about today. Add the ListView in ViewPager.
ViewPager is a software package officially provided by google that is compatible with android devices of lower versions,
The main function of ViewPager is to slide the views left and right. You can find the tutorials for adding static pages to ViewPaager on the Internet. Here is a brief introduction: (For details, refer to other ViewPager tutorials)
1. Add components to the layout File
<android.support.v4.view.ViewPager android:id="@+id/heroPager" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
Like other android components, you only need to declare a component and specify its basic attributes.
2. Load the tab to display
<Pre name = "code" class = "java" style = "color: rgb (70, 70, 70); font-size: 14px; line-height: 21px; text-indent: 28px; "> <span style =" font-family:; "> // This method moves the current view from viewPager. </Span>
Public void destroyItem (View container, int position, Object object) {}// this method returns an Object, which indicates which object PagerAapter chooses to put in the current ViewPager.
Public Object instantiateItem (View container, int position ){}
// Returns the current page number.
Public int getCount () {return mListViews. size ();}
// This method determines whether the interface is generated by this object.
Public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}
4. Add an adapter for viewPager:
viewPager.setAdapter(new HeroPagerAdapter(viewList));
Okay, the above is the method for adding a regular static interface to ViewPager. For listview, you need to set the adapter. listview is also a dynamic component, the following describes how to add a dynamic listview in ViewPager.
Similarly, add the ViewPager component to the layout file.
Step 2 is also the most important step.
The previous operations are the same as those above. Declare a LayoutInflater object first:
LayoutInflater inflater = getLayoutInflater();
Obtain the viewPager component:
viewPager = (ViewPager) findViewById(R.id.heroPager);
Create a list object:
viewList = new ArrayList<View>();
Here, I have tried the following methods, which are all incorrect. I hope you will pay attention to them:
Directly convert the View object to the ListView object by force type conversion:
ListViewView1=(ListView)Inflater. inflate (R. layout.Hero_list,Null);
This method can be compiled, but the system reports a type conversion error at runtime!
You can directly use the findViewbyId method to obtain the listview object through the listview component ID:
ListViewListView1=(ListView) findViewById (R. id.HeroList);
This method can also be used during compilation. This step does not cause problems during runtime. However, when the SimpleAdpater adapter is added for listview, a null pointer exception is thrown, when debugging mode reaches this step, we will observe that the listview obtained through the findViewbyId method is an empty object.
The original findviewById is the method in the View class. The default call should actually be:
This. findviewById ();
Since the declaration of the listview "R. id. herolist" in the Code is not in the xml layout of the current viewPager, The findviewById method cannot be used to obtain the listview instance.
Now that you know the findviewById () principle, it's easy! The LayoutInflater object is used to instantiate the layout of the listview, and then the listview instance is obtained through the findviewById method of the view.
ViewView1=Inflater. inflate (R. layout.Hero_list,Null);
ViewView2=Inflater. inflate (R. layout.Hero_list,Null);
ListViewListview1=View1.findViewById (R. id.HeroList);
ListViewListview2=View1.findViewById (R. id.HeroList);
Note that the view corresponds to the hero_list layout and the listview corresponds to the herolist layout. The two are different.
It can also be simplified as follows:
ListView listView1 = (ListView) (inflater. inflate (R. layout. hero_list, null). findViewById (R. id. heroList );
ListView listView2 = (ListView) (inflater. inflate (R. layout. hero_list, null). findViewById (R. id. heroList );
The rest of step 3 and Step 4 are the same as above, rewrite the PagerAdapter class and add the adapter.
But don't forget to add an adapter to each listview. I believe you are familiar with the process of adding SimpleAdapter to listview. I will not go into details here. go directly to the Code:
SimpleAdapter simpleAdapter_Wu = new SimpleAdapter(this, herolist_wu, R.layout.hero_info, new String[]{"heroImage", "heroName"}, new int[]{R.id.heroImage, R.id.heroName}); SimpleAdapter simpleAdapter_Shu = new SimpleAdapter(this, herolist_shu, R.layout.hero_info, new String[]{"heroImage", "heroName"}, new int[]{R.id.heroImage, R.id.heroName}); listView1.setAdapter(simpleAdapter_Wu); listView2.setAdapter(simpleAdapter_Shu);
Here, it is different from the above !!
The inflater method of the LayoutInflater object can only instantiate one view object. Because android cannot directly declare an xml layout as a Listview, you cannot directly use the inflater method to instance the listview object.
In this way, the two listview lists are successfully added to ViewPager. The following is: (the level is limited and I don't know how to do it dynamically)
At the beginning of this process, I also searched the internet for methods to add listview to viewPager, but I did not find any relevant tutorials. After a day of exploration, I finally made it, here, I would like to share with you some help to those who need similar results.