我有一个RecyclerView
,其CardView
作为列表项。 CardView
只有2个TextView
和一个ImageView
。
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="@+id/dcCardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/detail_coast_cardview_bg">
<ImageView
android:id="@+id/dcImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:src="@drawable/radiation"
android:layout_gravity="center_vertical" />
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/dcName"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="212"
android:textSize="18dp" />
<TextView
android:textColor="#fff"
android:id="@+id/dcValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="renu asdasasd"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
其中的类是这个
public class DetailedCoastRecyclerView {
//region fields
private String name;
private String value;
private int imageName;
//end region fields
//constructor
public DetailedCoastRecyclerView() {}
public DetailedCoastRecyclerView(String name, String value, int image) {
this.name = name;
this.value = value;
this.imageName = image;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public int getImageName() {
return imageName;
}
}
我的CoastDetailsActivity2
public class CoastDetailsActivity2 extends AppCompatActivity {
private Context context;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private int coastId;
private String jsonCoastDetailsString = "";
private RecyclerView recyclerView;
private DetailedCoastAdapter detailedCoastAdapter;
private ImageView btnBlueFlag;
private ImageView btnHandycapFriendly;
private ImageView btnFavorite;
private ImageView btnWaze;
private LinearLayout mainLayout;
private Beach mBeach;
private TextView tvBeachName;
private DetailedCoastWithForcast detailedCoastWithForcast;
private DetailedBeachMetadata detailedBeachMetadata;
private DetailedBeachHourlyForecast dbhfToday;
private List<DetailedCoastRecyclerView> detailedCoastRecyclerViewList = new ArrayList<>();
private Map<String, String> windBearing = new HashMap<>();
private Map<String, String> jellyType = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coast_details2);
setPointer();
}
private void setPointer() {
this.context = this;
mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
sharedPreferences = context.getSharedPreferences(sharedPreferencesName, MODE_PRIVATE);
editor = sharedPreferences.edit();
tvBeachName = (TextView) findViewById(R.id.tvBeachName);
//start the forecast activity
tvBeachName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ForecastActivity.class);
intent.putExtra("coastId", coastId);
startActivity(intent);
}
});
AndroidNetworking.initialize(context);
//get the coastId
Bundle extras = getIntent().getExtras();
if (extras == null) {
//TODO add snackbar for error
} else {
coastId = extras.getInt("coastId");
}
getBeachDetails();
recyclerView = (RecyclerView) findViewById(R.id.dcRecyclerView);
detailedCoastAdapter = new DetailedCoastAdapter(detailedCoastRecyclerViewList, context);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(detailedCoastAdapter);
}
}
和适配器
public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.myViewHolder> {
Context context;
private List<DetailedCoastRecyclerView> dbhf0;
public ForecastAdapter(Context context, List<DetailedCoastRecyclerView> dbhf0) {
this.context = context;
this.dbhf0 = dbhf0;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.detailed_coast_card, parent, false);
return new myViewHolder(view);
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
DetailedCoastRecyclerView dcrv = dbhf0.get(position);
holder.tvName.setText(dcrv.getName());
holder.tvValue.setText(dcrv.getValue());
//holder.ivImage.setImageResource(dcrv.getImageName());
Glide.with(context).load(dcrv.getImageName()).into(holder.ivImage);
}
@Override
public int getItemCount() {
return dbhf0.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
ImageView ivImage;
TextView tvName;
TextView tvValue;
public myViewHolder(View itemView) {
super(itemView);
ivImage = (ImageView) itemView.findViewById(R.id.dcImage);
tvName = (TextView) itemView.findViewById(R.id.dcName);
tvValue = (TextView) itemView.findViewById(R.id.dcValue);
}
}
}
当我运行它时,getBeachDetails()
方法从服务器获取所有信息,将其添加到ArrayList
并在适配器上调用notifyDataSetChanged()
。
我知道它有效,因为我看到每张卡的名称和价值,但不会显示本地存储的图像。
我在ViewPager
内使用相同的适配器,包含3个片段,一切正常。
有什么想法吗?
答案 0 :(得分:1)
答案 1 :(得分:0)
我猜你的图像分辨率太高,以至于android缓冲区无法处理并在渲染图像时抛出异常,因此我知道在ImageView
上加载图片时必须降低图像质量。
在Picasso中,这是通过调用resize(width,height)
方法完成的。
Picasso.with(context)
.load(res)
.resize(width,height)
.placeholder(placeHolder)
.into(view);
我想这与Glide的方式相同。