我知道有几次问这个问题,但答案都没有让我找到解决方案。这是我的问题:我创建了一个包含大约50个项目的ListView。其中很少需要白色背景,而另一个黄色背景。为此,我创建了两个布局feed1.xml和feed2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/yellow"
>
<ImageView
android:id="@+id/ReadUnread"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/FeedItem"
android:src="@drawable/readunread"
android:background="@color/yellow"
>
</ImageView>
<TextView
android:id="@+id/FeedItem"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="12sp"
android:textColor="#000"
android:textStyle="bold"
android:background="@color/yellow">
</TextView>
</LinearLayout>
第二个是完全相同的,除了我替换为“@ color / white”的背景颜色。
创建ListView的源代码是这样的(当然它是一个轻量级版本):
public class ItemsListActivity extends ListActivity implements Runnable {
static final int FEED_RESULT = 0;
private ArrayList<Feed> feeds;
private FeedAdapter m_feedAdapter;
public ListView lv;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
createListAdapter();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void createListAdapter()
{
m_feedAdapter = new FeedAdapter(this, R.layout.feed, feeds);
setListAdapter(m_feedAdapter);
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setCacheColorHint(0);
lv.setDividerHeight(2);
lv.setScrollingCacheEnabled(false);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(ItemsListActivity.this,
ReaderActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("id", feeds.get(position).getId());
intent.putExtras(bundle);
startActivityForResult(intent, FEED_RESULT);
}
});
}
@Override
public void run() {
Bundle bundle = this.getIntent().getExtras();
handler.sendEmptyMessage(0);
}
private Boolean getFeeds() {
feeds = new ArrayList<Feed>();
}
private class FeedAdapter extends ArrayAdapter<Feed> {
private ArrayList<Feed> feeds;
public FeedAdapter(Context context, int textViewResourceId,
ArrayList<Feed> feeds) {
super(context, textViewResourceId, feeds);
this.feeds = feeds;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Feed f = feeds.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (f.getWhite() == 0)
v = vi.inflate(R.layout.feed1, null);
else
v = vi.inflate(R.layout.feed2, null);
}
if (f != null) {
TextView tt = (TextView) v.findViewById(R.id.FeedItem);
ImageView im = (ImageView) v.findViewById(R.id.ReadUnread);
if (tt != null) {
tt.setText(Html.fromHtml("<b>"+f.getSubject()+"</b>"+"<small> >>"+f.getAuthor()+"</small>"));
tt.setTag(f.getId());
}
}
return v;
}
}
}
当我运行我的应用程序(在模拟器或设备上)时,直到我滚动它似乎很棒。当我滚动列表时,几个黄色项目变为白色,然后当我再次滚动时,它们再次变为黄色。白色物品完全一样。它似乎是完全随机的...背景颜色被交换并再次交换...我尝试了很多东西(比如setCacheColorHint()或setScrollingCacheEnabled())但似乎没有任何效果。你有什么想法吗?
感谢您的帮助。
解决方案
好的,问题解决了!
要修改的两件重要事情:
因此我的getView()函数的正确实现变为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Feed f = feeds.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feed1, null);
}
if (f != null) {
TextView tt = (TextView) v.findViewById(R.id.FeedItem);
ImageView im = (ImageView) v.findViewById(R.id.ReadUnread);
if (tt != null) {
tt.setText(Html.fromHtml("<b>"+f.getSubject()+"</b>"+"<small> >>"+f.getAuthor()+"</small>"));
tt.setTag(f.getId());
if (f.getWhite == 0) {
im.setBackgroundResource(R.color.white);
tt.setBackgroundResource(R.color.white);
}
else {
im.setBackgroundResource(R.color.yellow);
tt.setBackgroundResource(R.color.yellow);
}
}
}
return v;
}
答案 0 :(得分:0)
将android:cacheColorHint属性设置为透明色: android:cache Color Hint =&#34;#00000000&#34;
参考参考链接:
http://developer.android.com/resources/articles/listview-backgrounds.html