21

getView()の方法を教えてくださいArrayAdapter

ドキュメントを読みましたが、3つのパラメータがあります。

  • position:ビューが必要なアイテムのアダプターのデータセット内のアイテムの位置。
  • convertView:可能であれば、再利用する古いビュー。注:使用する前に、このビューがnullでなく、適切なタイプであることを確認する必要があります。このビューを変換して正しいデータを表示できない場合、このメソッドは新しいビューを作成できます。
    異種リストはビュータイプの数を指定できるため、このビューは常に正しいタイプになります(getViewTypeCount()およびgetItemViewType(int)を参照)。
  • parent:このビューが最終的にアタッチされる親

positionパラメータを理解しました。彼らが言ったように、それはビューが要求されたアイテムの位置を意味します。

どこconvertViewから来たのですか。convertViewnullかどうかをチェックする例をたくさん見てきました。isがnullの場合、行レイアウトの新しいインスタンスを膨らませ、データを入力して返します。私もその周りに頭を悩ませていると思いますが、それでも1つのことが私を困惑させます。convertViewパラメータを介して渡されるレイアウトは何ですか。初期化時に渡されるresourceパラメータArrayAdapterは?によって返された最後のレイアウトのキャッシュされたコピーはありgetView()ますか?

そして最後に。パラメータは何をしparentますか。これを利用した例はあまり見たことがありません。それらのほとんどは、単に行レイアウトを再利用/膨張させて返します。

(クリックアニメーションが含まれているので質問していますListView。具体的には、 Spotifyのドロップダウンクイックアクションメニューを複製することを目的としています。アニメーションが少し遅くなっています。この問題をしばらく診断した後、私は気づきました。これはgetView()、すべての反復で新しい行レイアウトをインフレートしているため、メソッドの完了に少し時間がかかるためです。ViewHolderしばらくの間、行レイアウトをキャッシュすることを提案した人がいます。他の例では、convertViewパラメータを再利用することを示しています。 convertViewnullの場合の行レイアウト。)

4

2 に答える 2

44

Is is a cached copy of the last layout returned by getView()?

The convertView is the view of a row that left the screen(so it isn't the last view returned by the getView method). For example, the list is first shown, in this case convertView is null, no row view was previously built and left the screen. If you scroll down, row 0 will leave the screen(will not be visible anymore), when that happens the ListView may choose to keep that view in a cache to later use it(this makes sense, as the rows of a ListView generally have the same layout with only the data being different). The reason to keep some views in a cache and later use them is because the getView method could be called a lot of times(each time the user scrolls up/down and new rows appear on the screen). If each time the row view would need to be recreated this would have resulted in a lot of objects being created which is something to avoid. In your getView method you would check convertView to see if it is null. If it's null then you must build a new row view and populate it with data, if it isn't null, the ListView has offered you a previous view. Having this previous view means you don't need to build a new row layout, instead you must populate it with the correct data, as that cached view has the old data still attached to it(you would see a lot of questions on stackoverflow where users ask why the rows of their ListView are duplicating when they scroll down).

What does the parent parameter do. I haven't seen too many examples utilising this. Most of them simply reuse/inflate a row layout and return it.

It should be used to get the correct LayoutParams for the newly inflated/built row. For example, if you inflate a layout which has a RelativeLayout as the root and you don't use the parent to get the LayoutParams you could have some problems with the row layout. To take the parent in consideration you would use :

convertView = getLayoutInflater().inflate(R.layout.row_layout, parent, false);
于 2012-09-13T06:28:29.287 に答える
2

My understanding of convertView is that it's essentially views that have been recycled because they're not being used at the moment - for example, you scroll down the list, the ones at the top aren't on the screen, so they get passed into this parameter for use when you need a new view (so you don't have to create a whole new one while having unused ones sitting around idle). iOS has a similar method called dequeueReusableCellWithIdentifier. If each row of your listview has the same structure, it's safe to cast this to the appropriate type and just update the information in it - text, images, etc. It will be a View that was previously returned by a getView() call for the same list.

My best guess (and it is admittedly a guess) with parent is that it's the view that this adapter's list is a child of. It gives you a route back to the rendering system if you need a context, access to the resource system, to pass information to or receive information from the list's parent view.

于 2012-09-13T06:28:40.253 に答える