2016-04-30 96 views
1

我想用不同的項目高度實現RecyclerView。 Item包含NetworkImageView。我設置了NetworkImageView layout_height="wrap_content"和NetworkImageView的所有父級。但我面臨一個問題。當滾動和結束,然後滾動我得到RecyclerView奇怪的行爲。有時一些圖像不顯示。但我注意到,如果我將NetworkImageView layout_height設置爲某個特定的值,它工作正常。

這是list_row.xml不同高度的RecyclerView項目

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

回答

1

我使用另一個圖像加載器庫解決了這個問題。我用畢加索,一切正常。可能是Volley Image Loader包。從

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

*compile 'com.squareup.picasso:picasso:2.5.2'*

在適配器改變

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


首先添加到搖籃