2012-03-13 195 views
0

这是正常的:(但宽度和高度的值没有改变)奇怪的行为,项目在滚动时ListView自动缩放?

This is the normal

这是奇怪的,当我滚动列表视图的一些项目收缩:

some items zoom-out

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

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

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

    } 

层次浏览器(右下角)的截图:Link 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> 
+0

你可以发布ListView的自定义行的XML代码? – Meatje 2012-03-13 09:00:07

+0

好的,我已更新该帖子。 – DouO 2012-03-13 09:08:02

+0

你真的有10000种不同的风景吗? – Blackbelt 2012-03-17 10:45:19

回答

1

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

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

,因为这个问题的原因由ListView的循环机制(稍微复杂一点,我还在学习,不能说明更多的:()

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

但它不能解释的怪异缩放。

更多的测试之后,我终于发现了奇怪的缩放是一个兼容性问题。 添加<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,查看。 buildDrawingCache(boolean autoScale)将返回错误的缓存位图,这就是奇怪的缩放发生的原因。

btw:返回10000或getViewTypeCount中的任何其他值没有意义。因为我发现系统从来不会调用它。

0

你似乎滥用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而不是使用类型。

+0

我相信getViewTypeCount()指的是你的listview将容纳多少个不同的视图。例如),在聊天应用程序中,您可能需要使用不同的聊天泡泡,其中一个用于发送消息,另一个用于发送给您的消息。在这种情况下,我相信getViewTypeCount应该返回2. getItemViewType()将通过告诉listview在某个位置使用哪个viewType来帮助回收视图。例如)返回1发送给您的消息,返回2您发送的消息。 – 2012-11-15 18:12:09

+0

这是正确的@Zachary,但对于OP示例,他不需要类型的视图,但他宣称10000视图类型,显然不适合它的使用。 – marmor 2012-11-17 12:25:45

+0

你是正确的,它不需要这些功能被超载。我混淆了实现ListAdapter接口,您必须具有这些功能与SubAdding的BaseAdapter它是可选的覆盖。作为除去函数的替代方法,可以将getViewTypeCount()的返回值设置为1,将getItemViewType()设置为0,这将解决问题,同时保持相同的结构并允许混合不同的视图。 – 2012-11-20 00:28:25