2

我的 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>
4

2 回答 2

0

发现错误。我的活动使用了自己的 onSwipe 控制器,该控制器阻止平滑滚动从其运动事件处理程序返回真值。将其更改为 false(将事件传递给其他人)修复了该行为。

于 2012-12-21T09:05:57.830 回答
0

正如文件所说

平滑滚动的 ListView 的关键是使应用程序的主线程(UI 线程)免于繁重的处理。确保您在单独的线程中执行任何磁盘访问、网络访问或 SQL 访问。

您可以从使 ListView 滚动平滑中获得更多详细信息

希望它会帮助你。

于 2012-11-19T12:53:22.170 回答