我想用不同的项目高度实现RecyclerView。项目包含NetworkImageView。我为NetworkImageView layout_height="wrap_content"
和NetworkImageView的所有父级设置。但是我面临一个问题。当滚动并到达末尾,然后向上滚动时,我得到RecyclerView的奇怪行为。有时某些图像无法显示。但是我注意到,如果将NetworkImageView layout_height
设置为某个特定值,则可以正常工作。这是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;
}
}
放在我用的地方
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;
}
}
最佳答案
我通过使用另一个图像加载器库解决了此问题。我使用了毕加索,一切都很好。可能是Volley Image Loader手提袋。首先添加到gradle中:*compile 'com.squareup.picasso:picasso:2.5.2'*
在适配器中从
prudactViewHolder.thumbNail.setImageUrl(news.get(A_Fragment.IMG_URL), imageLoader);
至
Uri uri = Uri.parse(news.get(A_Fragment.IMG_URL));
Picasso.with(mContext).load(uri).into(prudactViewHolder.thumbNail);