ListView滚动并匹配各自的名称

时间:2015-03-19 06:26:09

标签: android listview circular-list onscrolllistener

Scroll

这里我使用圆形ListView作为左端侧滚动图像。现在我想滚动ListView时,当Dog图像与右端的“DOG”名称相邻时,然后图像设置在那里,剩下的ListView开始滚动。相同的方案与其余名称一起执行。我使用了以下代码

    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();
            }
        }
    }
}

}

请帮助我。

0 个答案:

没有答案