2013-05-04 134 views
2

我試圖使用任何來自stackoverflow的解決方案,但沒有succesfull。 所以,我點擊後將其設置爲項目的背景顏色 - 其工作。滾動後(當項目不在屏幕上)和後滾動(項目在屏幕中)時,此項目丟失背景顏色。 下面是部分代碼:滾動列表視圖時丟失項目背景顏色

public class RowAdapter extends ArrayAdapter<FileRow> { 
private final Activity context; 
private final List<FileRow> list; 

public RowAdapter(Activity context, List<FileRow> list) { 
    super(context, R.layout.browser_row, list); 
    this.context = context; 
    this.list = list; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     View v = super.getView(position, convertView, parent); 

     LayoutInflater inflator = context.getLayoutInflater(); 
     convertView = inflator.inflate(R.layout.browser_row, null); 

     TextView textView = (TextView)convertView; 
     textView.setText(list.get(position).GetName()); 
     if (!list.get(position).GetName().contains("/")) { 
      textView.setPadding(5, 5, 5, 5); 
      textView.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);  
     } 
     for (int i=0; i<BrowserFiles.selectedItems.size(); i++) { 
      if (BrowserFiles.selectedItems.get(i).GetPosition() == position) { 
       v.setBackgroundColor(Color.rgb(255, 165, 0)); 
       break; 
      } 
     } 

    return convertView; 
} 

}

public class BrowserFiles extends ListActivity implements OnItemClickListener { 

private String lastDir = "/"; 
private List<FileRow> items = null; 
public static List<FileRow> selectedItems; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.browser_files);  
    ListView lv = (ListView) findViewById(android.R.id.list); 
    lv.setOnItemClickListener(this); 
    Intent i = getIntent(); 
    lastDir = i.getStringExtra("lastDir"); 

    getDir(lastDir); 
    selectedItems = new ArrayList<FileRow>(); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    TextView tv = (TextView)view; 
    //jedna se o soubor 
    if (tv.getText().toString().toLowerCase(Locale.ENGLISH).endsWith("mp3")) { 
     String item = lastDir + tv.getText().toString(); 

     int isInList = -1; 
     for (int i=0; i < selectedItems.size(); i++) { 
      if (selectedItems.get(i).GetName().compareToIgnoreCase(item) == 0) { 
       isInList = i; 
       break; 
      } 
     } 

     if (isInList != -1) { 
      selectedItems.get(isInList).SetSelected(false); 
      selectedItems.remove(isInList); 
      parent.getChildAt(position).setSelected(false); 
      parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT); 
     } 
     else { 
      selectedItems.add(new FileRow(item, true, position)); 
      parent.getChildAt(position).setSelected(true);    
      parent.getChildAt(position).setBackgroundColor(Color.rgb(255, 165, 0)); 
     }      
    } 
    //jedna se o adresar 
    else { 
     getDir(lastDir.compareTo("/") == 0 ? lastDir + tv.getText().toString() : lastDir + "/" + tv.getText().toString()); 
    }  
} 

回答

0

我找到了解決方案。 只需添加兩行:

for (int i=0; i<BrowserFiles.selectedItems.size(); i++) { 
      if (BrowserFiles.selectedItems.get(i).GetPosition() == position) { 

       v.setBackgroundColor(Color.rgb(255, 165, 0)); 
       if (convertView != null) 
        convertView.setBackgroundColor(Color.rgb(255, 165, 0)); 
       break; 
      } 
     } 
0

在短期內就可以加入這一行:

for (int i=0; i<BrowserFiles.selectedItems.size(); i++) { 
     if (BrowserFiles.selectedItems.get(i).GetPosition() == position) { 
      v.getBackground().mutate(); // <======= 
      v.setBackgroundColor(Color.rgb(255, 165, 0)); 
      break; 
     } 
    } 

對於一些解釋,你可以看一下答案this post

從長遠來看,您可能想優化您的getView()f或速度。
This is a very good training video you can watch

+0

v(View)中不存在方法mutate。 – Musketyr 2013-05-04 15:06:59

+0

@Musketyr右,它的背景是可變的,我會編輯。 – ilomambo 2013-05-05 05:58:24