Elaman Aitymbet
Elaman Aitymbet

Reputation: 515

RecyclerView item with different height

I want to implement RecyclerView with different item height. Item contains NetworkImageView. I set for NetworkImageView layout_height="wrap_content" and for all parent of NetworkImageView. But I am faced with an issue. When scrolling and reaching the end, and then scrolling up I get strange behavior of RecyclerView. And sometimes some images do not show. But I noticed if I set NetworkImageView layout_height to some specific value it's working fine.

This is list_row.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"

    >
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        android:background="#FFFFFF"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="7dp"
        android:layout_marginTop="7dp"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="1dp"
        card_view:cardBackgroundColor="#FFFFFF"

        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="13dp"
                    android:textColor="#000000"
                    android:text="Получайте бонусы от нашей компании"
                    android:textStyle="bold"/>

                <com.android.volley.toolbox.NetworkImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/image_net"
                    android:src="@drawable/wait"
                    android:layout_marginLeft="13dp"
                    android:layout_marginRight="13dp"
                    android:adjustViewBounds="true"
                    />
                <TextView
                    android:id="@+id/details"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="13dp"
                    android:textColor="@android:color/holo_red_light"
                    android:text="@string/tv_under"/>

        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>



RecyclerView.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.A_Fragment"
    android:background="#E8E8E8">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"
        />

</RelativeLayout>



RecyclerViewAdapter

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.PrudactViewHolder> {

List<HashMap<String,String>> newsList;
public Context mContext;
public static ItemAdd add;
public static FragmentListItemClick fragmentListItemClick;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public NewsAdapter(List<HashMap<String,String>> news, Context context){
    this.newsList = news;
    this.mContext = context;

}

public NewsAdapter(){

}

@Override
public PrudactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View  v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_news, parent, false);
    PrudactViewHolder pvh = new PrudactViewHolder(v,mContext);
    return pvh;
}


@Override
public void onBindViewHolder( PrudactViewHolder prudactViewHolder, int i) {

    /*NewsModel newsModel = newsList.get(i);
    prudactViewHolder.title.setText(String.valueOf(newsModel.getTitle()));
    prudactViewHolder.description.setText(String.valueOf(newsModel.getDescription()));
    prudactViewHolder.thumbNail.setDefaultImageResId(R.drawable.wait);
   prudactViewHolder.thumbNail.setImageUrl(newsModel.getId_image(), imageLoader);*/

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    HashMap<String,String> news = newsList.get(i);
    prudactViewHolder.title.setText(news.get(A_Fragment.TITLE));
    //prudactViewHolder.description.setText(news.get(A_Fragment.DESCRIPTION));
    if (news.get(A_Fragment.IMG_URL)!=null)
    prudactViewHolder.thumbNail.setImageUrl(news.get(A_Fragment.IMG_URL), imageLoader);
    else prudactViewHolder.thumbNail.setImageUrl(news.get(A_Fragment.IMG_URL), imageLoader);

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}


@Override
public int getItemCount() {

    return newsList.size();
}

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


    TextView title,description;
    NetworkImageView thumbNail;

    public PrudactViewHolder(View itemView,Context context) {
        super(itemView);

        title = (TextView)itemView.findViewById(R.id.title);
        description = (TextView)itemView.findViewById(R.id.details);
        thumbNail = (NetworkImageView) itemView
                .findViewById(R.id.image_net);

        description.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        A_Category_B a_category_b = new A_Category_B();
        fragmentListItemClick.itemClicked(a_category_b);

    }
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}


public void setfragmentclick(FragmentListItemClick fr){
    fragmentListItemClick = fr;
}
}

And Place where I use it

 public static final String TITLE = "title";
    public static final String DESCRIPTION = "des";
    public static final String IMG_URL = "img_url";

    NewsAdapter adapter;
    RecyclerView rv;

    ArrayList<HashMap<String,String>> data =  new ArrayList<HashMap<String, String>>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_a, container, false);
        rv = (RecyclerView) view.findViewById(R.id.rv);

        setHasOptionsMenu(true);
        data.clear();
        HashMap<String, String> hm1 = new HashMap<String, String>();

        // some code
       data.add(h1);
       data.add(h2);
       data.add(h2);


        adapter = new NewsAdapter(data,getContext());
        LinearLayoutManager llm = new LinearLayoutManager(rv.getContext());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        return view;
    }
}

Upvotes: 1

Views: 2893

Answers (1)

Elaman Aitymbet
Elaman Aitymbet

Reputation: 515

I solved this issue by using another Image loader library. I used Picasso and all worked fine. May be it was Volley Image Loader bag.

First add to gradle: *compile 'com.squareup.picasso:picasso:2.5.2'*

In adapter change from

prudactViewHolder.thumbNail.setImageUrl(news.get(A_Fragment.IMG_URL), imageLoader);

to

Uri uri = Uri.parse(news.get(A_Fragment.IMG_URL));
Picasso.with(mContext).load(uri).into(prudactViewHolder.thumbNail);

Upvotes: 1

Related Questions