2

listviewこの(非常に便利な)チュートリアルに基づいて、複数のタイプの行を処理するカスタムアダプターを実装しています: http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/

今、私はすべてを理解したと思っていましたが、1つのことが私を困惑させました. getView メソッドでは、リストビューの特定の行に表示する特定のレイアウトを持つビュー (グループ) であると想定される convertView を受け取ります。

public View getView(int position, View convertView, ViewGroup parent) {
    //first get the animal from our data model
    Animal animal = animals.get(position);

    //if we have an image so we setup an the view for an image row
    if (animal.getImageId() != null) {
        ImageRowViewHolder holder;
        View view;

        //don't have a convert view so we're going to have to create a new one
        if (convertView == null) {
            ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(AnimalHome.this)
                    .inflate(R.layout.image_row, null);

            //using the ViewHolder pattern to reduce lookups
            holder = new ImageRowViewHolder((ImageView)viewGroup.findViewById(R.id.image),
                        (TextView)viewGroup.findViewById(R.id.title));
            viewGroup.setTag(holder);

            view = viewGroup;
        }
        //we have a convertView so we're just going to use it's content
        else {
            //get the holder so we can set the image
            holder = (ImageRowViewHolder)convertView.getTag();

            view = convertView;
        }

        //actually set the contents based on our animal
        holder.imageView.setImageResource(animal.getImageId());
        holder.titleView.setText(animal.getName());

        return view;
    }
    //basically the same as above but for a layout with title and description
    else {
        DescriptionRowViewHolder holder;
        View view;
        if (convertView == null) {
            ViewGroup viewGroup = (ViewGroup)LayoutInflater.from(AnimalHome.this)
                    .inflate(R.layout.text_row, null);
            holder = new DescriptionRowViewHolder((TextView)viewGroup.findViewById(R.id.title),
                    (TextView)viewGroup.findViewById(R.id.description));
            viewGroup.setTag(holder);
            view = viewGroup;
        } else {
            view = convertView;
            holder = (DescriptionRowViewHolder)convertView.getTag();
        }

        holder.descriptionView.setText(animal.getDescription());
        holder.titleView.setText(animal.getName());

        return view;
    }
}

ただし、複数の種類の行が含まれている場合listview(たとえば、「mamals」、「fish」、「birds」などのタイトルの行に区切り記号が付いた動物のリスト) は、listviewconvertViewを送信すればよいかをどのように判断するのでしょうか? 2 つの完全に異なるタイプのいずれかになります。何かが私には非常に不明確です。誰か説明してくれませんか?

4

1 に答える 1

2

あなたが提供したチュートリアルから:)

さまざまな行タイプを管理するために Android アダプターが提供する 2 つの追加メソッドは次のとおりです。

getItemViewType(int position)getViewTypeCount()。リスト ビューでは、これらのメソッドを使用してビューのさまざまなプールを作成し、さまざまな種類の行に再利用します。

幸運を :)

于 2014-03-20T13:32:10.300 に答える