3

我尝试使用自定义 CursorLoader 将 ListView 项目加载到列表中。问题是,如果我滚动到底部并再次向上滚动,Android 会为每个列表项添加多个新图像。我的问题是,如何防止 Android 将更多图像加载到列表中。
第 1 列应该总是只有一张图片,第 2 列应该有两张图片...

主.java

public class Main extends FragmentActivity
    implements LoaderManager.LoaderCallbacks<Cursor> {

private Context mCtx       = null;
private MyAdapter mAdapter = null;

private final static String COLUMN_A = "a";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCtx     = this;
    mAdapter = new MyAdapter(mCtx);

    ((ListView) findViewById(R.id.listview)).setAdapter(mAdapter);
    getSupportLoaderManager().initLoader(0, null, this);
}


@Override
public Loader<Cursor> onCreateLoader(int loader_id, Bundle bundle) {

    return new MyCursorLoader(mCtx) {

        @Override
        public Cursor loadInBackground() {
            MatrixCursor m = new MatrixCursor(new String[]{"_id", COLUMN_A});
            m.addRow(new String[]{"1", "Column 1"});
            m.addRow(new String[]{"2", "Column 2"});
            m.addRow(new String[]{"3", "Column 3"});
            m.addRow(new String[]{"4", "Column 4"});
            m.addRow(new String[]{"5", "Column 5"});
            return m;
        }
    };
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
    mAdapter.changeCursor(c);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.changeCursor(null);
}


private class MyAdapter extends CursorAdapter {
    private final LayoutInflater mInflater;

    public MyAdapter(final Context ctx) {
        super(ctx, null, 0);
        mInflater = LayoutInflater.from(ctx);
    }

    @Override
    public View newView(Context ctx, Cursor c, ViewGroup parent) {
        return mInflater.inflate(R.layout.item, parent, false);
    }

    @Override
    public void bindView(final View v, final Context ctx, final Cursor c) {
        LinearLayout parent = (LinearLayout) v;
        final int position = c.getPosition();

        Log.i("MyAdapter", "Cursor position: "+ position
                + "\tImages: "+ (parent.getChildCount() -1));

        ((TextView) v.findViewById(R.id.content))
            .setText(c.getString(c.getColumnIndex(COLUMN_A)));


        LinearLayout ll = new LinearLayout(ctx);
        ll.setOrientation(LinearLayout.VERTICAL);

        for (int i=0; i<(position+1); i++) {
            SystemClock.sleep(i * 100);
            ImageView img = new ImageView(ctx);
            img.setImageResource(R.drawable.ic_launcher);
            ll.addView(img);
        }

        parent.addView(ll);
    }
}


private abstract class MyCursorLoader extends CursorLoader {

    public MyCursorLoader(Context ctx) {
        super(ctx);
    }

    @Override
    public abstract Cursor loadInBackground();
}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <ListView android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    </ListView>

</RelativeLayout>

项目.xml

<?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="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="30dip"
    android:text="---"
    />


</LinearLayout>


↓ 在 Galaxy S1 上尝试了 Diogo Bento 的建议后: 在此处输入图像描述

4

2 回答 2

4

这不是一个有效的解决方案,但您可以删除LinearLayout. 一个有效的解决方案是检查是否LinearLayout有与位置不同的数量childs并添加或删除图像以获得您想要的图像数量。

**parent.removeViewAt(1);**
LinearLayout ll = new LinearLayout(ctx);
ll.setOrientation(LinearLayout.VERTICAL);

 for (int i=0; i<(position+1); i++) {
        SystemClock.sleep(i * 100);
        ImageView img = new ImageView(ctx);
        img.setImageResource(R.drawable.ic_launcher);
        ll.addView(img);
 }
parent.addView(ll);
于 2013-06-20T18:38:32.360 回答
1

尝试这个:

public void bindView(final View v, final Context ctx, final Cursor c) {
LinearLayout parent = (LinearLayout) v;
final int position = c.getPosition();

Log.i("MyAdapter", "Cursor position: "+ position
        + "\tImages: "+ (parent.getChildCount() -1));

((TextView) v.findViewById(R.id.content))
    .setText(c.getString(c.getColumnIndex(COLUMN_A)));
//use a linear layout defined in the item.xml instead of dynamically create one
LinearLayout ll = (LinearLayout)v.findViewById(R.id.linearlayout);
for (int i=0; i<(position+1); i++) {
    SystemClock.sleep(i * 100);
    ImageView img = new ImageView(ctx);
    img.setImageResource(R.drawable.ic_launcher);
    //assign an id to the image to add
    int id = i+10000;
    img.setId(id);
    //check if the image has been already added.
    if(ll.findViewById(id) == null){
        ll.addView(img);
    }
}

}

项目.xml:

<?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="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="30dip"
    android:text="---"/>
    <LinearLayout 
        android:id="@+id/linearlayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>
</LinearLayout>

编辑:

如果您像这样稍微更改 for 循环,您当然可以跳过图像的创建和下次滚动时的睡眠:

    for (int i=0; i<(position+1); i++) {
        int id = i+10000;
    //check if the image has been already added.
    if(ll.findViewById(id) == null){ 
        SystemClock.sleep(i * 100);
        ImageView img = new ImageView(ctx);
        img.setImageResource(R.drawable.ic_launcher);
        //assign an id to the image to add
        img.setId(id);
        ll.addView(img);
    }
}
于 2013-06-27T07:40:43.710 回答