Android ListView在滚动时闪烁/未正确刷新
我是一个android新手,正在尝试我的第一个项目API级别10。我正在ListActivity中下载json数据,将其填充到本地sqlite数据库中,并使用自定义游标适配器显示它。我现在的布局中只有文本视图,但将改变它,因此自定义游标适配器。一切似乎都正常,数据库被填充,数据被显示,但当滚动列表时,它会闪烁,我会得到叠加的文本/不干净的刷新。任何帮助都将不胜感激。我尝试在ItemAdapter中使用ViewHolder,但没有帮助,然后将其删除。我没有具体的背景 我的代码:Android ListView在滚动时闪烁/未正确刷新,android,listview,Android,Listview,我是一个android新手,正在尝试我的第一个项目API级别10。我正在ListActivity中下载json数据,将其填充到本地sqlite数据库中,并使用自定义游标适配器显示它。我现在的布局中只有文本视图,但将改变它,因此自定义游标适配器。一切似乎都正常,数据库被填充,数据被显示,但当滚动列表时,它会闪烁,我会得到叠加的文本/不干净的刷新。任何帮助都将不胜感激。我尝试在ItemAdapter中使用ViewHolder,但没有帮助,然后将其删除。我没有具体的背景 我的代码: public cl
public class ItemList extends ListActivity {
private static ItemDBAdapter dao;
private static ProgressDialog dialog;
private static Cursor ItemCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Item_list);
getListView().setSmoothScrollbarEnabled(true);
dao = new ItemDBAdapter(this);
dao.open();
dialog = ProgressDialog.show(this, "Download", "Downloading");
load();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_Item_list, menu);
return true;
}
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
super.onDestroy();
}
private class DownloadDataTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String success = "false";
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
JSONArray Items = new JSONArray(response);
dao.saveAll(Items);
} catch (Exception e) {
e.printStackTrace();
}
}
success = "true";
return success;
}
@Override
protected void onPostExecute(String result) {
// TODO: check if internet failed, not all data loaded, etc...
dialog.dismiss();
fillData();
if (result == "true") {
}
}
}
public void load() {
DownloadDataTask task = new DownloadDataTask();
task.execute(new String[] { "http://247.85.45.12:3000/Item/index.json" });
}
public void fillData() {
ItemCursor = dao.getAll();
ItemAdapter adapter = new ItemAdapter(this, ItemCursor);
setListAdapter(adapter);
startManagingCursor(ItemCursor);
}
}
public class ItemAdapter extends CursorAdapter {
private final Context context;
private int NAME, ADDRESS, PHONE, URL;
private LayoutInflater inflater;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
NAME = c.getColumnIndex(ItemDBAdapter.KEY_NAME);
URL = c.getColumnIndex(ItemDBAdapter.KEY_URL);
ADDRESS = c.getColumnIndex(ItemDBAdapter.KEY_ADDRESS);
PHONE = c.getColumnIndex(ItemDBAdapter.KEY_PHONE);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view != null) {
TextView name = (TextView)view.findViewById(R.id.name);
TextView url = (TextView)view.findViewById(R.id.url);
TextView address = (TextView)view.findViewById(R.id.address);
TextView phone = (TextView)view.findViewById(R.id.phone);
name.setText(cursor.getString(NAME));
url.setText(cursor.getString(URL));
address.setText(cursor.getString(ADDRESS));
phone.setText(cursor.getString(PHONE));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.Item_item, null);
return v;
}
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
试着打电话
getListView.setCacheColorHint(...);
使用项目的背景色或0,如果没有背景色,则将禁用缓存。如果你根本没有背景,甚至没有窗口背景,那也可能是问题所在
希望有帮助。问题解决了,我更改了ListView宽度和线性布局宽度以匹配\u父级
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
我注意到有不干净刷新和覆盖文本的区域是未使用的区域。这解决了问题。在虚拟设备上根本看不到问题,但在高分辨率galaxy s3上非常明显。我以前已经通过xml尝试过。但是我没有在布局中指定任何背景。默认设置您可以将android:cacheColorHint=00000000添加到Listview。