我尝试使用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());
}
}
答案 0 :(得分: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;
}
}
有关一些解释,您可以查看this post
中的答案从长远来看,您可能希望针对速度优化getView() This is a very good training video you can watch
答案 1 :(得分:-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;
}
}