绑定的 ImageView 在滚动 RecyclerView 时消失

huangapple go评论86阅读模式
英文:

Binded ImageView disappears when scrolling RecyclerView

问题

我在我的应用程序中创建了一个排行榜活动,在其中通过 RecyclerView 显示前25名用户。

当加载完成时,它的显示效果如下:

绑定的 ImageView 在滚动 RecyclerView 时消失

然而,出现了一个问题,当我滚动 RecyclerView 时,奖牌消失了,显示如下:

绑定的 ImageView 在滚动 RecyclerView 时消失

我的适配器是:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    storageRef = FirebaseStorage.getInstance().getReference();

    otherUserID = usersID.get(position);
    otherUserName = usersName.get(position);
    if (tabCase == 0) {
        String result = String.format("%.2f", usersResult.get(position));
        otherUserResult = result;
    } else {
        otherUserResult = String.format("%.0f", usersResult.get(position));
    }
    holder.tv_PersonName.setText(otherUserName);
    holder.tv_Result.setText(otherUserResult);
    holder.tv_Ranking.setText(Integer.toString(position + 1));

    if (position == 0) {
        holder.iv_Medal.setImageResource(R.drawable.ic_gold_medal);
    } else if (position == 1) {
        holder.iv_Medal.setImageResource(R.drawable.ic_silver_medal);
    } else if (position == 2) {
        holder.iv_Medal.setImageResource(R.drawable.ic_bronze_medal);
    } else {
        holder.iv_Medal.setVisibility(View.GONE);
    }
    storageRef.child("/Images/" + otherUserID + ".jpg").getDownloadUrl().addOnSuccessListener(uri -> Glide.with(mView.getContext()).load(uri.toString()).into(holder.iv_PersonPic)).addOnFailureListener(e -> {
        Glide.clear(holder.iv_PersonPic);
        holder.iv_PersonPic.setImageDrawable(null);
    });

    holder.itemView.setOnClickListener(v -> {
        Intent intent = new Intent(mView.getContext(), UsersPreviewActivity.class);
        intent.putExtra(AppConstants.PREVIEW_KEY, otherUserID);
        mView.getContext().startActivity(intent);
    });

}

有办法让这些奖牌始终固定在前三名吗?

谢谢

英文:

I've created a Leaderboard activity in my app where I show the top 25 users in a RecyclerView.

It all works well when it is loaded and it appears as:

绑定的 ImageView 在滚动 RecyclerView 时消失

However, for some reason, when I scroll the RecyclerView it removes the medals and it shows:

绑定的 ImageView 在滚动 RecyclerView 时消失

My adapter is:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
storageRef = FirebaseStorage.getInstance().getReference();
otherUserID = usersID.get( position );
otherUserName = usersName.get( position );
if (tabCase == 0) {
String result = String.format( "%.2f", usersResult.get( position ) );
otherUserResult = result;
} else {
otherUserResult = String.format( "%.0f", usersResult.get( position ) );
}
holder.tv_PersonName.setText( otherUserName );
holder.tv_Result.setText( otherUserResult );
holder.tv_Ranking.setText( Integer.toString( position + 1 ) );
if (position == 0) {
holder.iv_Medal.setImageResource( R.drawable.ic_gold_medal );
} else if (position == 1) {
holder.iv_Medal.setImageResource( R.drawable.ic_silver_medal );
} else if (position == 2) {
holder.iv_Medal.setImageResource( R.drawable.ic_bronze_medal );
} else {
holder.iv_Medal.setVisibility( View.GONE );
}
storageRef.child( "/Images/" + otherUserID + ".jpg" ).getDownloadUrl().addOnSuccessListener( uri -> Glide.with( mView.getContext() ).load( uri.toString() ).into( holder.iv_PersonPic ) ).addOnFailureListener( e -> {
Glide.clear( holder.iv_PersonPic );
holder.iv_PersonPic.setImageDrawable( null );
} );
holder.itemView.setOnClickListener( v -> {
Intent intent = new Intent( mView.getContext(), UsersPreviewActivity.class );
intent.putExtra( AppConstants.PREVIEW_KEY, otherUserID );
mView.getContext().startActivity( intent );
} );
}

Any way to make those medals stick to the first 3?

Thank you

答案1

得分: 0

在其他条件中调用以下代码,并将视图设置为可见:

holder.iv_Medal.setVisibility(View.VISIBLE);

类似于:

if (position == 0) {
    holder.iv_Medal.setImageResource(R.drawable.ic_gold_medal);
    holder.iv_Medal.setVisibility(View.VISIBLE);
} else if (position == 1) {
    holder.iv_Medal.setImageResource(R.drawable.ic_silver_medal);
    holder.iv_Medal.setVisibility(View.VISIBLE);
} else if (position == 2) {
    holder.iv_Medal.setImageResource(R.drawable.ic_bronze_medal);
    holder.iv_Medal.setVisibility(View.VISIBLE);
} else {
    holder.iv_Medal.setVisibility(View.GONE);
}
英文:

call the following in other if conditions and set the view to VISIBLE

holder.iv_Medal.setVisibility( View.VISIBLE);

like so:

if (position == 0) {
holder.iv_Medal.setImageResource( R.drawable.ic_gold_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else if (position == 1) {
holder.iv_Medal.setImageResource( R.drawable.ic_silver_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else if (position == 2) {
holder.iv_Medal.setImageResource( R.drawable.ic_bronze_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else {
holder.iv_Medal.setVisibility( View.GONE );
}

huangapple
  • 本文由 发表于 2020年10月28日 05:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64563306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定