【问题标题】:Inflating Multiple layouts with different views in listview using BaseAdapter in android在android中使用BaseAdapter在listview中使用不同视图膨胀多个布局
【发布时间】:2023-03-20 06:04:01
【问题描述】:

我一直在尝试实现一个列表视图实现,其中根据使用 BaseAdapter 在活动中输入到 ArrayList 的数据显示文本视图或显示图像视图。

我对自定义基础适配器的实现:

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import com.android.pls.R;
import com.android.pls.common.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author ANKIT
 *
 */
public class CommentSplashListViewAdapter extends BaseAdapter 
{
    Context context;
    LayoutInflater inflater;
    ImageLoader imageLoader;
    private List<CommentSplashList> comments = new ArrayList<CommentSplashList>();
    private ArrayList<CommentSplashList> arraylist;

    public CommentSplashListViewAdapter(Context context, List<CommentSplashList> comments)
    {
        this.context = context;
        this.comments = comments;
        this.arraylist = new ArrayList<CommentSplashList>();
        imageLoader = new ImageLoader(context);
    }

    public void setListItems(List<CommentSplashList> commentLists) 
    {
        this.comments = commentLists;
        this.arraylist.addAll(commentLists);
    }

    @Override
    public int getCount() 
    {
        return comments.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return comments.get(position);
    }

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

    @Override
    public int getViewTypeCount() 
    { 
        return 2;
    }

    @Override
    public int getItemViewType(int position) 
    {
        CommentSplashList comment = comments.get(position);

        if(comment.getContainsUrl() == false)
            return 0;
        else
            return 1;

    }

    public static class WithoutSplashbackViewHolder 
    {
        TextView creatorUserName;
        ImageView creatorProfilePic;
        TextView userComment;
        TextView timeAgo;
        //ImageView splashbackImage;
    }

    public static class WithSplashbackViewHolder 
    {
        TextView creatorUserName;
        ImageView creatorProfilePic;
        //TextView userComment;
        TextView timeAgo;
        ImageView splashbackImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        CommentSplashList comment = comments.get(position);

        WithoutSplashbackViewHolder holder1 = null;
        WithSplashbackViewHolder holder2 = null;

        View v = convertView;

        if (getItemViewType(position) == 0) 
        {
            holder1 = new WithoutSplashbackViewHolder();

            if (v == null) 
            {
                //GET View 1
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_without_splashback, null);

                v = viewGroup;


                holder1.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
                holder1.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
                holder1.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);

                holder1.userComment = (TextView) v.findViewById(R.id.userComment);
                //holder.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);


            } 

            // Set the results into TextViews
            holder1.creatorUserName.setText(comment.getCreatorUserName());
            holder1.timeAgo.setText(comment.getTimeAgo());
            // Set the results into ImageView
            if(comment.getCreatorProfilePic() != "null")
                imageLoader.DisplayImage(comment.getCreatorProfilePic(),
                        holder1.creatorProfilePic);
            holder1.userComment.setText(comment.getUserComment());

            return v;


        } 
        else 
        {
            holder2 = new WithSplashbackViewHolder();


            if (v == null) 
            {
                //GET View 2
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_with_splashback, null);

                v = viewGroup;


                holder2.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
                holder2.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
                holder2.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);

                //holder.userComment = (TextView) v.findViewById(R.id.userComment);
                holder2.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);

            } 

            // Set the results into TextViews
            holder2.creatorUserName.setText(comment.getCreatorUserName());
            //holder2.creatorUserName.setText("username");
            holder2.timeAgo.setText(comment.getTimeAgo());
            // Set the results into ImageView
            if(comment.getCreatorProfilePic() != "null")
                imageLoader.DisplayImage(comment.getCreatorProfilePic(),
                        holder2.creatorProfilePic);
            imageLoader.DisplayImage(comment.getSplashbackImage(),
                    holder2.splashbackImage);

            return v;
        }
    }



}

我还尝试删除以下行:if (v == null) 及其循环,它删除了错误,但它没有使用splashbackviewholder 膨胀视图。

我在堆栈和在线上进行了搜索,但我找到了解决方案,他们只有多个布局,只有 textviews 而不是 imageviews。

【问题讨论】:

    标签: android android-listview baseadapter layout-inflater android-inflate


    【解决方案1】:

    我猜你误解了 View Holders 的含义。 您应该将 ViewHolder 添加为 convertView 的标签。 一般逻辑如下:

    if (converView == null) {
        convertView = inflateView();
        holder = new Holder(convertView);
        converView.setTag(holder);
    } else {
        holder = (Holder)convertView.getTag();
    }
    

    另外,如果 getItemViewTypeCount() = 2,那么您必须有 2 个项目类型,而不是 3(您有 0、1、2)。

    请告诉我这个方法:

    comment.getContainsUrl()
    

    【讨论】:

    • 我刚才也试过了,但结果是一样的。它没有为 -else(getItemViewType(position) == 1) 块提供输出。
    • 如果你在行上放了一个断点 return 1;在 getItemViewType 中,它会停在那里吗?如果没有,那就是你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多