【问题标题】:RecyclerView strange behaviour inflating cellsRecyclerView 膨胀细胞的奇怪行为
【发布时间】:2015-11-13 12:02:11
【问题描述】:

我在两个应用程序中都有这个布局,一个在 RecyclerView 中,另一个在根活动布局中。

这里的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:orientation="vertical"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:background="#CFCFCF"
        android:minHeight="250dp">


        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_red_dark"
                >


                <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/holo_blue_bright"
                        android:layout_above="@+id/commerceTextView"
                        >

                        <ImageView
                                android:src="@drawable/imagen"
                                android:id="@+id/commerceImageView"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center"/>
                </FrameLayout>

                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/commerceTextView"
                        android:gravity="center"
                        android:textStyle="bold"
                        android:textSize="24sp"
                        android:textColor="#F1F1F1"
                        android:background="@color/colorPrimary"
                        android:layout_alignParentBottom="true"
                        android:paddingTop="10dp"
                        android:text="Best food ever"
                        android:paddingBottom="10dp"/>

        </RelativeLayout>

</FrameLayout>

这里是适配器

public class CommercesAdapter extends RecyclerView.Adapter<CommercesAdapter.CommercesViewHolder> {

    private final Context context;
    private final ImageLoader loader;
    private List<CommerceEntity> commercesList;

    @Inject
    public CommercesAdapter(Context context, ImageLoader loader) {
        this.context = context;
        this.loader = loader;
    }

    public void setData(List<CommerceEntity> commercesList) {
        this.commercesList = commercesList;
    }

    @Override
    public CommercesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                                  .inflate(R.layout.list_comerces_item, parent, false);
        return new CommercesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CommercesViewHolder holder, int position) {
        CommerceEntity commerce = commercesList.get(position);
        String imageUri = commerce.getImageUri();
        String name = commerce.getName();

       // holder.commerceTypeName.setText(name);
        //loader.bind(holder.commerceImage, imageUri);

    }

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

    public static class CommercesViewHolder extends RecyclerView.ViewHolder {
        public ImageView commerceImage;
        public TextView commerceTypeName;
        public CommercesViewHolder(View itemView) {
            super(itemView);
            commerceImage = (ImageView) itemView.findViewById(R.id.commerceImageView);
            commerceTypeName = (TextView) itemView.findViewById(R.id.commerceTextView);
        }
    }

这里是 RecyclerView

在根活动布局中

有人知道为什么会这样吗?如果我将 android:centerInParent"true" 添加到嵌套的 FrameLayout 图像会出现,但我不明白为什么。

【问题讨论】:

  • 请发布您的适配器代码。
  • @Nanoc 完成。但是你可以看到适配器什么都不做

标签: android android-layout


【解决方案1】:

在您的适配器中,取消注释您已注释掉的两行;

` // holder.commerceTypeName.setText(name);

    //loader.bind(holder.commerceImage, imageUri);

`

【讨论】:

  • 它是一样的,我评论它只是为了表明图像是从资源加载的并且总是相同的。
猜你喜欢
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多