列表视图的 Android LazyList 初始加载在滚动之前不显示图像

Posted

技术标签:

【中文标题】列表视图的 Android LazyList 初始加载在滚动之前不显示图像【英文标题】:Android LazyList initial load of listview doesn't show images until scrolling 【发布时间】:2013-09-25 05:46:24 【问题描述】:

我目前正在从这里使用 LazyList 视图:https://github.com/thest1/LazyList

一切都很好,但列表视图的初始加载。没有图像正在加载。我实际上必须向下滚动然后备份以填充图像。下面是我正在使用的适配器的代码。我在这里做错了什么?

更新:请注意,我取出了图像视图的标签以查看它是否有帮助,但我仍然得到相同的结果。

public class BattleAdapter extends ArrayAdapter<Battle> 

private ArrayList<Battle> data = null;
private Context context;
private int layoutResourceId;
private ImageLoader imageLoader;

public BattleAdapter(Context context, int layoutResourceId,
        ArrayList<Battle> data) 
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
    this.imageLoader = new ImageLoader(context);


static class BattleHolder 
    public CropImageView imageRight;
    public CropImageView imageLeft;
    public TextView displayNameRight;
    public TextView displayNameLeft;
    public TextView numComments;
    public TextView numVotes;
    public TextView timeLeft;
    public ImageView winnerImg1;
    public TextView winnerTxt1;
    public ImageView winnerImg2;
    public TextView winnerTxt2;
    public TextView tiedTxt1;
    public TextView tiedTxt2;


@Override
public View getView(int position, View convertView, ViewGroup parent) 

    BattleHolder holder = null;

    if (convertView == null) 

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        holder = new BattleHolder();

        convertView = inflater.inflate(R.layout.versus_list_item, null);
        holder.imageLeft = (CropImageView) convertView
                .findViewById(R.id.versus_challenger);
        holder.imageLeft.setClickable(false);
        holder.imageRight = (CropImageView) convertView
                .findViewById(R.id.versus_challenged);
        holder.imageRight.setClickable(false);
        holder.displayNameLeft = (TextView) convertView
                .findViewById(R.id.versus_challenger_name);
        holder.displayNameRight = (TextView) convertView
                .findViewById(R.id.versus_challenged_name);
        holder.numVotes = (TextView) convertView
                .findViewById(R.id.num_votes);
        holder.numComments = (TextView) convertView
                .findViewById(R.id.num_comments);
        holder.timeLeft = (TextView) convertView
                .findViewById(R.id.time_left);
        holder.winnerImg1 = (ImageView) convertView
                .findViewById(R.id.winner_img1);
        holder.winnerImg2 = (ImageView) convertView
                .findViewById(R.id.winner_img2);
        holder.winnerTxt1 = (TextView) convertView
                .findViewById(R.id.winner_img1_txt);
        holder.winnerTxt2 = (TextView) convertView
                .findViewById(R.id.winner_img2_txt);
        holder.tiedTxt1 = (TextView) convertView
                .findViewById(R.id.tie_txt1);
        holder.tiedTxt2 = (TextView) convertView
                .findViewById(R.id.tie_txt2);
        holder.timeLeft.setSelected(true);

        convertView.setTag(holder);
     else 
        holder = (BattleHolder) convertView.getTag();
    

    Battle battle = data.get(position);

    if (battle != null) 

        User challenger = battle.getChallenger();
        User challenged = battle.getChallenged();

        holder.displayNameLeft.setText(challenger.getDisplayName());

        holder.displayNameRight.setText(challenged.getDisplayName());

        holder.numComments.setText(Integer.toString(battle
                .getCommentCount()));
        holder.numVotes.setText(Integer.toString(battle.getVoteCount()));

        if (battle.getTimeLeft() != null) 

            if (battle.getTimeLeft().startsWith("-")) 
                holder.timeLeft.setText("Time's up. Calculating winner");
             else if (battle.getTimeLeft().endsWith("responded")) 

                String newString = String.format(battle.getTimeLeft(),
                        challenged.getDisplayName());
                holder.timeLeft.setText(newString);

             else
                holder.timeLeft.setText(battle.getTimeLeft());
        

        imageLoader.DisplayImage(challenger.getPicPath(), holder.imageLeft);

        imageLoader.DisplayImage(challenged.getPicPath(), holder.imageRight);

        if (battle.getWinnerID() == -5) 
            holder.winnerImg1.setVisibility(View.VISIBLE);
            holder.winnerImg2.setVisibility(View.VISIBLE);
            holder.winnerTxt2.setVisibility(View.INVISIBLE);
            holder.winnerTxt1.setVisibility(View.INVISIBLE);
            holder.tiedTxt1.setVisibility(View.VISIBLE);
            holder.tiedTxt2.setVisibility(View.VISIBLE);
        

        if (battle.getWinnerID() != 0 && battle.getWinnerID() != -1) 

            if (challenger.getUserID() == battle.getWinnerID()) 
                holder.winnerImg1.setVisibility(View.VISIBLE);
                holder.winnerTxt1.setVisibility(View.VISIBLE);
                holder.winnerImg2.setVisibility(View.VISIBLE);
                holder.winnerTxt2.setVisibility(View.INVISIBLE);
                holder.tiedTxt1.setVisibility(View.INVISIBLE);
                holder.tiedTxt2.setVisibility(View.INVISIBLE);

            

            else if (challenged.getUserID() == battle.getWinnerID()) 
                holder.winnerImg2.setVisibility(View.VISIBLE);
                holder.winnerTxt2.setVisibility(View.VISIBLE);
                holder.winnerImg1.setVisibility(View.VISIBLE);
                holder.winnerTxt1.setVisibility(View.INVISIBLE);
                holder.tiedTxt1.setVisibility(View.INVISIBLE);
                holder.tiedTxt2.setVisibility(View.INVISIBLE);

            
        

        if (battle.getWinnerID() == 0 || battle.getWinnerID() == -1) 
            holder.winnerImg1.setVisibility(View.INVISIBLE);
            holder.winnerTxt1.setVisibility(View.INVISIBLE);
            holder.winnerImg2.setVisibility(View.INVISIBLE);
            holder.winnerTxt2.setVisibility(View.INVISIBLE);
            holder.tiedTxt1.setVisibility(View.INVISIBLE);
            holder.tiedTxt2.setVisibility(View.INVISIBLE);
            Log.i("BattleAdapter", "No winner");
        

    

    return convertView;


【问题讨论】:

【参考方案1】:

为此使用 androidQuery。 ImageLoader 不是一个通用的 ImageLoader,Android 已经为我们提供了 AndroidQuery 和它的使用方法,非常快速和高效。

从这里下载 jar:http://code.google.com/p/android-query/ 将此 jar 添加到您的构建路径。

private AQuery androidAQuery;
androidAQuery=new AQuery(this);
                androidAQuery.id(YOUR IMAGE VIEW).image(YOUR IMAGE URL, true, true,DEFUALT IMAGE(optional));

【讨论】:

【参考方案2】:

试试这个,在你的 manifest.xml 文件中添加以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【讨论】:

问题是关于在列表视图的初始加载时加载图像的困难。描述表明,在列表稍微缩放后,图像可以正常加载。

以上是关于列表视图的 Android LazyList 初始加载在滚动之前不显示图像的主要内容,如果未能解决你的问题,请参考以下文章

惰性列表布局

Jetpack Compose LazyList 未正确显示项目

未创建 Android listview 内容视图

如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图

使用适用于 Android 应用的自定义适配器将项目动态添加到列表视图

如何通过 Intent 初始化 Android DatePicker?