怪异的行为,在项目的ListView自动缩放滚动时? [英] Weird behavior,Item in ListView auto-scale when scrolling?

查看:189
本文介绍了怪异的行为,在项目的ListView自动缩放滚动时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是正常的:

这是怪异的,当我滚动列表视图的一些项目收缩(但宽度和高度的值没有变化。)

在这个截图项[1:3]是同一种,但项目[3]并没有形成规模。缩放哪个项目似乎随机或许基础设备或内容。

ListView和适配器的code是没有什么特别的:

  mListView =新的ListView(mContext);
mListView.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
mProviderAdapter =新ProviderAdapter();
mListView.setAdapter(mProviderAdapter);
类ProviderAdapter扩展了BaseAdapter {        @覆盖
        公众诠释的getCount(){
            返回mCount;
        }        @覆盖
        公共对象的getItem(INT位置){
            返回mProviders.get(位置);
        }        @覆盖
        众长getItemId(INT位置){
            返回mProviders.get(位置).mMsgSpace;
        }        @覆盖
        公众诠释getViewTypeCount(){
            返回10000;
        }
        @覆盖
        公众诠释getItemViewType(INT位置){
            // TODO自动生成方法存根
            返回(位置);
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            返回mProviders.get(位置).getUI()getView();
        }    }

层次浏览器(右下角)的截图:链接

和这里的自定义视图:

 <?XML版本=1.0编码=UTF-8&GT?;
< ViewAnimator的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=#44FFFFFF>
        <的LinearLayout
            机器人:ID =@ + ID /词
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直>            <的LinearLayout
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:方向=横向>                <的TextView
                    机器人:ID =@ + ID /内容
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文字颜色=#EC5800
                    机器人:TEXTSIZE =24sp/>                <的TextView
                    机器人:ID =@ + ID / PRON
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:重力=底/>
            < / LinearLayout中>            <的TextView
                机器人:ID =@ + ID /定义
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =20SP/>            <的LinearLayout
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:方向=横向>                <按钮
                    机器人:ID =@ + ID /加
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:填充=16DP
                    机器人:文字=@字符串/加
                    机器人:TEXTSIZE =20SP/>                <按钮
                    机器人:ID =@ + ID /声音
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginLeft =16DP
                    机器人:contentDescription =@字符串/音频
                    机器人:填充=16DP
                    机器人:文字=@字符串/音频
                    机器人:TEXTSIZE =20SP/>                <按钮
                    机器人:ID =@ + ID / switch_button
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginLeft =16DP
                    机器人:contentDescription =@字符串/音频
                    机器人:填充=16DP
                    机器人:文字=@字符串/定义
                    机器人:TEXTSIZE =20SP/>            < / LinearLayout中>
        < / LinearLayout中>    <的LinearLayout
        机器人:ID =@ + ID /登入
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>        <按钮
            机器人:ID =@ + ID / login_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =CENTER_HORIZONTAL
            机器人:文字=登录/>
    < / LinearLayout中>    <的LinearLayout
        机器人:ID =@ + ID /空白
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
    < / LinearLayout中>< / ViewAnimator>

另一种观点认为:

 <?XML版本=1.0编码=UTF-8&GT?;
< ViewSwitcher的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / ViewSwitcher1
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的LinearLayout
        机器人:ID =@ + ID / example_panel
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        <的TextView
            机器人:ID =@ + ID / example_1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=中文字
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>        <的TextView
            机器人:ID =@ + ID / example_zh_1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=TextView的/>        <的TextView
            机器人:ID =@ + ID / example_2
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=中文字
            机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>        <的TextView
            机器人:ID =@ + ID / example_zh_2
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=TextView的/>        <按钮
            机器人:ID =@ + ID / switchto_note
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=按钮/>
    < / LinearLayout中>    <的LinearLayout
        机器人:ID =@ + ID / note_panel
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        <的EditText
            机器人:ID =@ + ID / note_input
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            安卓的inputType =textMultiLine>            < requestFocus的/>
        < /&的EditText GT;        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>            <按钮
                机器人:ID =@ + ID / comfirm
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=按钮/>            <按钮
                机器人:ID =@ + ID /取消
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=按钮/>
        < / LinearLayout中>
    < / LinearLayout中>< / ViewSwitcher>


解决方案

@mice的评论将会解决这个问题。

  @覆盖
公众诠释getItemViewType(INT位置){
    回报IGNORE_ITEM_VIEW_TYPE
}

由于这个问题造成的ListView的回收机制(有点复杂,我还在学习,不能说明更多的:()

更准确的是视图的缓存机制。

但它不能解释怪异的比例。

更多的测试后,我终于发现了奇怪的缩放是兼容性问题。
添加<采用-SDK机器人:=的minSdkVersion4/> 来的Andr​​oidManifest.xml将修复它。

为什么?

 显示显示= getWindowManager()
        .getDefaultDisplay();
INT宽度= display.getWidth();
INT高度= display.getHeight();
视图V = getWindow()findViewById(Window.ID_ANDROID_CONTENT)。
字符串s =DisplayMetrics:
        + getResources()。getDisplayMetrics()
        +\\的nView:
            + v.getWidth()+*+ v.getHeight()
            +\\ nscreen:+宽+*+高度;

所以,如果没有添加的android:的m​​inSdkVersion ,查看<一个href=\"https://github.com/CyanogenMod/android_frameworks_base/blob/gingerbread/core/java/android/view/View.java#L6533\"相对=nofollow> buildDrawingCache(布尔自动量程)将返回错误的缓存位图,这就是为什么怪异的比例happend。

BTW:在getViewTypeCount返回10000或任何其他值没有任何意义。因为我发现系统永远不会调用它。

This is the normal:

This is the weird,when I scroll the listview some item shrinked(But the value of width and height did not change.):

In this screenshot item[1:3] is same kind,but item[ 3] didn't scale. which item scaled seem randomization maybe base the device or content.

The code of ListView and Adapter is nothing special :

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();
        }

    }

the screenshot of Hierarchy Viewer(Lower Right Corner):Link

and here is the custom views:

<?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>

another view:

<?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>

解决方案

@mice 's comment will fix the problem.

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

because this problem cause by the listview's recycle mechanism(a little more complicated ,I'm still learning,Can't explain more :( )

more accurate is the view's cache mechanism.

But It can't explain the weird scaling.

After more test, I finally find the weird scaling is a compatibility problem. add <uses-sdk android:minSdkVersion="4"/> to AndroidManifest.xml will fix it .

why?

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:

So if didn't add the android:minSdkVersion, View.buildDrawingCache(boolean autoScale) will return the wrong cache bitmap,that's why the weird scaling happend.

btw:return 10000 or any other values in the getViewTypeCount didn't make sense. because I found the system never call it.

这篇关于怪异的行为,在项目的ListView自动缩放滚动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆