我的ListView非常简单,使用 ViewHolder 方法,但“橡胶”滚动不起作用。 如果我用手指运行列表,只需向下/向上运行4-5行并停止(所有列表大约有50行)。
适配器getView和getHolder方法
private ViewHolder getHolder(View vi) {
ViewHolder holder = new ViewHolder();
holder._id = (TextView) vi.findViewById(R.id._id);
holder.creator = (TextView) vi.findViewById(R.id.creator);
holder.message = (TextView) vi.findViewById(R.id.message);
holder.updated = (TextView) vi.findViewById(R.id.updated);
holder.userid = (TextView) vi.findViewById(R.id.userid);
return holder;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
ViewHolder holder;
View vi = convertView;
if (vi == null || vi.getTag() == null) {
vi = inflater.inflate(itemLayoutIdCommon, null);
holder = getHolder(vi);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
}
holder._id.setText((CharSequence) mValues.get(position).get("_id"));
holder.creator.setText((CharSequence) mValues.get(position).get("creator"));
holder.updated.setText((CharSequence) mValues.get(position).get("updated"));
holder.userid.setText((CharSequence) mValues.get(position).get("userid"));
holder.message.setText((CharSequence) mValues.get(position).get("title"));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.message.getLayoutParams();
holder.message.setBackgroundResource(layoutChooser.getBackground(mValues.get(position)));
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
layoutParams.addRule(layoutChooser.getAlign(mValues.get(position))); // ALIGN_PARENT_RIGHT / LEFT etc.
holder.message.setLayoutParams(layoutParams);
return vi;
}
public static class ViewHolder {
TextView _id;
TextView userid;
TextView message;
TextView creator;
TextView updated;
}
和row_layout.xml(itemLayoutIdCommon)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="_id"
android:visibility="gone" />
<TextView
android:id="@+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="userid"
android:visibility="gone" />
<TextView
android:id="@+id/creator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/updated"
android:tag="creator"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:textSize="14sp"
android:visibility="invisible" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="30dp"
android:enabled="false"
android:gravity="center_vertical|center_horizontal"
android:tag="title"
android:textColor="@color/black" >
</TextView>
<TextView
android:id="@+id/updated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:tag="updated"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue_border"
android:textSize="14sp" />
</RelativeLayout>
** lisview.xml **
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dim_foreground_disabled_holo_dark"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tableRowMessage"
android:layout_alignParentLeft="true"
android:layout_below="@+id/actionBar1"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="@drawable/chatshape"
android:dividerHeight="0dp"
android:fillViewport="true"
ptr:ptrAnimationStyle="flip"
android:cacheColorHint="#00000000"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrMode="both" />
</RelativeLayout>
答案 0 :(得分:0)
正如文件所说
平滑滚动ListView的关键是保留应用程序 主线程(UI线程)没有繁重的处理。确保你这样做 在单独的线程中进行任何磁盘访问,网络访问或SQL访问。
您可以从Making ListView Scrolling Smooth
获取更多详细信息希望它会对你有所帮助。
答案 1 :(得分:0)
发现错误。我的活动使用了自己的onSwipe控制器,阻止了平滑滚动从其运动事件处理程序返回true值。 将其更改为false(将事件传递给其他人)修复了该行为。