从drawable一张一张地在cardview中设置图像
Set image in cardview one by one from drawable
我正在尝试使用 recyclerview 将可绘制的图像和文本逐一添加到 cardview 中。假设我的可绘制对象中有 10 张图像,我只想使用其中的 5 张。那么该怎么做呢?
这是我的卡片视图:
<?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 步。 创建一个方法,其中 return arraylist of imageModel in your fragment/activity from where you set the adapter
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 步。 在片段中设置适配器/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();
}
此代码将告诉您如何做 it.Fill 免费修改此答案。
我正在尝试使用 recyclerview 将可绘制的图像和文本逐一添加到 cardview 中。假设我的可绘制对象中有 10 张图像,我只想使用其中的 5 张。那么该怎么做呢? 这是我的卡片视图:
<?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 步。 创建一个方法,其中 return arraylist of imageModel in your fragment/activity from where you set the adapter
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 步。 在片段中设置适配器/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();
}
此代码将告诉您如何做 it.Fill 免费修改此答案。