ViewHolder 在偶数和奇数位置上膨胀布局

2024-04-08

我需要两个布局来使用 ViewHolder 根据列表视图项的偶数和奇数位置进行膨胀。在偶数位置,我需要不同的布局,在奇数位置,另一个具有相同元素但布局不同的布局。然而,我实现了它,它给了我不同位置的随机布局,无论它们的位置如何。需要做什么来解决它。谢谢。

public SimpleAdapter(ArrayList<WishListData> wishDataList, Context context,
        ListView swipelistview) {
    super(context, android.R.layout.simple_list_item_1, wishDataList);
    notifyDataSetChanged();
    SimpleAdapter.wishListData = wishDataList;
    this.swipelistview = swipelistview;

    mPreferences = new Preferences(context);
    SCREEN_WIDTH = mPreferences.getScreenWidth();
    SCREEN_HEIGHT = mPreferences.getScreenHeight();
    mFunctions = new UserFunctions();
    this.context = context;
    imageloader1 = new ImageLoader1(context);
    userImageLoader = new UserImageLoader(context); 

}

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

    ViewHolder viewHolder = null;
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        System.out.println("position "+ position);
        if ((position % 2) == 0) {

            row = inflater.inflate(R.layout.single_wish_view_right, parent,
                    false);

        } else if ((position % 2) == 1){
            row = inflater
                    .inflate(R.layout.single_wish_view, parent, false);

        }
        viewHolder = new ViewHolder();
        viewHolder.back = (LinearLayout) row
                .findViewById(R.id.back);
        viewHolder.lenear = (LinearLayout) row
                .findViewById(R.id.linear);

        viewHolder.front = (RelativeLayout) row
                .findViewById(R.id.front);
        viewHolder.likeButton = (ImageButton) row
                .findViewById(R.id.likemain);
        viewHolder.deathWish = (TextView) row
                .findViewById(R.id.death_wish);
        viewHolder.time = (TextView) row
                .findViewById(R.id.time_wish);
        viewHolder.name = (TextView) row.findViewById(R.id.name1);
        viewHolder.commentButton = (ImageButton) row
                .findViewById(R.id.comment);
        viewHolder.shareButton = (ImageButton) row
                .findViewById(R.id.sharemain);
        viewHolder.helpButton = (ImageButton) row
                .findViewById(R.id.help);

        viewHolder.profilePic = (ImageView) row
                .findViewById(R.id.profile_image);
        viewHolder.likecount = (TextView) row
                .findViewById(R.id.likecountadapter);
        commentcount = (TextView) row
                .findViewById(R.id.commentcountadapter);
        viewHolder.tagImg = (ImageView) row
                .findViewById(R.id.tag_arrow);
        viewHolder.image1 = (ImageView) row
                .findViewById(R.id.image1);
        viewHolder.image2 = (ImageView) row
                .findViewById(R.id.image2);
        row.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) row.getTag();
    }
    mWishesData = wishListData.get(position);
    viewHolder.wishlikecount = mWishesData.getDeathWishLike();


    if ((position % 4) == 0) {
        viewHolder.lenear.setBackgroundColor(Color.rgb(255, 168, 0));
        viewHolder.back.setBackgroundResource(R.drawable.a2bg);
        viewHolder.tagImg.setBackgroundResource(R.drawable.a11);
        viewHolder.front.setBackgroundColor(Color.rgb(255, 168, 0));

    } else if ((position % 4) == 1) {
        viewHolder.lenear.setBackgroundColor(Color.rgb(253, 81, 43));
        viewHolder.back.setBackgroundResource(R.drawable.a3bg);
        viewHolder.tagImg.setBackgroundResource(R.drawable.a21);
        viewHolder.front.setBackgroundColor(Color.rgb(253, 81, 43));

        // viewHolder.Adlayout.invalidate();
        // viewHolder.Adlayout.setVisibility(View.GONE);
    } else if ((position % 4) == 2) {
        viewHolder.lenear.setBackgroundColor(Color.rgb(155, 89, 182));
        viewHolder.back.setBackgroundResource(R.drawable.a4bg);
        viewHolder.tagImg.setBackgroundResource(R.drawable.a31);
        viewHolder.front.setBackgroundColor(Color.rgb(155, 89, 182));

    } else if ((position % 4) == 3) {
        viewHolder.lenear.setBackgroundColor(Color.rgb(46, 204, 113));
        viewHolder.back.setBackgroundResource(R.drawable.a1bg);
        viewHolder.tagImg.setBackgroundResource(R.drawable.a41);
        viewHolder.front.setBackgroundColor(Color.rgb(46, 204, 113));
    }

查看持有人:

public static class ViewHolder {
    public int wishlikecount;
    public int wishcommentcount;
    LinearLayout back, lenear;
    RelativeLayout front;
    TextView deathWish;
    ImageButton likeButton, commentButton, shareButton, helpButton;
    TextView time, name, likecount;
    ImageView tagImg, image1, image2, profilePic;
}

问题是当视图被回收时,ListView 可以在您期望“左”布局时返回“右”布局。你应该覆盖getItemViewType() https://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int) and getViewTypeCount() https://developer.android.com/reference/android/widget/Adapter.html#getViewTypeCount()在您的适配器实现中;这些方法确保 ListView 在提供回收视图时为您提供适当的视图类型getView()

@Override
public int getViewTypeCount() {
    // return the total number of view types. this value should never change at runtime
    return 2;
}

@Override
public int getItemViewType(int position) {
    // return a value between 0 and (getViewTypeCount - 1)
    return position % 2;
}

@Override
public View getView(int position, View convertView, ViewGroup container) {
    int layoutResource; // determined by view type
    int viewType = getItemViewType(position);
    switch(viewType) {
    case 0:
        layoutResource = R.layout.list_item_even; break;
    case 1:
        layoutResource = R.layout.list_item_odd; break;
    }

    ViewHolder holder;
    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        convertView = inflater.inflate(layoutResource, container, false);
        holder = new ViewHolder();
        ...
        convertView.setTag(holder);
    }
    ...
    return convertView;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ViewHolder 在偶数和奇数位置上膨胀布局 的相关文章

随机推荐

  • iOS 应用程序可以在运行时读取自己的权利吗?

    iOS 应用程序可以在运行时发现 检查或以其他方式读取自己的权利吗 理想情况下 我可以将 entitlements 文件的全部 已处理 内容作为 plist 读取 仅获取应用程序标识符前缀将是可接受的次优方案 这里的目标包括 允许使用各种应
  • 创建没有 UI 的 iOS 操作扩展

    我正在尝试创建一个类似于 iOS 中可用的系统 复制 操作的操作扩展 我发现不同的答案说不可能有非全屏用户界面 但根据苹果官方文档 https developer apple com library archive documentatio
  • 树中始终向左|向右的下降路径的最大长度

    我正在准备技术面试 所以基本上从一开始就学习算法 我们得到了 BST 我需要找到其中 desc 路径的最大长度 该路径总是向左或向右 换句话说 示例树的下降路径是2 即15 10 6 5 2 15 10 6 14 我对算法问题非常陌生 解决
  • Predict() glmnet 函数中的错误:尚未实现的方法

    当我使用预测 glmnet 函数时 我收到代码下面提到的错误 mydata lt read csv data csv x lt mydata 1 4 y lt mydata 5 data lt cbind x y model lt mode
  • 如何在Flutter中创建45度的线性渐变?

    我无法理解如何以度数 LinearGradient 进行操作 我有以下代码 Container height 100 0 decoration BoxDecoration borderRadius BorderRadius circular
  • PowerShell 支持常量吗?

    我想在 PowerShell 中声明一些整数常量 有什么好的办法吗 Use Set Variable test Option Constant Value 100 or Set Variable test Option ReadOnly V
  • Visual Studio Code (vscode) - 按键时请求文本文档/文档链接失败错误

    您好 我正在我的 Mac 和 Windows 10 计算机上运行 VSCode 在最新的更新 回滚后 它们都开始显示相同的症状 每按几次按键 就会出现输出框 并在下拉列表中选择 HTML 语言服务器 显示的错误是 Error 13 47 0
  • scala 中 `=> String` 的类型是什么?

    在scala中 有一些按名称调用的参数 def hello who gt String println hello who 参数的类型是什么who 它将 scala REPL 上的函数显示为 hello who gt String Unit
  • 增加 R heatmap() 函数中的行高

    我有一个包含数百行和数十列的矩阵 希望绘制热图 如果我使用本机 R 函数 heatmap matrix sample 1 10000 nrow 1000 ncol 10 我得到一个行标题难以辨认的数字 我假设生成的图像符合当前绘图设备的规格
  • 获取手机方向,但将屏幕方向固定为纵向

    我想要获得手机方向 但将屏幕方向保持为纵向 因此 无论用户将手机转向横向还是纵向 视图都保持不变 但我可以知道它是转向横向还是纵向 将活动设置为android screenOrientation 肖像 将解决这两个问题 但我无法通过以下方式
  • PHP 检测无用文件或无用代码的工具

    我有一个非常大的 PHP 项目 我认为有很多无用的东西 您是否有一些技巧或工具来检测无用的代码部分或无用的文件 PHP 混乱检测器 PHPMD https phpmd org 可能的错误 次优代码 表达式过于复杂 未使用的参数 方法 属性
  • 无法从源代码构建 qtermwidget

    我正在尝试建立qterm小部件 https github com lxqt qtermwidget来自源头 但它给了我错误 我已经成功构建了lxqt 构建工具 https github com lxqt lxqt build tools 然
  • 如何编辑 Angular CLI 项目的 Service Worker 文件

    我已经添加了 angular pwa使用以下命令打包到我的 Angular CLI 项目ng add angular pwa project project name 所以它变成了一个渐进式 Web 应用程序 这增加了我认识的服务人员 我想
  • Nodejs:如何向浏览器发送可读流

    如果我查询框 REST API 并返回可读流 处理它的最佳方法是什么 怎么发送到浏览器呢 免责声明 我对流和缓冲区很陌生 所以其中一些代码非常理论化 你能在响应中传递readStream并让浏览器处理它吗 或者您是否必须将块流式传输到缓冲区
  • Delphi-如何获取目录中所有文件的列表

    我正在使用 delphi 当我执行 openpicturedialog 时 我想要一个目录中所有文件的列表 即 当执行打开对话框时并且 我从中选择一个文件 我想要 目录中所有文件的列表 所选文件的 您甚至可以建议我从中获取目录名称FileN
  • 当 net462 应用程序引用 netstandard1.5 库时,出现“无法加载文件或程序集”错误。但为什么?

    我试图找出在这个示例项目中我可能做错了什么 当我的net462应用参考文献anetstandard1 5图书馆 该应用程序依赖于 System Collections Immutable 1 3 0 根据 Nuget 的说法 它的目标是 N
  • C++ 释放结构体使用的所有内存

    快速提问 我已经用谷歌搜索并找到了一些答案 但我有点偏执 所以我想确定一下 考虑这种情况 struct CoordLocation float X float Y float Z int main CoordLocation coord n
  • 如何清除货物缓存?

    当我跑步时cargo build 各种库存储在文件夹中 usr local lib rustlib 清除这些库的正确方法是什么 我可以rm手动这些文件 但是这样做正确吗 我注意到 usr local lib rustlib manifest
  • 使用 AWS Kinesis Firehose 写入 S3 存储桶中的特定文件夹

    我希望能够根据数据内的内容将数据发送到 kinesis firehose 例如 如果我发送此 JSON 数据 name John id 345 我想根据 id 过滤数据并将其发送到我的 s3 存储桶的子文件夹 例如 S3 myS3Bucke
  • ViewHolder 在偶数和奇数位置上膨胀布局

    我需要两个布局来使用 ViewHolder 根据列表视图项的偶数和奇数位置进行膨胀 在偶数位置 我需要不同的布局 在奇数位置 另一个具有相同元素但布局不同的布局 然而 我实现了它 它给了我不同位置的随机布局 无论它们的位置如何 需要做什么来