当我向下滚动列表视图时,项目标记在列表下方

时间:2014-09-21 16:37:06

标签: android listview

我创建了一个listview,显示他标记了这个调用:view.setBackgroundResource(R.drawable.active_row) 但是当您向下滚动列表时,它会标记lista.exe下面的项目项: 屏幕上显示10个项目,当滚动列表时标记为第一个,以便显示11,它将此项目标记为第一项:

    final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
    SongsManager plm = new SongsManager();
    // get all songs from sdcard
    this.songsList = plm.getPlayList();
    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);

        // adding HashList to ArrayList
        songsListData.add(song);
    }
    // Adding menuItems to ListView
     final ListAdapter adapter = new SimpleAdapter(this, songsListData,
            R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                    R.id.songTitle2 });
    setListAdapter(adapter);

ListView lv = getListView();
    // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {
        int flag1 = 0;
        int flag2 = 0;

        int auxPosicao1 = -1;//primeira vez
        int auxPosicao2 = -1;//primeira vez

        @SuppressWarnings("unused")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            View lastRow = null;        
            if (lastRow != null) {
                lastRow.setBackgroundColor(0x00000000);
                }
            if (flag1==0 &&((auxPosicao1 == position || (auxPosicao1 == -1 && auxPosicao2==-1)))){
                flag1 = 1;
                auxPosicao1 = position;
                aux = position;
                view.setBackgroundResource(R.drawable.active_row);

            }else if(auxPosicao1==position)
            {           
                if (flag2 == 1){

                    aux = aux2;
                    auxPosicao1 = auxPosicao2;  

                    aux2 = -1;
                    flag2 =0;
                    auxPosicao2 =-1;

                }else{
                    aux = -1;
                    flag1=0;
                    auxPosicao1 =-1;

                }
                view.setBackgroundResource(R.drawable.active_row2);

            }else if(flag2 == 0 &&((auxPosicao2 == position || auxPosicao2 == -1))){
                aux2 = position;
                view.setBackgroundResource(R.drawable.active_row);
                flag2 = 1;
                auxPosicao2 = position;

            }else if(position == auxPosicao2){
                aux2 = -1;
                flag2 =0;
                auxPosicao2 =-1;
                view.setBackgroundResource(R.drawable.active_row2);
            }else{
                Toast.makeText(getBaseContext(), "Coming soon ...", Toast.LENGTH_SHORT).show();
            }
            //lastRow = view;
        }
    });

... 我需要它来正确存储我的列表状态

1 个答案:

答案 0 :(得分:0)

android listview重新使用相同的视图

所以一旦为一个位置设置背景颜色

您需要将背景颜色重置为其他位置