0

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

      <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" >
  <ScrollView 
                android:id="@+id/scrollView"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                 android:layout_weight="1" 
                 >
    <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">
        <Button android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:text="DOG"/>
         <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"/>
          <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"/>
           <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"/>
            <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"/>
        </RelativeLayout>
        </ScrollView>
            <ScrollView 
                android:id="@+id/scrollView2"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                 android:layout_weight="1" 
                 >
    <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"
       >

        <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"/>
         <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"/>
          <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"/>
           <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"/>
            <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"/>
             <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"/>
              <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"/>
        </RelativeLayout>

</ScrollView>
</LinearLayout>

Sample Image

I want that ImageView side scrolling infinite means these five images repeated always.

This is my MainActivity:

    public class MainActivity extends Activity implements OnScrollListener {

List<String> animalNameList = new ArrayList<String>();
ArrayList<Integer> animalImageList = new ArrayList<Integer>();
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<DataSplit> array = new ArrayList<DataSplit>();
@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<String> animalName;
    private Activity context;

    public NameAdapter(Activity context, int textViewResourceId,
            List<String> 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<Integer> {
    private ArrayList<Integer> animalImage;
    private Activity context;

    public ImageAdapter(Activity context, int textViewResourceId,
            ArrayList<Integer> 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<Integer> {

    ArrayList<DataSplit> dataSplit;
    private Activity context;

    public UpdateAdapter(Activity context, int textViewResourceId,
            ArrayList<DataSplit> 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;
    }
}

}

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.

How to manage to complete matches if the application crashes.

James Z
  • 12,209
  • 10
  • 24
  • 44
upender
  • 99
  • 1
  • 12

3 Answers3

1

This has been discussed in the past, with using circular Listviews:

  1. How to create a closed (circular) ListView?

  2. Closed or circular Horizontal ListView Android

Community
  • 1
  • 1
A B
  • 338
  • 3
  • 10
  • Great! Please also mark it as correct for further references. Thanks! – A B Mar 18 '15 at 06:57
  • Can you help me some more concepts. I am stucked. – upender Mar 18 '15 at 07:14
  • If related to the subject, explain with examples what the issues are and maybe I can help. – A B Mar 18 '15 at 07:35
  • I just want when I scroll ImageView part then if Dog image matches with the right end side Dog name then image stick with the Dog Name Button and rest of the images remain scrolling. Is this information enough for you? – upender Mar 18 '15 at 08:38
  • Hey please help me for the above issue i stuck. Its very crucial for me. – upender Mar 19 '15 at 04:16
1

I just want when I scroll ImageView part then if Dog image matches with the right end side Dog name then image stick with the Dog Name Button and rest of the images remain scrolling.

For this here is what you could try:

  • after scrolling items in list1 and list2, iterate over each item in the shortest listview and check if item1(position) [tag] = item2(position) [tag] (so, if you have a match)

  • if true:

    1. redraw screen like this: add a new listview at the bottom of the screen (below both listviews). This listview with contain both item1 and item2 (so it's litviewitem xml will be a linear layout with other two linear layouts, one containing the matched pet name and the other the matched pet picture)

    2. remove item1(position) and item2(position) from listview1, listview2 (which means you have to remove, for example, dog name from listview pet names datasource, and dog image from listview pet pictures datasource and reset adapters to listviews)

  • this way you will continue to be able to scroll and match items from listview1 and listview2 - without the already matching items-, and also have a bottom listview with all the matching elements that cannot be matched any more

Hope this helps!

A B
  • 338
  • 3
  • 10
  • Thanks for the suggestion but its not worked. Please if possible post the code for same. – upender Mar 24 '15 at 04:35
  • Hey, I have done this and it will work for me. Thanks for this wonderful solution, but there is one small issue i face. One is when I match Image with right end side name, then I want to remove that image from data set, but I cant because in Circular Adapter Integer.MAXDATA is in bulk. and another is when matches finish, then application crashed. – upender Mar 24 '15 at 11:23
  • Great that you managed to implement the solution! In order to help with the errors I'll need you to post the code with the adapter and how you are handling the matches, maybe edit the first post. – A B Mar 25 '15 at 12:15
0

After spend couple of days I found the perfect solution for my question. I dynamically created the ImageView and also the ListView. Image data are populating in the ListView. After matching the text with right Image, I take that ImageView from the List and set in dynamically created ImageView. Following is my code.

    public class MainActivity extends Activity implements OnScrollChangedListener, OnScrollListener{
int index = 0;
ImageView imageView1;
List<Integer> spectrum = new ArrayList<Integer>();
String[] animalName;
ImageView[] imageArray;
Button[] buttonArray;
ListView listView;
SpectrumAdapter spectrumAdapter;
ListView animalImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    animalName =  new String[]{"CAT","DOG","COW","RAT","PARROT","HORSE","FISH"};
    imageArray = new ImageView[animalName.length];
    buttonArray = new Button[animalName.length];        
    LinearLayout inner1 = (LinearLayout)findViewById(R.id.innerLayout1);
    LinearLayout inner2 = (LinearLayout)findViewById(R.id.innerLayout2);

    ListView animalList = (ListView)findViewById(R.id.animalList);

    NameAdapter nameAdapter = new NameAdapter(MainActivity.this, 
            -1, animalName);
    animalList.setAdapter(nameAdapter);
    for(int i=0;i<animalName.length;i++)
    {
        ImageView imageView = new ImageView(this);
        imageView.setId(i);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,150);
        imageView.setTag(i);
        imageView.setLayoutParams(params1);
        imageView.setVisibility(View.GONE);
        imageArray[i] = imageView;
        inner1.addView(imageView);
    }
    ListView listView = new ListView(this);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    listView.setLayoutParams(param);
    inner2.addView(listView);
    spectrum.add(R.drawable.horse);
    spectrum.add(R.drawable.rat);
    spectrum.add(R.drawable.cow);
    spectrum.add(R.drawable.dog);
    spectrum.add(R.drawable.fish);
    spectrum.add(R.drawable.parrot);
    spectrum.add(R.drawable.cat);
    spectrumAdapter = new SpectrumAdapter(MainActivity.this, 
            -1, spectrum);

    CircularListAdapter circularAdapter = new CircularListAdapter(spectrumAdapter);
    listView.setAdapter(circularAdapter);
    listView.setOnScrollListener(this);
}


class NameAdapter extends BaseAdapter {
    private String[] animalName;
    private Activity context;

    public NameAdapter(Activity context, int textViewResourceId,
            String[] 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.setText(animalName[position]);
        return convertView;

    }

    public int getCount() 
    {
        int size = animalName.length;
        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 SpectrumAdapter extends ArrayAdapter<Integer> {
    private List<Integer> spectrum;
    public SpectrumAdapter(Context context, int textViewResourceId,
            List<Integer> spectrum) {
        super(context, textViewResourceId, spectrum);
        this.spectrum = spectrum;
    }

    @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(spectrum.get(position));
        return view;
    }

    public int getCount() 
    {
        int size = spectrum.size();
        return size;
    }

}

@Override
public void onScrollChanged() {
    // TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

    int childInt = view.getChildAt(0).getId();
    String img1Text = this.getResources().getResourceEntryName(spectrum.get(childInt));
    String bb = animalName[index];
    if(bb.equalsIgnoreCase(img1Text))
    {
        ImageView img = imageArray[index];
        img.setBackgroundResource(spectrum.get(childInt));
        img.setVisibility(View.VISIBLE);
        index++;
    }
    else
    {
    }
}
}

This is the perfect solution for the question. Thanks all for the suggestion.

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
upender
  • 99
  • 1
  • 12