我创建了一个 ListView,每个列表项都包含一个进度条。滚动此列表视图时,所有进度条都会显示并很好地滚动,除了第一个带有进度条的项目,它始终位于顶部。视觉效果如下:向下滚动时,所有进度条都滚动良好,但另外新的进度条总是在最初显示第一个进度条的固定位置绘制。有谁知道如何摆脱这个?
布局规划器_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<include layout="@layout/planner_row"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include layout="@layout/parent_view"/>
<TextView
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/no_entries"
android:textSize="38sp"
android:visibility="gone"/>
</RelativeLayout>
布局规划器_row.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_layout"
android:layout_width="wrap_content"
android:layout_height="50dp">
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:textColor="@color/black"
android:textSize="18sp">
</TextView>
<TextView
android:id="@+id/category_spent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/category_name"
android:layout_centerVertical="true"
android:textColor="@color/black"
android:textSize="18sp" >
</TextView>
<ProgressBar
android:id="@+id/category_bar"
android:layout_width="200dp"
android:layout_height="15dp"
android:layout_below="@id/category_name"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100" />
<TextView
android:id="@+id/category_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/category_bar"
android:layout_below="@id/category_spent"
android:layout_centerVertical="true"
android:textColor="@color/black"
android:textSize="18sp" >
</TextView>
</RelativeLayout>
适配器的主要部分:
public class PlannerListAdapter extends SimpleCursorAdapter {
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PlannerViewHolder holder;
// create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.planner_row, parent, false);
// find children views
TextView tvName = (TextView) convertView.findViewById(R.id.category_name);
TextView tvLimit = (TextView) convertView.findViewById(R.id.category_limit);
TextView tvSpent = (TextView) convertView.findViewById(R.id.category_spent);
ProgressBar bar = (ProgressBar) convertView.findViewById(R.id.category_bar);
holder = new PlannerViewHolder(tvName, tvLimit, tvSpent, bar);
// tag the row with it's children views
convertView.setTag(holder);
} else { // reuse existing row view
holder = (PlannerViewHolder)convertView.getTag();
}
if (dataCursor.moveToPosition(position)) {
holder.tvCategory.setText(dataCursor.getString(1));
holder.bar.setProgress(50);
}
return convertView;
}
...
public static class PlannerViewHolder {
private ProgressBar bar;
private TextView tvCategory, tvLimit, tvSpent;
public PlannerViewHolder(TextView tv1, TextView tv2, TextView tv3, ProgressBar b) {
tvCategory = tv1;
tvLimit = tv2;
tvSpent = tv3;
bar = b;
}
}
}
使用适配器的活动代码:
@Override
public void onCreate(Bundle savedInstanceState) {
setLayout(R.layout.planner_main);
super.onCreate(savedInstanceState);
...
listView = (ListView)findViewById(android.R.id.list);
Cursor c = db.getCategoriesData();
adapter = new PlannerListAdapter(context, c, new String[] { Data.Categories.NAME, Data.Categories.LIMIT }, new int[] { R.id.category_name, R.id.category_limit });
listView.setAdapter(adapter);