Android custom control 5-basic implementation of carousel image advertisement ViewPager and androidviewpager
URL: http://www.cnblogs.com/wuyudong/p/5918021.html.
This article begins to implement the carousel image advertisement series. This article first slides the image (ViewPager) and the effect is as follows:
First, implement layout
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <RelativeLayout android: layout_width =" match_parent "android: layout_height =" 160dp "> <android. support. v4.view. viewPager android: id = "@ + id/viewpager" android: layout_width = "match_parent" android: layout_height = "match_parent"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_alignParentBottom = "true" android: background = "#66000000" android: gravity = "center_horizontal" android: orientation = "vertical" android: padding = "5dp"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: singleLine = "true" android: text = "Harmonious Society, harmonious society, harmonious Society, harmonious society "android: textColor =" @ android: color/white "/> <LinearLayout android: id =" @ + id/ll_point_container "android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: orientation = "horizontal"> </LinearLayout> </RelativeLayout>
Run the project with the following error:
09:08:44 09-28. 032: E/AndroidRuntime (5794): java. lang. runtimeException: Unable to start activity ComponentInfo {com. wuyudong. viewpager/com. wuyudong. viewpager. mainActivity}: android. view. inflateException: Binary XML file line #11: Error inflating class android. support. v4.view. viewPager
ViewPager is a very powerful control. Many applications use it to achieve cool results. However, in many casesError inflating class android. support. v4.view. ViewPagerThis problem,
The solution to this problem is simple: Right-click the project and selectBuild Path->Configure Build PathAnd then selectOrder and exportOption. StickAndroid-support-v4.jarAnd thenCleanUnder the project, the problem is solved after recompilation ~
Then write the logic code:
Package com. wuyudong. viewpager; import java. util. arrayList; import android. OS. bundle; import android. app. activity; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; public class MainActivity extends Activity {private ViewPager viewPager; private int [] imageResIds; private ArrayList <ImageView> imageViewList; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the Layout View initViews (); // Model data initData (); // Controller initAdapter ();} private void initViews () {viewPager = (ViewPager) findViewById (R. id. viewpager); imageResIds = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; imageViewList = new ArrayList <ImageView> (); ImageView imageView; for (int I = 0; I <imageResIds. length; I ++) {imageView = new ImageView (this); imageView. setBackgroundResource (imageResIds [I]); imageViewList. add (imageView) ;}} private void initAdapter () {//} private void initData () {// initialize viewPager. setAdapter (new MyAdapter ();} class MyAdapter extends PagerAdapter {@ Override public int getCount () {return imageViewList. size () ;}// 3. Specify the reusable judgment logic @ Override public boolean isViewFromObject (View view, Object object) {// when sliding to a new entry, return again, whether the view can be reused return view = object;} // 1. return the content of the entry to be displayed. Create the entry @ Override public Object instantiateItem (ViewGroup container, int position) {// container: ViewPager // position: ImageView imageView = imageViewList. get (position); // a. Add the view object to container. addView (imageView); // B. return the view object to the Framework. The adapter returns imageView. // It must be rewritten, otherwise, an exception is thrown.} // 2. Destroy the entry @ Override public void destroyItem (ViewGroup container, int position, Object object) {// the container of the object to be destroyed. removeView (View) object );}}}
After running the project, the effect is as follows: