0

1.i just implemented some recyclerviews, some of their items have extra empty space. this space sometimes disappears after some scrolling. i have tried every way you could think! i have changed heights to wrap content but still issue exists!

here is the screenshot: Recyclerview empty space

here is item 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>

main page 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. second question is i used some recyclerviews inside a nested scroll view to prevent scrolling issues. everything is good but in lower versions of android like kitkat i have problem with scrolling(cause nested wont work on sdk<21 ) . i could use a normal scrollview that works well on both versions but normal scroll view doesnt work with coordinator layout and so appbar layout hiding behaviour wont work!

can you help me with these problems please?

here is the adapter class:

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

}

UPDATE: the place holder aspect ratio must not be much different from the images aspect ratio. they should be the same.

Mohamad Rostami
  • 365
  • 2
  • 17
  • I see you've added xml for the recycler item, please add for where `RecyclerView` is added and for the adapter. Also show how do you initialize `RecyclerView` i.e. where its `LayoutManager`, `Adapter` etc is set. And Try to state the issue you are facing and the preferable outcome separately. – Abbas Jul 27 '16 at 10:53
  • fyi: these attributes does not apply for cardview `android:orientation="horizontal" android:adjustViewBounds="true" android:padding="8dp" android:background="#ffffff" ` – Kosh Jul 27 '16 at 11:27
  • ok, but thats not the problem! i just changed cardview to linear layout and viceversa, and these extra attributes are there because of that! @k0sh – Mohamad Rostami Jul 27 '16 at 11:49
  • Your issue is because of padding and margin – Piyush Aug 04 '16 at 07:56
  • @PiyushGupta are you sure? what exactly should i do? – Mohamad Rostami Aug 04 '16 at 08:44

2 Answers2

0

If possible then provide adapter class because may be some issue with onCreateViewHolder with LayoutInflater.

ViramP
  • 1,381
  • 9
  • 10
0

I finally figured out what the problem was! that extra space was because of
" .placeholder(R.drawable.background_card) " !

problem solved after removing the placeholder.

ofcourse issue is with the image! not placeholder.

Mohamad Rostami
  • 365
  • 2
  • 17