Android:当列表项滚动到屏幕外时,我的 ListView 样式如何被删除

标签 android listview scroll listitem

目前,我有一个自定义列表适配器,它具有一些逻辑,可以根据变量在一行中隐藏/显示某个 ImageView。

最初,逻辑在应用程序首次启动时起作用。 (相应地隐藏/显示 ImageView)。

但是一旦我上下滚动屏幕,最终,所有行的 ImageView 将永远隐藏。

有谁知道如何解决这个问题?

这是我的适配器:

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        PostHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new PostHolder();
            holder.postThumb = (ImageView)row.findViewById(R.id.post_Thumb);
            holder.postComments = (TextView)row.findViewById(R.id.post_comments);
            holder.postInfo = (TextView)row.findViewById(R.id.item_subtitle);
            holder.postScore = (TextView)row.findViewById(R.id.post_score);
            holder.postTitle = (TextView)row.findViewById(R.id.item_title);
            holder.postThumbHolder = (LinearLayout)row.findViewById(R.id.post_Thumb_holder);
            row.setTag(holder);
        }
        else
        {
            holder = (PostHolder)row.getTag();
        }

        HashMap<String, String> post = data.get(position);

        if(post.get("thumbnail").equals("default") || post.get("thumbnail").equals("self")){
            holder.postThumbHolder.setVisibility(View.GONE);
        }

        holder.postComments.setText(post.get("comments"));
        holder.postInfo.setText(post.get("info"));
        holder.postScore.setText(post.get("score"));
        holder.postTitle.setText(post.get("title"));

        return row;
    }


    static class PostHolder
    {
        LinearLayout postThumbHolder;
        ImageView postThumb;
        TextView postComments;
        TextView postScore;
        TextView postTitle;
        TextView postInfo;      
    }

最佳答案

它与 android 如何在您开始滚动时立即将列表项的背景转换为位图有关。为确保您的 ImageView 在滚动后可见,您还必须在每次调用适配器 getView 时设置该属性,在变量检查的其他部分执行此操作

if(post.get("thumbnail").equals("default") || post.get("thumbnail").equals("self")){
        holder.postThumbHolder.setVisibility(View.GONE);
} else {
        holder.postThumbHolder.setVisibility(View.VISIBLE);
}

关于Android:当列表项滚动到屏幕外时,我的 ListView 样式如何被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506387/

相关文章:

android - 单元测试 Bolts Tasks With Continuations (Android)

android - Android ListView 中未显示顶部和底部分隔符

android - Android Q(10) 中带有 MediaRecorder 的 AccessibilityService 30 秒音频,而调用接收(应答)无法工作

安卓运营商app e-sim激活码使用

android - 如何取消选中删除 Android 中项目的复选框?

java - 位图在 Android 应用程序中加载和显示需要很长时间

"-webkit-overflow-"滚动 :touch;"the scrolling just stops working sometimes… 的 iPad 网站

Javascript滚动到滚动元素

Android BottomSheet 禁用手动滚动

java - 指向应用程序内部 Activity 的 anchor 标记超链接