0

Scroll

Here I use circular ListView for Left-end-side for scrolling images. Now I want when I scroll ListView and when Dog image came adjacent to "DOG" name of right-end-side, then image set there and remaining ListView start to scroll. Same scenario perform with rest of the names. I used following code

    public class MainActivity extends ListActivity implements OnScrollChangedListener, OnScrollListener{

Button dog;
Button cat;
Button cow;
Button rat;
Button horse;

ImageView imageView1;

List<Integer> spectrum = new ArrayList<Integer>();
String[] animalName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    animalName =  new String[]{"CAT","DOG","COW","RAT","PARROT","HORSE","FISH"};
    //List<Integer> animalImage =  new List<Integer>{R.drawable.cat,R.drawable.dog,R.drawable.cow,R.drawable.rat,R.drawable.parrot,R.drawable.horse,R.drawable.fish};

    dog = (Button)findViewById(R.id.button1);
    cat = (Button)findViewById(R.id.button2);
    cow = (Button)findViewById(R.id.button3);
    rat = (Button)findViewById(R.id.button4);
    horse = (Button)findViewById(R.id.button5);
    imageView1 = (ImageView)findViewById(R.id.imageView1);


        spectrum.add(R.drawable.horse);
        spectrum.add(R.drawable.rat);
        spectrum.add(R.drawable.cow);
        spectrum.add(R.drawable.fish);
        spectrum.add(R.drawable.parrot);
        spectrum.add(R.drawable.cat);
        spectrum.add(R.drawable.dog);

    SpectrumAdapter spectrumAdapter = new SpectrumAdapter(MainActivity.this, 
            -1, spectrum,animalName);

    CircularListAdapter circularAdapter = new CircularListAdapter(spectrumAdapter);

    ListView listView = getListView();
    listView.setDivider(null);

    // 4. Set the circular adapter to the list, and we're done
    setListAdapter(circularAdapter);

        listView.setOnScrollListener(this);

}



 /*
 * A custom adapter that extends the ArrayAdapter<T>.
 */
class SpectrumAdapter extends ArrayAdapter<Integer> {
    private List<Integer> spectrum;

    public SpectrumAdapter(Context context, int textViewResourceId,
            List<Integer> spectrum,String[] animal) {
        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);
        }

        // Set property
        view.setBackgroundResource(spectrum.get(position));


        return view;
    }

}



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

}



@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
    int state = scrollState;
    if(scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
    {
        String b1Text = (String) dog.getText();
        for(int i=0;i<spectrum.size();i++)
        {
            String img1Text = this.getResources().getResourceEntryName(spectrum.get(i));
            if(b1Text.equalsIgnoreCase(img1Text))
            {
                imageView1.setBackgroundResource(spectrum.get(i));
                imageView1.setVisibility(View.VISIBLE);

            }
            else
            {
                Toast.makeText(this, "I am not Dog", Toast.LENGTH_LONG).show();
            }
        }
    }
}

}

Please help me for same.

upender
  • 99
  • 1
  • 12
  • i would use a model class with name and a drawable. Then you can have a list of type class and then use the same in adapter – Raghunandan Mar 19 '15 at 06:29
  • Could you please type some code, actually I tried a lot but didn't get any success. or if possible separate chat session for this. Its so crucial for me. – upender Mar 19 '15 at 06:32
  • try this http://stackoverflow.com/questions/20886795/populate-listview-from-listobject/20886827#20886827 and also this http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct – Raghunandan Mar 19 '15 at 06:39
  • Hey you didn't get my question, I want to scroll my ListView and when Dog image came equivalent to DOG name then image stick there. The link you show I already done that. – upender Mar 19 '15 at 07:02
  • i still don't understand. you can use listview.scrollTo(position) – Raghunandan Mar 19 '15 at 07:46
  • Actually I want to scroll ListView of Images and when Dog image appear in front of DOG name then Dog image stick in front of DOG name. If possible can I start to conversation with you some other portal or website. – upender Mar 19 '15 at 08:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73307/discussion-between-upender-and-raghunandan). – upender Mar 19 '15 at 08:53

0 Answers0