Странное поведение, элемент в ListView автоматически масштабируется при прокрутке? - PullRequest
0 голосов
/ 13 марта 2012

Это нормальное явление:

This is the normal

Это странно, когда я прокручиваю вид списка, когда какой-то элемент сжимается (но значения ширины и высоты не изменились.):

some items zoom-out

На этом снимке экрана элемент [1: 3] имеет тот же вид, но элемент [3] не масштабируется.масштабирование какого-либо элемента кажется рандомизацией, может быть, основанием устройства или содержимого.

Код ListView и Adapter не является чем-то особенным:

mListView = new ListView(mContext);
mListView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mProviderAdapter = new ProviderAdapter();
mListView.setAdapter(mProviderAdapter);


class ProviderAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public Object getItem(int position) {
            return mProviders.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mProviders.get(position).mMsgSpace;
        }

        @Override
        public int getViewTypeCount() {
            return 10000;
        }
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return (position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return mProviders.get(position).getUI().getView();
        }

    }

снимок экрана средства просмотра иерархии (нижний правый угол): Ссылка enter image description here

и вот пользовательские виды:

<?xml version="1.0" encoding="UTF-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#44FFFFFF" >


        <LinearLayout
            android:id="@+id/word"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#EC5800"
                    android:textSize="24sp" />

                <TextView
                    android:id="@+id/pron"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="bottom" />
            </LinearLayout>

            <TextView
                android:id="@+id/definition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:text="@string/add"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/sound"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:contentDescription="@string/audio"
                    android:padding="16dp"
                    android:text="@string/audio"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/switch_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:contentDescription="@string/audio"
                    android:padding="16dp"
                    android:text="@string/definition"
                    android:textSize="20sp" />

            </LinearLayout>
        </LinearLayout>

    <LinearLayout
        android:id="@+id/login"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Login" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/blank"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>

</ViewAnimator>

другой вид:

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ViewSwitcher1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/example_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/example_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/example_zh_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/example_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/example_zh_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <Button
            android:id="@+id/switchto_note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/note_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/note_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine" >

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/comfirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </LinearLayout>
    </LinearLayout>

</ViewSwitcher>

Ответы [ 2 ]

1 голос
/ 24 марта 2012
Комментарий

@ mice решит проблему.

@Override
public int getItemViewType(int position) {
    return IGNORE_ITEM_VIEW_TYPE
}

, потому что эта проблема вызвана механизмом повторного использования списка (немного сложнее, я все еще учусь, не могу объяснить больше :()

более точным является механизм кэширования представления.

Но он не может объяснить странное масштабирование.

После дополнительного теста я наконец обнаружил, что странное масштабирование является совместимостьюпроблема. добавьте <uses-sdk android:minSdkVersion="4"/> в AndroidManifest.xml, чтобы исправить это.

почему?

Display display = getWindowManager()
        .getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
View v  = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
String s = "DisplayMetrics:"
        + getResources().getDisplayMetrics()
        + "\nview : "
            + v.getWidth()+"*"+v.getHeight()
            + "\nscreen:" + width + "*" + height;

До:

before

После:

after

Так что, если не добавить android:minSdkVersion, View. buildDrawingCache (boolean autoScale) вернет неправильный битовый массив кэша, поэтому странномасштабирование произошло.

btw: возвращать 10000 или любые другие значения в getViewTypeCount не имело смысла, потому что я обнаружил, что система никогда не вызывает его.

0 голосов
/ 23 марта 2012

Вы, кажется, неправильно используете getViewTypeCount

Удалите следующие методы из вашего адаптера

@Override
public int getViewTypeCount() {
    return 10000;
}
@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return (position);
}

Если вы хотите, чтобы первый элемент в списке выглядел иначе, используйте addHeaderView(View v, Object data, boolean isSelectable) на вашем ListView вместо использования типов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...