The method of dynamically adding view and realizing pseudo infinite loop in Android Viewpager _android

Source: Internet
Author: User
Tags object object

This article illustrates the method of dynamically adding view and realizing pseudo infinite loops in Android Viewpager. Share to everyone for your reference, specific as follows:

The use of Viewpager, we are familiar with, it can achieve sliding between the left and right, there is a demand, that is, the number of pages loaded in Viewpager is not determined, but based on the amount of data to determine. A common application is to display a picture of the news in a detailed page of the news.

Now let's take a look at the code:

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "Match_ Parent "
  android:layout_height=" match_parent ">
  <android.support.v4.view.viewpager
    android:id= "@+id/list_pager"
    android:layout_width= "fill_parent"
    android:layout_height= "150DP"
    />
</ Relativelayout>

Fragment_page.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: gravity= "center"
  android:background= "#ffff44" >
  <textview
    android:id= "@+id/txt_num"
    Android:layout_width= "Fill_parent"
    android:layout_height= "fill_parent"
    android:text= "1"
    android: Textsize= "22DP"
    android:gravity= "center"/>
</LinearLayout>

Viewpageadapter.java

Import Android.support.v4.view.PagerAdapter;
Import Android.view.View;
Import Android.view.ViewGroup;
Import java.util.List;
/**
 * Created by CG on 2015/10/28.
 * * Public
class Viewpageadapter extends Pageradapter {
  private list<view> list_view;
  Public Viewpageadapter (list<view> list_view) {
    this.list_view = List_view;
  }
  @Override public
  int GetCount () {return
    integer.max_value;
  }
  @Override Public
  Boolean isviewfromobject (View view, Object object) {return
    view==object
  }
  @Override public
  Object instantiateitem (viewgroup container, int position) {
    Container.addview (list_ View.get (position% list_view.size ()));
    Return List_view.get (position% list_view.size ());
  }
  @Override public
  void Destroyitem (ViewGroup container, int position, object object) {
    Container.removeview ( List_view.get (position% list_view.size ()));
  }


Mainactivity.java

Import Android.os.Bundle;
Import Android.support.v4.view.ViewPager;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.TextView;
Import java.util.ArrayList;
Import java.util.List; /** * Dynamically add view to Viewpager and implement infinite loop/public class Mainactivity extends Appcompatactivity {private Viewpager List_page
  R
  Private list<view> List_view;
  Private Viewpageadapter Adpter;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    List_pager = (Viewpager) Findviewbyid (R.id.list_pager);
    List_view = new arraylist<> (); This is only set to 4. Because in the implementation application, when we load the page, you will know the number of the page according to the amount of data//Generally, we will be based on the number of list<> or string[to determine how many pages for (int i=
      0;i<4;i++) {View view = Layoutinflater.from (this). Inflate (R.layout.fragment_page,null); TextView txt_num = (TextView) View.findviewbyid (r.id.txt_NUM);
      Txt_num.settext (i + "");
    List_view.add (view);
    } adpter = new Viewpageadapter (List_view);
    List_pager.setadapter (Adpter);
    At first. The current page is the first to the maximum half for circular sliding int currentitem = INTEGER.MAX_VALUE/2;
    Let the first current page be 0//currentitem = CurrentItem-((INTEGER.MAX_VALUE/2)% 4);
  List_pager.setcurrentitem (CurrentItem);

 }
}

Effect Chart:

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.