2

我正在尝试创建一个具有多种视图类型的 ListView 适配器。我正在关注以下教程:

http://android.amberfog.com/?p=296

但是,在第一轮构建之后,代码如下:

public class MultipleItemList extends Activity
{
  private MyCustomAdapter mAdapter;
  private ListView        mListView;
  private String          mTag = "MultipleItemList";

  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );

    setContentView( R.layout.main );

    mListView = (ListView)findViewById( R.id.main_list );

    mAdapter = new MyCustomAdapter();

    for( int i = 0; i < 50; i++ )
    {
      mAdapter.addItem( "Item: " + i );
    }

    mListView.setAdapter( mAdapter );
  }

  private class MyCustomAdapter extends BaseAdapter
  {
    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater    mInflater;

    public MyCustomAdapter()
    {
      mInflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    }

    public void addItem( final String item )
    {
      mData.add( item );
      notifyDataSetChanged();
    }

    public int getCount()
    {
      return mData.size();
    }

    public String getItem( int position )
    {
      return mData.get( position );
    }

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

    public View getView( int position, View convertView, ViewGroup parent )
    {
      Log.e( mTag, "getView: " + position + " " + convertView );

      ViewHolder holder = null;

      if( convertView == null )
      {
        convertView = mInflater.inflate( R.layout.list_item, null );

        holder = new ViewHolder();

        holder.textView = (TextView)convertView.findViewById( R.id.item_text );

        convertView.setTag( holder );
      }
      else
      {
        holder = (ViewHolder)convertView.getTag();
      }

      holder.textView.setText( mData.get( position ) );

      return convertView;
    }
  }

  public static class ViewHolder
  {
    public TextView textView;
  }

}

main.xml 在哪里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

</LinearLayout>

而 list_item.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/item_text"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="TextView" 
        android:gravity="center_vertical" 
        android:paddingLeft="25dp"/>

</LinearLayout>

我希望输出与他的预期输出相似:

02-05 13:47:32.559: INFO/System.out(947): getView 0 null
02-05 13:47:32.570: INFO/System.out(947): getView 1 null
02-05 13:47:32.589: INFO/System.out(947): getView 2 null
02-05 13:47:32.599: INFO/System.out(947): getView 3 null
02-05 13:47:32.619: INFO/System.out(947): getView 4 null
02-05 13:47:32.629: INFO/System.out(947): getView 5 null
02-05 13:47:32.708: INFO/System.out(947): getView 6 null
02-05 13:47:32.719: INFO/System.out(947): getView 7 null
02-05 13:47:32.729: INFO/System.out(947): getView 8 null

但是相反,没有用户交互,只是加载屏幕,我得到以下内容:

05-09 15:24:00.347: E/MultipleItemList(11109): getView: 0 null
05-09 15:24:00.357: E/MultipleItemList(11109): getView: 1 android.widget.LinearLayout@413304d0
05-09 15:24:00.357: E/MultipleItemList(11109): getView: 2 android.widget.LinearLayout@413304d0
05-09 15:24:00.357: E/MultipleItemList(11109): getView: 3 android.widget.LinearLayout@413304d0
05-09 15:24:00.357: E/MultipleItemList(11109): getView: 4 android.widget.LinearLayout@413304d0
05-09 15:24:00.357: E/MultipleItemList(11109): getView: 5 android.widget.LinearLayout@413304d0
05-09 15:24:00.367: E/MultipleItemList(11109): getView: 6 android.widget.LinearLayout@413304d0
05-09 15:24:00.367: E/MultipleItemList(11109): getView: 7 android.widget.LinearLayout@413304d0
05-09 15:24:00.367: E/MultipleItemList(11109): getView: 8 android.widget.LinearLayout@413304d0
05-09 15:24:00.367: E/MultipleItemList(11109): getView: 9 android.widget.LinearLayout@413304d0
05-09 15:24:00.407: E/MultipleItemList(11109): getView: 0 android.widget.LinearLayout@413304d0
05-09 15:24:00.407: E/MultipleItemList(11109): getView: 1 null
05-09 15:24:00.407: E/MultipleItemList(11109): getView: 2 null
05-09 15:24:00.417: E/MultipleItemList(11109): getView: 3 null
05-09 15:24:00.417: E/MultipleItemList(11109): getView: 4 null
05-09 15:24:00.417: E/MultipleItemList(11109): getView: 5 null
05-09 15:24:00.427: E/MultipleItemList(11109): getView: 6 null
05-09 15:24:00.427: E/MultipleItemList(11109): getView: 7 null
05-09 15:24:00.437: E/MultipleItemList(11109): getView: 8 null
05-09 15:24:00.437: E/MultipleItemList(11109): getView: 9 null
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 0 null
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 1 android.widget.LinearLayout@41338b60
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 2 android.widget.LinearLayout@41338b60
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 3 android.widget.LinearLayout@41338b60
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 4 android.widget.LinearLayout@41338b60
05-09 15:24:00.457: E/MultipleItemList(11109): getView: 5 android.widget.LinearLayout@41338b60
05-09 15:24:00.467: E/MultipleItemList(11109): getView: 6 android.widget.LinearLayout@41338b60
05-09 15:24:00.467: E/MultipleItemList(11109): getView: 7 android.widget.LinearLayout@41338b60
05-09 15:24:00.467: E/MultipleItemList(11109): getView: 8 android.widget.LinearLayout@41338b60
05-09 15:24:00.467: E/MultipleItemList(11109): getView: 9 android.widget.LinearLayout@41338b60

为什么在渲染列表中的前 10 个项目时要做这么多工作?

4

1 回答 1

2

getViewActivity的调用和本教程中对seen的调用之间存在差异,getView因为本教程使用ListActivity. Adapters使用不同的活动类时,调用方式存在一些潜在差异。

我在您问题的代码中提取了代码,并且能够重现您报告的日志。当我将其更改为从 a 扩展时,ListActivity我能够重现教程中看到的日志。

查看 StackTraceElements 的getView()我可以看到以下内容:

活动日志

getView: pos[0] convertView[null]
CallerClass[android.widget.AbsListView] CallerMethod[obtainView]
CallerClass[android.widget.ListView] CallerMethod[measureHeightOfChildren]
CallerClass[android.widget.ListView] CallerMethod[onMeasure]
CallerClass[android.view.View] CallerMethod[measure]
CallerClass[android.view.ViewGroup] CallerMethod[measureChildWithMargins]
CallerClass[android.widget.LinearLayout] CallerMethod[measureChildBeforeLayout]
CallerClass[android.widget.LinearLayout] CallerMethod[measureVertical]
CallerClass[android.widget.LinearLayout] CallerMethod[onMeasure]
CallerClass[android.view.View] CallerMethod[measure]
CallerClass[android.view.ViewGroup] CallerMethod[measureChildWithMargins]
CallerClass[android.widget.FrameLayout] CallerMethod[onMeasure]
CallerClass[android.view.View] CallerMethod[measure]
CallerClass[android.widget.LinearLayout] CallerMethod[measureVertical]
CallerClass[android.widget.LinearLayout] CallerMethod[onMeasure]
CallerClass[android.view.View] CallerMethod[measure]
CallerClass[android.view.ViewGroup] CallerMethod[measureChildWithMargins]
CallerClass[android.widget.FrameLayout] CallerMethod[onMeasure]
CallerClass[com.android.internal.policy.impl.PhoneWindow$DecorView] CallerMethod[onMeasure]
CallerClass[android.view.View] CallerMethod[measure]
CallerClass[android.view.ViewRootImpl] CallerMethod[performTraversals]
CallerClass[android.view.ViewRootImpl] CallerMethod[handleMessage]
CallerClass[android.os.Handler] CallerMethod[dispatchMessage]
CallerClass[android.os.Looper] CallerMethod[loop]
CallerClass[android.app.ActivityThread] CallerMethod[main]
CallerClass[java.lang.reflect.Method] CallerMethod[invokeNative]
CallerClass[java.lang.reflect.Method] CallerMethod[invoke]
CallerClass[com.android.internal.os.ZygoteInit$MethodAndArgsCaller] CallerMethod[run]
CallerClass[com.android.internal.os.ZygoteInit] CallerMethod[main]
CallerClass[dalvik.system.NativeStart] CallerMethod[main]

ListActivity.log

getView: pos[0] convertView[null]
CallerClass[android.widget.AbsListView] CallerMethod[obtainView]
CallerClass[android.widget.ListView] CallerMethod[makeAndAddView]
CallerClass[android.widget.ListView] CallerMethod[fillDown]
CallerClass[android.widget.ListView] CallerMethod[fillFromTop]
CallerClass[android.widget.ListView] CallerMethod[layoutChildren]
CallerClass[android.widget.AbsListView] CallerMethod[onLayout]
CallerClass[android.view.View] CallerMethod[layout]
CallerClass[android.view.ViewGroup] CallerMethod[layout]
CallerClass[android.widget.FrameLayout] CallerMethod[onLayout]
CallerClass[android.view.View] CallerMethod[layout]
CallerClass[android.view.ViewGroup] CallerMethod[layout]
CallerClass[android.widget.LinearLayout] CallerMethod[setChildFrame]
CallerClass[android.widget.LinearLayout] CallerMethod[layoutVertical]
CallerClass[android.widget.LinearLayout] CallerMethod[onLayout]
CallerClass[android.view.View] CallerMethod[layout]
CallerClass[android.view.ViewGroup] CallerMethod[layout]
CallerClass[android.widget.FrameLayout] CallerMethod[onLayout]
CallerClass[android.view.View] CallerMethod[layout]
CallerClass[android.view.ViewGroup] CallerMethod[layout]
CallerClass[android.view.ViewRootImpl] CallerMethod[performTraversals]
CallerClass[android.view.ViewRootImpl] CallerMethod[handleMessage]
CallerClass[android.os.Handler] CallerMethod[dispatchMessage]
CallerClass[android.os.Looper] CallerMethod[loop]
CallerClass[android.app.ActivityThread] CallerMethod[main]
CallerClass[java.lang.reflect.Method] CallerMethod[invokeNative]
CallerClass[java.lang.reflect.Method] CallerMethod[invoke]
CallerClass[com.android.internal.os.ZygoteInit$MethodAndArgsCaller] CallerMethod[run]
CallerClass[com.android.internal.os.ZygoteInit] CallerMethod[main]
CallerClass[dalvik.system.NativeStart] CallerMethod[main]

所以我的回答是,不同活动类型的填充/呈现方式存在关键差异,ListViews这会导致对getView.

于 2012-05-10T00:08:58.833 回答