Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

horizontal recyclerView with wrap_content in vertical recyclerView

In my app I have a situation in which I have to create horizontal RecyclerView in vertical RecyclerView. Their are some rows which will show casual details and in some it will show a horizontal RecyclerView.

I have set the height of horizontal RecyclerView to wrap_content which is not visible. But when I have added hardcoded height to it, then RecyclerView is visible. I have searched a lot regarding this problem but I didn't got any working or convincing solution.

Here is my adapter class

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);
        }
    }
}

}

Please help me to get this problem solved.

like image 614
Kislay Kumar Avatar asked Nov 10 '22 04:11

Kislay Kumar


1 Answers

A simpler solution can be to keep the height hardcoded (as it is a horizontal view, height can stay fixed) and then hide the horizontal recycler view if the input is empty.

if(list.size() == 0){
   horizontalRecyclerView.setVisibility(View.GONE);
}
like image 77
Harsh Nigam Avatar answered Nov 14 '22 23:11

Harsh Nigam