0

我正在尝试实现自定义 SimpleCursorAdapter 以切换 ListView 中的布局,但滚动时得到非常随机的结果。

我的问题是,当我上下滚动时,ListView 似乎是随机的,混淆了布局。例如,一行一开始可以有 listview_item_row 布局,但是当滚动进出屏幕时,它可以被 listview_item_reply_row 替换并再次返回。我不能说我真的了解 newView 是如何工作的。我已经成功地能够使用 bindView 来确定我是否要在布局中隐藏图像,但是对于它的实现以及为什么列表滚动的行为方式,新视图对我来说是黑暗的。

我的目标是有一个包含 x 个项目的列表。根据该项目是回复还是新消息,我想在该行上加载特定布局。根据行是否有图像,我想在行布局中显示/隐藏图像视图。

我在代码中省略的是导入和行布局。我正在尝试通过在 v4 支持包中使用 Fragments 和 SimpleCursorAdapter 来实现这一点。ListFragment 的行布局明显不同,但包含相同的小部件。

ListView 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_feed_header_random"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:padding="4dp"
    android:text="Allmänt"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFF" />

<!--
The frame layout is here since we will be showing either
the empty view or the list view.

-->

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/text_feed_header_random"
    android:layout_above="@+id/footer" >

    <!--
         Here is the list. Since we are using a ListActivity, we
         have to call it "@android:id/list" so ListActivity will
         find it

    -->

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

    <!-- Here is the view to show if the list is emtpy -->

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No items."
        android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>

<LinearLayout
    android:id="@+id/footer"
    style="@android:style/ButtonBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_random_post"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Gör ett inlägg!" />

    <Button
        android:id="@+id/button_random_refresh"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Refresh list!" />
</LinearLayout>
</RelativeLayout>

使用上述布局的浓缩片段:

public class RandomFragment extends ListFragment implements LOG {
private DatabaseHelper mDbHelper;
private KarenfeedCursorAdapter mAdapter;
private Cursor mCursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "RANDOMFRAGMENT START!");

    mDbHelper = new DatabaseHelper(getActivity());
    mDbHelper.open();
    mDbHelper.setTable(Posts.TABLE_RANDOM_POSTS);

    //TODO: Replace SimpleCursorAdapter with a FragmentList instead...
    mCursor = mDbHelper.getAllPostsSortedCursor();
    String[] columns = {    Posts.COLUMN_ID,        Posts.COLUMN_CREATED,   Posts.COLUMN_USER,  Posts.COLUMN_COMMENT };
    int[] to = {            R.id.imageItemPhoto,    R.id.textItemDate,      R.id.textItemUser,  R.id.textItemComment };
    int flags = 0;
    mAdapter = new FeedCursorAdapter(getActivity(), R.layout.listview_item_row, mCursor, columns, to, flags);
    this.setListAdapter(mAdapter);
    initFeedList(); // This call in the end executes mCursor = mDbHelper.getAllPostsSorted(); mAdapter.changeCursor(mCursor); mAdapter.notifyDataSetChanged();
}
}

ListFragment 连接到的 SimpleCursorAdapter:

public class FeedCursorAdapter extends SimpleCursorAdapter implements LOG {
private Context mContext;
private int mLayout;

public FeedCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
    super(context, layout, c, from, to, flags);
    // TODO Auto-generated constructor stub
    mContext = context;
    mLayout = layout;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view;
    int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
    int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
    Log.d(TAG, "id: " +id+ " parentId: " +parentId);
    int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
    if(id == parentId) {
        view = inflater.inflate(R.layout.listview_item_row, parent, false);
    } else {
        view = inflater.inflate(R.layout.listview_item_reply_row, parent, false);
    }
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Log.d(TAG, "bindView()");
    int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
    int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
    int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
    String date = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_CREATED));
    String user = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_USER));
    String comment = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_COMMENT));
    TextView dateView = (TextView) view.findViewById(R.id.textItemDate);
    TextView userView = (TextView) view.findViewById(R.id.textItemUser);
    TextView commentView = (TextView) view.findViewById(R.id.textItemComment);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageItemPhoto);

    dateView.setText(date);
    userView.setText(user);
    commentView.setText(comment);
    if(hasImage == 0) {
        imageView.setVisibility(ImageView.GONE);
    } else {
        String bitmapPath = Environment.getExternalStorageDirectory().getPath() + "/feed/" + id + "_thumb.jpg";
        Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
        imageView.setImageDrawable(bitmapDrawable);
        imageView.setVisibility(ImageView.VISIBLE);
    }
}
}
4

1 回答 1

0

在阅读了您的问题和代码后,我认为您应该知道,每当您向上或向下滚动列表视图时,它都会与适配器通信以填充进入焦点的新项目,并且对于不同数据的不同行的情况,您应该制作一个arraylist 将包含背景不同的项目的名称或 id,然后您在每次调用适配器时根据需要开始制作背景。

于 2012-04-04T08:48:29.043 回答