草庐IT

android - 垂直 recyclerView 中带有 wrap_content 的水平 recyclerView

coder 2023-12-15 原文

在我的应用程序中,我必须在垂直 RecyclerView 中创建水平 RecyclerView。有些行会显示随意的细节,有些行会显示水平的 RecyclerView。

我已将水平 RecyclerView 的高度设置为不可见的 wrap_content。但是当我向它添加硬编码高度时,RecyclerView 就可见了。我已经就这个问题进行了很多搜索,但没有找到任何有效或令人信服的解决方案。

这是我的适配器类

public class HomeRVAdapter extends RecyclerView.Adapter<HomeRVAdapter.HomeViewHolder> {
private ArrayList<LinkedHashMap<String, Object>> data;
private Context ctx;
private int selectedIndex;

public HomeRVAdapter(Context ctx, ArrayList<LinkedHashMap<String, Object>> val) {
    data = val;
    this.ctx = ctx;
    selectedIndex = -1;
}

@Override
public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();

    View view = inflater.inflate(R.layout.custom_home_recycler, null);

    return new HomeViewHolder(view);
}

@Override
public int getItemCount() {
    return data.size();
}

@Override
public void onBindViewHolder(final HomeViewHolder holder, final int position) {
    if (getItemCount() == position+1) {
        holder.categoryLL.setVisibility(View.GONE);
        holder.productLL.setVisibility(View.VISIBLE);

        LinearLayoutManager manager = new LinearLayoutManager(ctx);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);

        ArrayList<SubCategoryDetails> list = new ArrayList<>();
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());
        list.add(new SubCategoryDetails());

        holder.subCatRV.setAdapter(new PromoProductAdapter(list, holder.subCatRV));
        holder.subCatRV.setLayoutManager(manager);

    } else {
        holder.categoryLL.setVisibility(View.VISIBLE);
        holder.productLL.setVisibility(View.GONE);

        if (selectedIndex == position) {
            holder.subCatGrid.setVisibility(View.VISIBLE);
            showGrid(holder);
        } else {
            holder.subCatGrid.setVisibility(View.GONE);
        }

        holder.catTR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedIndex == position) {
                    holder.subCatGrid.setVisibility(View.GONE);
                    selectedIndex = -1;
                } else {
                    selectedIndex = position;
                    showGrid(holder);
                }
            }
        });
    }
}

private void showGrid(HomeViewHolder holder) {
    ArrayList<SubCategoryDetails> list = new ArrayList<>();
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());
    list.add(new SubCategoryDetails());

    SubCategoryGridAdapter adapter = new SubCategoryGridAdapter(list);

    holder.subCatGrid.setAdapter(adapter);
    holder.subCatGrid.setVisibility(View.VISIBLE);
}

class HomeViewHolder extends RecyclerView.ViewHolder {
    RecyclerView subCatRV;
    TextView catTitleTV, productNameTV;
    ImageView productIV;
    NonScrollableGridView subCatGrid;
    LinearLayout categoryLL, productLL;
    TableRow catTR;

    public HomeViewHolder(View view) {
        super(view);

        subCatRV = (RecyclerView) view.findViewById(R.id.subCatRV);
        productNameTV = (TextView) view.findViewById(R.id.productNameTV);

        catTitleTV = (TextView) view.findViewById(R.id.catTitleTV);
        productIV = (ImageView) view.findViewById(R.id.productIV);
        subCatGrid = (NonScrollableGridView) view.findViewById(R.id.subCatGrid);
        categoryLL = (LinearLayout) view.findViewById(R.id.categoryLL);
        productLL = (LinearLayout) view.findViewById(R.id.productLL);
        catTR = (TableRow) view.findViewById(R.id.catTR);
    }
}

class SubCategoryGridAdapter extends BaseAdapter {
    ArrayList<SubCategoryDetails> subCatData;

    public SubCategoryGridAdapter(ArrayList<SubCategoryDetails> subCatData){
        this.subCatData = subCatData;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) ctx).getLayoutInflater().inflate(R.layout.custom_sub_cat_grid, null, true);
        }

        return convertView;
    }

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

class PromoProductAdapter extends RecyclerView.Adapter<PromoProductAdapter.PromoHolder> {
    ArrayList<SubCategoryDetails> data;
    RecyclerView horizontalRV;

    public PromoProductAdapter(ArrayList<SubCategoryDetails> data, RecyclerView horizontalRV){
        this.data = data;
        this.horizontalRV = horizontalRV;
    }

    @Override
    public PromoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();

        View view = inflater.inflate(R.layout.custom_promoted_product, null);

        if (horizontalRV != null) {
            int height = view.getMeasuredHeight();
            horizontalRV.getLayoutParams().height = height;
        }

        return new PromoHolder(view);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

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

    }

    class PromoHolder extends RecyclerView.ViewHolder {
        public PromoHolder(View view) {
            super(view);
        }
    }
}

请帮我解决这个问题。

最佳答案

一个更简单的解决方案可以是保持高度硬编码(因为它是水平 View ,高度可以保持固定)然后如果输入为空则隐藏水平回收器 View 。

if(list.size() == 0){
   horizontalRecyclerView.setVisibility(View.GONE);
}

关于android - 垂直 recyclerView 中带有 wrap_content 的水平 recyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689669/

有关android - 垂直 recyclerView 中带有 wrap_content 的水平 recyclerView的更多相关文章

  1. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby - 在 Ruby 中,垂直线是什么? - 2

    1.upto(9){|x|printx}为什么这行不通?{printx|x}}y呢? 最佳答案 它用于传递给您的block的参数。即在您的示例中,upto将使用1到9中的每个数字调用您的block,当前值可作为x获得。block参数可以有任何名称,就像方法参数一样。例如1.upto(9){|num|putsnum是有效的。就像一个方法的参数一样,一个block也可以有多个参数。例如hash.each_pair{|key,value|puts"#{key}is#{value}"} 关于ru

  4. ruby-on-rails - rails 不需要 Content-Type "application/json" - 2

    我在rails中有一个API端点,默认情况下,如果您没有设置任何Content-Typeheader,则会处理中的参数application/x-www-form-urlencoded有没有办法在不指定header中的内容类型的情况下处理来自POST请求的rails中的json字符串? 最佳答案 在您的routes.rb文件中,您可以将POST路由放置在命名空间中,并像这样定义预期的格式:namespace:api,defaults:{format::json}dopost'example'=>'controller#action'

  5. ruby-on-rails - Ruby on Rails 中的水平数据库扩展 - 2

    我有一个RubyonRails应用程序和一个具有以下结构的PostgreSQL数据库:classA只有几个A,而且增长缓慢(比如一个月5个)。每个A有数千个B,每个B有数万个C(因此每个A有数百万个C)。A是独立的,并且永远不会同时需要来自不同A的B和C(即在同一查询中)。我的问题是现在我只有几个A,ActiveRecord查询需要很长时间。当C的表有数千万行时,查询将永远无法进行。我正在考虑水平扩展数据库(即A的一张表,B的一张表和每个A的一张C的表)。但我不知道该怎么做。我猜这是一种分片,但我无法弄清楚如何动态创建数据库表并使用ActiveRecord访问数据(如果该表取决于我正在

  6. ruby - ruby 中带有多行字符串的空格 - 2

    我有一个多行字符串的空白问题。我在生成一些SQL的代码中有类似的内容。defgenerate_sql但是我的SQL缩进全乱了,这是我不想要的。"UPDATEpage\nSETview_count=10;\n"我能做到defgenerate_sql输出的正是我想要的"UPDATEpage\nSETview_count=10;\n"但是我的代码缩进全乱了,我真的不想要这样。关于如何最好地实现我所追求的目标有什么建议吗? 最佳答案 Ruby2.3.0使用squigglyheredoc很好地解决了这个问题.请注意示例之间波浪号/连字符的区别

  7. ruby-on-rails - Rails 中带后备功能的动态命名空间 Controller - 2

    我对新的Rails应用程序有一个有点奇怪的要求。我需要构建一个应用程序,其中所有路由都在多个命名空间中定义(让我解释一下)。我想要一个应用程序,其中学校科目(数学、英语等)是namespace:%w[mathenglish].eachdo|subject|namespacesubject.to_symdoresources:studentsendend这很棒而且有效,但它需要我为每个主题创建一个命名空间StudentsController,这意味着如果我添加一个新主题,那么我需要创建一个新Controller。我想创建一个Base::StudentsController,如果Math:

  8. ruby - 如何在 ruby​​ 中设置 header ['content-type' ] ='application/json' - 2

    require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby​​中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade

  9. ruby-on-rails - Ruby on Rails 平均水平? - 2

    有没有一种简单的方法可以获取集合中某个属性的平均值?例如,每个用户都有一个分数。给定一组用户(@users),您如何获得该组的平均分?有没有类似@users.average(:score)的东西?我想我在数据库字段中遇到过类似的东西,但我需要它来处理集合...... 最佳答案 对于你的问题,实际上可以这样做:@users.collect(&:score).sum.to_f/@users.lengthif@users.length>0早些时候我认为,@users.collect(&:score).average会起作用。对于数据库字段

  10. ruby-on-rails - 如何在 RoR 中使用 content_tag 嵌入标签? - 2

    我有这个可以为我生成一个超链接:我希望它显示在td标签中,所以我想使用这个content_tag来帮助我:"example")%>我想要我的td中的超链接,所以我有这样的东西:,:class=>"example")%>但是我收到语法错误,我该怎么办? 最佳答案 内联:'example')%>或block形式:'example')do%> 关于ruby-on-rails-如何在RoR中使用content_tag嵌入标签?,我们在StackOverflow上找到一个类似的问题:

随机推荐