title: Infinite Scrolling of items created in two Linear Layout in Android
body: <p>I divide my screen in two parts, one of the parts contain five buttons and another part contain seven ImageView with images. Now I want that ImageView rotate infinite,means, after last image, again images start to come. My XML is</p>
  
  <pre><code>      &lt;LinearLayout 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"
      android:orientation="horizontal"
      android:baselineAligned="true"
      tools:context="in.example.splitapp.MainActivity" &gt;
    &lt;ScrollView 
                  android:id="@+id/scrollView"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                   android:layout_weight="1" 
                   &gt;
      &lt;RelativeLayout 
          android:id="@+id/linearLayout1"
           android:orientation="vertical"
          android:layout_weight="1" 
          android:layout_height="fill_parent" 
          android:layout_width="fill_parent"
          android:background="@android:color/holo_green_dark"&gt;
          &lt;Button android:id="@+id/button1"
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_centerHorizontal="true"
              android:text="DOG"/&gt;
           &lt;Button android:id="@+id/button2"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
               android:layout_below="@id/button1"
              android:text="CAT"/&gt;
            &lt;Button android:id="@+id/button3"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/button2"
              android:text="COW"/&gt;
             &lt;Button android:id="@+id/button4"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/button3"
              android:text="RAT"/&gt;
              &lt;Button android:id="@+id/button5"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/button4"
              android:text="PARROT"/&gt;
          &lt;/RelativeLayout&gt;
          &lt;/ScrollView&gt;
              &lt;ScrollView 
                  android:id="@+id/scrollView2"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                   android:layout_weight="1" 
                   &gt;
      &lt;RelativeLayout 
          android:id="@+id/linearLayout2"
          android:orientation="horizontal"
          android:layout_weight="1" 
          android:layout_height="fill_parent" 
          android:layout_width="fill_parent"
          android:background="@android:color/holo_purple"
         &gt;
  
          &lt;ImageView android:id="@+id/imageView1"
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:layout_centerHorizontal="true"
              android:background="@drawable/dog"
              android:text="DOG"/&gt;
           &lt;ImageView android:id="@+id/imageView2"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
               android:layout_below="@id/imageView1"
               android:background="@drawable/cat"
              android:text="CAT"/&gt;
            &lt;ImageView android:id="@+id/imageView3"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/imageView2"
                 android:background="@drawable/cow"
              android:text="COW"/&gt;
             &lt;ImageView android:id="@+id/imageView4"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/imageView3"
                 android:background="@drawable/rat"
              android:text="RAT"/&gt;
              &lt;ImageView android:id="@+id/imageView5"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/imageView4"
                 android:background="@drawable/parrot"
              android:text="PARROT"/&gt;
               &lt;ImageView android:id="@+id/imageView6"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/imageView5"
                 android:background="@drawable/horse"
              android:text="HORSE"/&gt;
                &lt;ImageView android:id="@+id/imageView7"
              android:layout_width="match_parent"
              android:layout_height="100dp"
               android:layout_centerHorizontal="true"
                 android:layout_below="@id/imageView6"
                 android:background="@drawable/fish"
              android:text="FISH"/&gt;
          &lt;/RelativeLayout&gt;
  
  &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/mz3TT.png" alt="Sample Image"></p>
  
  <p>I want that ImageView side scrolling infinite means these five images repeated always. </p>
  
  <p>This is my MainActivity:</p>
  
  <pre><code>    public class MainActivity extends Activity implements OnScrollListener {
  
  List&lt;String&gt; animalNameList = new ArrayList&lt;String&gt;();
  ArrayList&lt;Integer&gt; animalImageList = new ArrayList&lt;Integer&gt;();
  ImageAdapter imageAdapter;
  NameAdapter nameAdapter = null;;
  boolean flag = false;
  ListView listView;
  ListView listView1;
  ListView upperListView;
  RelativeLayout parentLayout;
  LinearLayout childLayout;
  int index = 0;
  UpdateAdapter updateAdapter = null;
  ArrayList&lt;DataSplit&gt; array = new ArrayList&lt;DataSplit&gt;();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);     
       animalNameList.add("CAT");
       animalNameList.add("DOG");
       animalNameList.add("COW");
       animalNameList.add("RAT");
       animalNameList.add("PARROT");
       animalNameList.add("HORSE");
       animalNameList.add("FISH");
  
       animalImageList.add(R.drawable.horse);
       animalImageList.add(R.drawable.parrot);
       animalImageList.add(R.drawable.fish);
       animalImageList.add(R.drawable.rat);
       animalImageList.add(R.drawable.dog);
       animalImageList.add(R.drawable.cat);
       animalImageList.add(R.drawable.cow);
  
       parentLayout = (RelativeLayout)findViewById(R.id.parentRelative);
       childLayout = (LinearLayout)findViewById(R.id.childLinearLayout);
  
       nameAdapter = new NameAdapter(MainActivity.this, 
                  -1, animalNameList);
       listView = (ListView)findViewById(R.id.listView1);
      listView.setAdapter(nameAdapter);
  
  
        imageAdapter = new ImageAdapter(MainActivity.this, -1,      animalImageList);
       listView1 = (ListView)findViewById(R.id.listView2);
       CircularListAdapter circularAdapter = new     CircularListAdapter(imageAdapter);
      listView1.setDivider(null);
      listView1.setAdapter(circularAdapter);
  
      listView1.setOnScrollListener(this);
  
  
  
      upperListView = (ListView)findViewById(R.id.upperListView);
  
  
      listView.setOnScrollListener(new OnScrollListener() {
  
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {
              // TODO Auto-generated method stub
  
  
          }
  
          @Override
          public void onScroll(AbsListView view, int firstVisibleItem,
                  int visibleItemCount, int totalItemCount) {
              // TODO Auto-generated method stub
              if(flag)
              {
  
  
  
              }
  
          }
      });
  
  }
  
  
  class NameAdapter extends BaseAdapter {
      private List&lt;String&gt; animalName;
      private Activity context;
  
      public NameAdapter(Activity context, int textViewResourceId,
              List&lt;String&gt; animalName) {
          super();
          this.animalName = animalName;
          this.context = context;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              LayoutInflater inflater=context.getLayoutInflater();
              convertView = inflater.inflate(R.layout.animalname, parent, false);
              Button button=(Button)convertView.findViewById(R.id.button);
              button.setId(position);
              button.setText(animalName.get(position));
  
          }
          return convertView;
      }
  
      public int getCount() 
      {
          int size = animalName.size();
          return size;
      }
  
      @Override
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
  }
  
  
  class ImageAdapter extends ArrayAdapter&lt;Integer&gt; {
      private ArrayList&lt;Integer&gt; animalImage;
      private Activity context;
  
      public ImageAdapter(Activity context, int textViewResourceId,
              ArrayList&lt;Integer&gt; animalImage) {
          super(context, textViewResourceId);
          this.animalImage = animalImage;
          this.context = context;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View view = convertView;
          if(view == null) {
              view = new View(MainActivity.this);
          }
          view.setId(position);
          //view.setBackgroundResource(animalImage[position]);
          view.setBackgroundResource(animalImage.get(position));
          return view;
      }
  
      public int getCount() 
      {
          int size = animalImage.size();
          return size;
      }
  
  }
  
  
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
  
  
          int childInt = view.getChildAt(0).getId();
          //String img1Text = this.getResources().getResourceEntryName(animalImage[childInt]);
          String img1Text = this.getResources().getResourceEntryName(animalImageList.get(childInt));
          //String buttonText = animalName[index];
          String buttonText = animalNameList.get(index);
          if(img1Text.equalsIgnoreCase(buttonText))
          {
              final String name = animalNameList.get(index);
              int image = animalImageList.get(childInt);
  
              DataSplit data = new DataSplit();
              data.setAnimalName(name);
              data.setAnimalImage(image);
              array.add(data);
  
  
              index++;
              flag = true;
              if(array.size() == 1)
              {
  
                   updateAdapter = new UpdateAdapter(MainActivity.this,-1,array);
                   upperListView.setAdapter(updateAdapter);
                   upperListView.setVisibility(View.VISIBLE);
  
              }
              else
              {
                  updateAdapter.notifyDataSetChanged();
  
              }
  
  
               listView.post(new Runnable() {
  
                      @Override
                      public void run() {
                          animalNameList.remove(name);
                          nameAdapter.notifyDataSetChanged();     
                      }
                  });
  
  
          }
  
  
  
  }
  
  
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem,
          int visibleItemCount, int totalItemCount) {
  
  }
  
  class UpdateAdapter extends ArrayAdapter&lt;Integer&gt; {
  
      ArrayList&lt;DataSplit&gt; dataSplit;
      private Activity context;
  
      public UpdateAdapter(Activity context, int textViewResourceId,
              ArrayList&lt;DataSplit&gt; dataSplit) {
          super(context, textViewResourceId);
          this.dataSplit = dataSplit;
          this.context = context;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
              if (convertView == null) {
              LayoutInflater inflater=context.getLayoutInflater();
              convertView = inflater.inflate(R.layout.addeddata, parent, false);
              Button button=(Button)convertView.findViewById(R.id.button);
              ImageView imageView=(ImageView)convertView.findViewById(R.id.imageView);                
  
              String animalName = dataSplit.get(position).getAnimalName();
              int imageName = dataSplit.get(position).getAnimalImage();
  
              button.setText(dataSplit.get(position).getAnimalName());
              imageView.setBackgroundResource(dataSplit.get(position).getAnimalImage());
  
  
  
          }
          return convertView;
      }
  
      public int getCount() 
      {
          int size = dataSplit.size();
          return size;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I created activity_main.xml which contains a parent RelativeLayout and under this a ListView named upperListView and initially set visibility gone. Below this I created a LinearLayout and for this I created two ListView for Name DataSet and Image DataSet. When data matches I create a UpdateAdapter in which I use addeddata.xml, in which a button and ImageView created and when it is a successful match then hidden list view visibility becomes Visible and this "addeddata.xml" loaded in listview. This I perform. It will work perfectly.But when first matches,i.e, CAT name match with the Cat image, data is not deleted from both of listview and CAT name again shown in below the new ListView. </p>
  
  <p>How to manage to complete matches if the application crashes.</p>