0

リストビューにバインドするカスタム アダプターがあります。その部分は問題ありません。リストビューをスクロールすると、最初にスクロールすると(つまり、下にスクロールした後に上にスクロールすると)、表示されている TextView が最初のものではないため、順序が崩れます。スクロール中にアダプターで処理する必要があるものはありますか?

public class TextViewAdapter extends BaseAdapter 
{
    private Context context;

    public TextViewAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return textViews.size();
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an TextView---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        TextView tv;
        Log.d("Position",  Integer.toString(position));
        if (convertView == null) {

            tv = (TextView)textViews.get(position);
        } else {
            tv = (TextView) convertView;
        }            
        return tv;
    }
}
4

3 に答える 3

1

getViewメソッドを間違った方法で実装します。を使用する場合はconvertView、次のようにテキスト コンテンツをリセットする必要があります。

public View getView(int position, View convertView, ViewGroup parent) 
{
    TextView tv;
    Log.d("Position",  Integer.toString(position));
    if (convertView == null) {

        tv = (TextView)textViews.get(position);
    } else {
        tv = (TextView) convertView;
        // HERE, set its content
        tv.setText(textViews.get(position).getText().toString());
    }            
    return tv;
}

AListViewは、スクロール時に子ビューをリサイクルするため、一部の子ビューが非表示になり、再利用可能になります。そのため、最初は everyが null ですが、下にスクロールするconvertViewと null ではなくなります。ListViewただし、 null でないものはconvertView元のテキスト コンテンツを保持しているため、コンテンツを手動でリセットする必要があります。

ちなみに、TextViewアプリ起動時にたくさん新規作成する必要はありませStringん。

/* somewhere else */
List<String> strings = ...

public class TextViewAdapter extends BaseAdapter 
{
    /* ... */

    //---returns the number of images---
    public int getCount() {
        return strings.size();
    }

    /* ... */

    //---returns an TextView---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        TextView tv;
        Log.d("Position",  Integer.toString(position));
        if (convertView == null) {
            tv = new TextView(context);
            tv.setText(strings.get(position));
        } else {
            tv = (TextView) convertView;
            // HERE, set its content
            tv.setText(strings.get(position));
        }            
        return tv;
    }
}
于 2012-12-14T01:42:11.963 に答える
0

convertView が null の場合は、ビューを再インフレートする必要があります。それ以外の場合は、convertView のデータをリセットする必要があります。

于 2012-12-14T02:07:59.650 に答える
0

発生する主な問題は、アダプタが使用するビューへの参照を保存しようとすることです。

tv = (TextView)textViews.get(position);

問題は、アダプターのリサイクラーと競合していることです。これにより、予測できない結果が生じる可能性があります...要するに、これを行うことはできません。詳細については、Android 開発者によるWorld of ListViewをご覧ください。

つまり、アダプターが TextView 自体ではなく、TextView が表示する文字列のリストを保持するように、アプローチを変更する必要があります。

于 2012-12-14T01:48:26.427 に答える