在recyclerview中自动更改项目高度

时间:2019-01-21 07:55:08

标签: android android-recyclerview

我在垂直recyclerview中使用水平recyclerview。启动我的应用程序时,垂直的recyclerview的项目高度将根据具有最高高度的textview进行拉伸(如图所示)。当我滚动到最后一个项目时,此高度已更改。但是,当我滚动回到第一项时,此高度尚未重置。

如何根据水平回收站中textview内容的高度设置垂直recyclerview的高度。

下面是我的图片和代码

对不起,我英语水平不好。

VerticalRecyclerviewAdapter

@NonNull
@Override
public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_food_menu, null, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(lp);
    return new MenuViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MenuViewHolder menuViewHolder, int i) {

    Category category = menuArrayList.get(i).getCategory();
    ArrayList<MenuItem> menuItemArrayList = (ArrayList<MenuItem>) menuArrayList.get(i).getMenuItems();

    String headerMenu;

    //Set text by language
    if (LanguageUtils.getCurrentLanguage().getCode().equals(context.getString(R.string.language_japanese_code))) {
        headerMenu = category.getNameJp();
    } else {
        headerMenu = category.getNameEn();
    }

    menuViewHolder.txtHeaderMenu.setText(context.getResources().getString(R.string.character_title_category) +" "+ headerMenu);

    ItemRecyclerViewAdapter itemRecyclerViewAdapter = new ItemRecyclerViewAdapter(context, menuItemArrayList,this.mListener);

    menuViewHolder.recyclerListFood.setHasFixedSize(true);
    menuViewHolder.recyclerListFood.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    menuViewHolder.recyclerListFood.setAdapter(itemRecyclerViewAdapter);
}

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

public class MenuViewHolder extends RecyclerView.ViewHolder {

    protected TextView txtHeaderMenu;
    protected RecyclerView recyclerListFood;

    public MenuViewHolder(@NonNull View itemView) {
        super(itemView);

        this.txtHeaderMenu = itemView.findViewById(R.id.txtHeaderMenu);
        this.recyclerListFood = itemView.findViewById(R.id.recyclerListFood);
    }
}

Horizo​​ntalRecyclerviewAdapter

@NonNull
@Override
public FoodRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_food, null);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    //if you need three fix imageview in width
    return new ItemRecyclerViewAdapter.FoodRecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull FoodRecyclerViewHolder itemRecyclerViewHolder, int i) {
    MenuItem menuItem = this.menuItem.get(i);
    final Item item = menuItem.getItem();

    String name;
    String price = Utils.priceAndTaxToString(item.getPrice(), item.getTax());
    String tax = Utils.getTaxToString(item.getTax());
    //Set text with language
    if (LanguageUtils.getCurrentLanguage().getCode().equals(context.getString(R.string.language_japanese_code))) {
        name = item.getNameJp();
    } else {
        name = item.getNameEn();
    }



    itemRecyclerViewHolder.txtItemName.setText(name);
    itemRecyclerViewHolder.txtItemPrice.setText(Utils.increaseFontSizeForPath(price,tax,0.75f));

    String imgPath = item.getImg();
    try {
        RequestOptions options = new RequestOptions()
                .fitCenter()
                .placeholder(R.drawable.ic_no_image)
                .error(R.drawable.ic_no_image)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH);

        Glide.with(context)
                .load(FileUtil.getUrlImgFile(context, imgPath))
                .apply(options)
                .into(itemRecyclerViewHolder.imItem);
    } catch (Exception e) {
        itemRecyclerViewHolder.imItem.setImageResource(R.drawable.ic_no_image);
    }


    itemRecyclerViewHolder.onItemClickListener(new FoodRecyclerViewHolder.OnItemClickListener() {
        @Override
        public void onClick(View view, int position) {
            OrderDialog orderDialog = new OrderDialog(context, item, mListener);
            orderDialog.show();
        }
    });
}



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


public static class FoodRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    protected ImageView imItem;
    protected TextView txtItemName;
    protected TextView txtItemPrice;

    private OnItemClickListener onItemClickListener;

    public FoodRecyclerViewHolder(@NonNull View itemView) {
        super(itemView);

        this.txtItemName = itemView.findViewById(R.id.txtItemName);
        this.txtItemPrice = itemView.findViewById(R.id.txtItemPrice);
        this.imItem = itemView.findViewById(R.id.imItem);

        itemView.setOnClickListener(this);
    }

    public void onItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onClick(v, getAdapterPosition());
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }
}

Step1

Step2

Step3

1 个答案:

答案 0 :(得分:0)

根据文档:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setHasFixedSize(boolean)

  

void setHasFixedSize(布尔值hasFixedSize)。 RecyclerView可以执行   如果可以提前知道RecyclerView的   大小不受适配器内容的影响。 RecyclerView仍然可以   根据其他因素(例如其父级的大小)更改其大小,但   此大小计算不能取决于其子项的大小,或者   适配器的内容(适配器中的项目数除外)。   如果您对RecyclerView的使用属于此类别,请将其设置为   真正。它将允许RecyclerView避免使整个无效   适配器内容更改时的布局。

因此,您应使用RecyclerView.setHasFixedSize(false);。您的代码应为menuViewHolder.recyclerListFood.setHasFixedSize(false);