gpt4 book ai didi

android - 从drawable一张一张地在cardview中设置图像

转载 作者:行者123 更新时间:2023-11-29 17:15:36 24 4
gpt4 key购买 nike

我正在尝试使用 recyclerview 从 drawable 和文本中一个一个地添加图像到 cardview 中。假设我的可绘制对象中有 10 张图像,我只想使用其中的 5 张。那么该怎么做呢?这是我的卡片 View :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/placeCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="@dimen/card_corner_radius">

<ImageView
android:id="@+id/placeImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />

<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="@+id/mainHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />

<LinearLayout
android:id="@+id/placeNameHolder"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"
android:orientation="horizontal">

<TextView
android:id="@+id/placeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />

</LinearLayout>

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

这是我的 recyclerview 适配器:

public class SubPlaceAdapter extends RecyclerView.Adapter<SubPlaceRecyclerViewHolder>{

String ImageUri;

@Override
public SubPlaceRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_places, null);

SubPlaceRecyclerViewHolder viewHolder = new SubPlaceRecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(SubPlaceRecyclerViewHolder holder, int position) {

// holder.subPlaceImage
}

@Override
public int getItemCount() {
return 0;
}

我的观点持有人:

public class SubPlaceRecyclerViewHolder extends RecyclerView.ViewHolder {

protected ImageView subPlaceImage;
protected TextView subPlaceTitle;

public SubPlaceRecyclerViewHolder(View view) {
super(view);
this.subPlaceImage = (ImageView) view.findViewById(R.id.placeImage);
this.subPlaceTitle = (TextView) view.findViewById(R.id.placeName);
}

}感谢您的帮助:)

最佳答案

第 1 步。首先创建一个模型

public class ImageModel {
int imageId;
String aboutText;


public int getImageId() {
return imageId;
}

public void setImageId(int imageId) {
this.imageId = imageId;
}

public String getAboutText() {
return aboutText;
}

public void setAboutText(String aboutText) {
this.aboutText = aboutText;
}
}

第 2 步。创建一个方法,从您设置适配器的位置返回 fragment/Activity 中 imageModel 的数组列表

private ArrayList<ImageModel> setImageData(){
ArrayList<ImageModel> projectList=new ArrayList<>();

ImageModel imageModel=new ImageModel();
imageModel.setImageId(R.mipmap.ic_star_fill);
imageModel.setAboutText("RandomText");
projectList.add(imageModel);

ImageModel imageModel1=new ProjectModel();
projectModel.setImageId(R.mipmap.ic_star_fill);
projectModel.setAboutText("RandomText");
projectList.add(projectModel);
return projectList;
}

第 3 步。在您的适配器中创建以 ArrayList 作为参数的构造函数

public SubPlaceAdapter(ArrayList<ImageModel> mImageList) {
this.mActivity = mActivity;
this.mImageList = mImageList);
}

第 4 步。在 fragment/Activity 中设置适配器。

mSubPlaceAdapter = new SubPlaceAdapter(setImageData());

第 5 步。将您的项目放在 BindView Holder 上

@Override 
public void onBindViewHolder(SubPlaceRecyclerViewHolder holder, int position) {
ImageModel imageModel= mImageList.get(position)
holder.subPlaceImage.setImageResource(imageModel.getImageId());
}

@Override
public int getItemCount() {
return mImageList.size();
}

这段代码会给你如何去做的想法。可以自由修改这个答案。

关于android - 从drawable一张一张地在cardview中设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39211333/

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