可扩展列表 Android 滚动时黑色背景
我在 Android 中的 ExpandableList 视图上遇到了困难。 我正在使用 api 演示中提供的代码,但我不知道如何解决我的问题。
我的视图上有白色背景,但当我滚动列表时它会变成黑色......为什么?
我不想要这种效果...有什么办法可以改变它吗?有没有办法使用此代码自定义列表? 我正在寻找这样的东西..希望你能帮助我! 谢谢 :)
package com.example.android.apis.view;
import android.R;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class ExpandableList3 extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
R.layout.simple_expandable_list_item_3,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
I'm struggling with the ExpandableList View in Android.
I'm using the code provided in the apis demo but I don't know how to solve my problem.
I got a white background on my view but when I'm scrolling the list it turns black..why?
I don't want this effect...is there a way to change it? is there a way to customize the list using this code?
I'm looking for something like this..Hope you could help me!
Thank you :)
package com.example.android.apis.view;
import android.R;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class ExpandableList3 extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
R.layout.simple_expandable_list_item_3,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
添加到 xml 文件中的列表视图:
android:cacheColorHint="#00000000"
或在 Java 代码中:
getListView().setCacheColorHint(0);
Add to your listview in the xml file:
android:cacheColorHint="#00000000"
or in your Java code:
getListView().setCacheColorHint(0);
将
android:cacheColorHint="#00000000"
添加到 ListView示例;
add
android:cacheColorHint="#00000000"
to ListViewexample;
在你的 xml 中写入这个,你会得到答案
android:scrollingCache="false"
Write this one in your xml you will get the answer
android:scrollingCache="false"
按照此链接上的说明进行操作:
Android 开发人员资源:Listview 背景
简而言之:
...要解决此问题,您所要做的就是禁用缓存颜色提示优化(如果您使用非纯色背景),或者将提示设置为适当的纯色值。您可以从代码中执行此操作(请参阅 setCacheColorHint(int)),或者最好使用 android:cacheColorHint 属性从 XML 中执行此操作。要禁用优化,只需使用透明颜色#00000000。以下屏幕截图显示了 XML 布局文件中设置的 android:cacheColorHint="#00000000" 列表...
Follow the instructions on this link:
Android Developer Ressources: Listview Backgrounds
In short:
...To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file ...
我只想指出,有一种更优雅的方式来表达
#00000000
,只需使用:@android/color/transparent
。如果您稍后再回到该代码,就会更容易理解(是的,我知道无论如何它只是一个 ARGB 数字;-))。I'd just like to point out that there's a more elegant way of expressing
#00000000
, just use:@android/color/transparent
. It's easier to understand if you come back to that code some time later (Yeah, I know that it's just an ARGB number anyway ;-)).设置列表颜色:
只需使用以下方法或在 Java 代码中
Simply set your list color, using the methods below:
or in your Java code: