3

我尝试使用 stackoverflow 中的任何解决方案,但没有成功。所以,我在点击后将背景颜色设置为项目 - 它的工作。在滚动(当项目不在屏幕上)和向后滚动(项目在屏幕上)之后,这个项目失去了背景颜色。以下是部分代码:

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

2 回答 2

1

在短期内,您可以添加以下行:

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

有关一些解释,您可以查看这篇文章中的答案

从长远来看,您可能希望优化您的 getView() 以提高速度。
这是一个非常好的培训视频,您可以观看

于 2013-05-04T15:03:58.260 回答
-1

我找到了解决方案。只需添加两行:

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;
            }
        }
于 2013-05-04T15:33:57.783 回答