RecyclerView项目

时间:2016-07-27 08:51:01

标签: android android-layout android-studio android-recyclerview android-nestedscrollview

1.i刚刚实施了一些Recyclerviews,他们的一些物品有额外的空白空间。一些滚动后,这个空间有时会消失。我已经尝试过各种你能思考的方式!我已经改变了包装内容的高度,但仍存在问题!

这是截图: Recyclerview empty space

这是项目xml:

<android.support.v7.widget.CardView
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="horizontal"
    android:adjustViewBounds="true"
    android:padding="8dp"

    android:layout_margin="6dp"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/banner_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/no_image_banner" />

        <TextView
            android:id="@+id/txtHey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:gravity="center"
            android:maxLength="20"
            android:text="sadas"
            android:textColor="#4d4a46"
            android:textSize="11dp"

            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtHi"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:gravity="center"
            android:text="sadas"
            android:textColor="#999999"
            android:textSize="9dp" />

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/txtPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="sadas"
                android:textColor="#888888"
                android:textSize="9dp" />

            <LinearLayout
                android:id="@+id/price_cancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:background="#888888"
                android:orientation="horizontal" />
        </FrameLayout>

        <TextView
            android:id="@+id/txtFinalPrice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="sadas"

            android:textColor="#3dc24d"
            android:textSize="9dp" />

        <TextView
            android:id="@+id/txtHello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="hey"
            android:textSize="9dp"
            android:visibility="gone" />
    </LinearLayout>
</android.support.v7.widget.CardView>

主页xml:

<android.support.design.widget.AppBarLayout
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <include
        layout="@layout/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/product_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        >

    </android.support.v7.widget.RecyclerView>

    <TextView
        android:id="@+id/request_results"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="21dp"
        android:textStyle="bold" />
</RelativeLayout>

  1. 第二个问题是我在嵌套滚动视图中使用了一些recyclerviews来防止滚动问题。一切都很好,但在较低版本的android像kitkat我有滚动问题(导致嵌套不能在sdk&lt; 21上工作)。我可以使用普通的scrollview,它在两个版本上运行良好,但普通的滚动视图不适用于协调器布局,因此appbar布局隐藏行为不会起作用!
  2. 你可以帮我解决这些问题吗?

    这是适配器类:

    private ArrayList<Struct> structs;
    OnItemListener onItemListener;
    private boolean isGrid;
    private int Tab;
    
    public Adapter_Recycler(ArrayList<Struct> structs, OnItemListener onItemListener, int Tab, boolean isGrid) {
        this.structs = structs;
        this.onItemListener = onItemListener;
        this.Tab = Tab;
        this.isGrid = isGrid;
    }
    
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;
    
    
        if (Tab == 2) {
            view = inflater.inflate(R.layout.item_product_color, parent,   false);
        }
        if (Tab == 1 && isGrid == false) {
            view = inflater.inflate(R.layout.item_product_list, parent, false);
        }
        if (Tab == 1 && isGrid) {
            view = inflater.inflate(R.layout.item_product_list, parent, false);
        }
    
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (Tab == 2) {
            holder.txtHey.setText(structs.get(position).hey);
            holder.txtPrice.setText(structs.get(position).hi);
            holder.txtHi.setVisibility(View.GONE);
            holder.PriceCancel.setVisibility(View.GONE);
            holder.txtFinalPrice.setVisibility(View.GONE);
            holder.txtHey.setTypeface(Activity_Main.SANS);
            holder.txtPrice.setTypeface(Activity_Main.SANS);
            Glide
                    .with(Activity_Main.CONTEXT)
                    .load(structs.get(position).image)
                    .placeholder(R.drawable.background_card)
                    .fitCenter()
                    .into(holder.banner_image);
        }
    
        if (Tab == 1) {
            holder.txtHey.setText(structs.get(position).hey);
            holder.txtHi.setText(structs.get(position).hi);
            holder.txtPrice.setText(structs.get(position).price);
            holder.txtFinalPrice.setText(structs.get(position).final_price);
            if (holder.txtFinalPrice.getText().toString() == "") {
                holder.PriceCancel.setVisibility(View.GONE);
            } else {
                holder.PriceCancel.setVisibility(View.VISIBLE);
            }
            holder.txtHey.setTypeface(Activity_Main.SANS);
            holder.txtHi.setTypeface(Activity_Main.SANS);
            holder.txtPrice.setTypeface(Activity_Main.SANS);
            holder.txtFinalPrice.setTypeface(Activity_Main.SANS);
            Glide
                    .with(Activity_Main.CONTEXT)
                    .load(structs.get(position).image)
                    .placeholder(R.drawable.background_card)
                    .fitCenter()
                    .into(holder.banner_image);
        }
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener != null) {
                    onItemListener.onItemSelect(position);
                }
            }
        });
    
        holder.txtHello.setText(structs.get(position).hello);
        holder.txtHello.setTypeface(Activity_Main.SANS);
    
    
    }
    
    
    @Override
    public int getItemCount() {
        return structs.size();
    }
    
    
    public static class ViewHolder extends RecyclerView.ViewHolder {
    
        CardView linearLayout;
        ImageView banner_image;
        TextView txtHey;
        TextView txtHi;
        TextView txtHello;
        TextView txtPrice;
        TextView txtFinalPrice;
        LinearLayout PriceCancel;
    
        public ViewHolder(View itemView) {
            super(itemView);
            linearLayout = (CardView) itemView.findViewById(R.id.btn);
            banner_image = (ImageView) itemView.findViewById(R.id.banner_image);
            txtHey = (TextView) itemView.findViewById(R.id.txtHey);
            txtHi = (TextView) itemView.findViewById(R.id.txtHi);
            txtHello = (TextView) itemView.findViewById(R.id.txtHello);
            txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
            txtFinalPrice = (TextView) itemView.findViewById(R.id.txtFinalPrice);
            PriceCancel = (LinearLayout) itemView.findViewById(R.id.price_cancel);
        }
    }
    

    }

    更新: 占位符纵横比与图像纵横比不得有太大差别。它们应该是一样的。

2 个答案:

答案 0 :(得分:0)

如果可能,那么提供适配器类,因为onCreateViewHolder与LayoutInflater可能存在一些问题。

答案 1 :(得分:0)

我终于弄明白了问题是什么! 额外的空间是因为 “ .placeholder(R.drawable.background_card) ”!

删除占位符后解决了

问题。

的问题是图像!不是占位符。