gpt4 book ai didi

android - 具有不同高度的 RecyclerView 项目

转载 作者:行者123 更新时间:2023-11-30 01:21:57 25 4
gpt4 key购买 nike

我想实现具有不同项目高度的 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>



回收 View 适配器

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

最佳答案

我通过使用另一个图像加载器库解决了这个问题。我用过 Picasso,一切正常。可能是 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);

关于android - 具有不同高度的 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36956045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com