gpt4 book ai didi

java - 如何根据android中的图像选择设置图像标题

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:09 24 4
gpt4 key购买 nike

我正在开发一个Android应用程序,我试图在其中实现图像库的一项功能。现在,最初,我想显示一些图像 url 及其标题名称。我正在使用 hashmap 将标题与其图像进行映射。我的模型类具有两个 String 字段 url 和 title。但问题是我在开发领域非常陌生,现在知道如何根据图像点击显示标题。这是我的模型类

public class ImageModel implements Parcelable {

String name, url;

public ImageModel() {

}

protected ImageModel(Parcel in) {
name = in.readString();
url = in.readString();
}

public static final Creator<ImageModel> CREATOR = new Creator<ImageModel>() {
@Override
public ImageModel createFromParcel(Parcel in) {
return new ImageModel(in);
}

@Override
public ImageModel[] newArray(int size) {
return new ImageModel[size];
}
};

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(url);
}
}

现在是主要 Activity ,我创建一个带有图像和标题的 HashMap ,以将它们显示到 View 中。

public class MainActivity extends AppCompatActivity {

GalleryAdapter mAdapter;
RecyclerView mRecyclerView;

ArrayList<ImageModel> data = new ArrayList<>();

ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
HashMap<String, String> h1 = new HashMap<>();

String[] imgUrls = {"https://images.unsplash.com/photo-1444090542259-0af8fa96557e?q=80&fm=jpg&w=1080&fit=max&s=4b703b77b42e067f949d14581f35019b",
"https://images.unsplash.com/photo-1439546743462-802cabef8e97?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1441155472722-d17942a2b76a?q=80&fm=jpg&w=1080&fit=max&s=80cb5dbcf01265bb81c5e8380e4f5cc1"};
String[] imgNames = {"name1","name2","name3"};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


for (int i = 0; i < 3; i++) { //change size according to your size.

ImageModel imageModel = new ImageModel();
imageModel.setName(imgNames[i]);
imageModel.setUrl(imgUrls[i]);
data.add(imageModel);

}

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mRecyclerView = (RecyclerView) findViewById(R.id.list);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
mRecyclerView.setHasFixedSize(true);


mAdapter = new GalleryAdapter(MainActivity.this, data);
mRecyclerView.setAdapter(mAdapter);

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this,
new RecyclerItemClickListener.OnItemClickListener() {

@Override
public void onItemClick(View view, int position) {

Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putParcelableArrayListExtra("data", data);
intent.putExtra("pos", position);
startActivity(intent);

}
}));

}

}

GalleryAdapterCode

public class GalleryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

Context context;
List<ImageModel> data = new ArrayList<>();

public GalleryAdapter(Context context, List<ImageModel> data) {
this.context = context;
this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
View v;
v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item, parent, false);
viewHolder = new MyItemHolder(v);

return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

Glide.with(context).load(data.get(position).getUrl())
.thumbnail(0.5f)
.override(200,200)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(((MyItemHolder) holder).mImg);
((MyItemHolder) holder).mTextView.setText(data.get(position).getName());

}

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

public static class MyItemHolder extends RecyclerView.ViewHolder {
ImageView mImg;
TextView mTextView;


public MyItemHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textView);
mImg = (ImageView) itemView.findViewById(R.id.item_img);
}

}

}

我的项目列表行的 XML 类是

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<ImageView
android:id="@+id/item_img"
android:layout_width="match_parent"
android:layout_height="188dp"
android:adjustViewBounds="true"
android:background="@color/colorAccent"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/placeholder" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7000"
android:orientation="vertical"
android:padding="10dp"
android:layout_alignBottom="@+id/item_img"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/textView"
android:text="Headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="15dp"
android:textStyle="normal|bold"
/>
</LinearLayout>
</RelativeLayout>

最佳答案

创建两个 String 数组,其中一个包含 MainActivity 中的图像 url 和其他名称。

String[] imgUrls = {"url1","url2","url3"};  
String[] imgNames = {"name1","name2","name3"};

迭代这些数组并创建模型类的列表。

ArrayList<ImageModel> data = new ArrayList<ImageModel>();
for (int i = 0; i < 3; i++) { //change size according to your size.

ImageModel imageModel = new ImageModel();
imageModel.setName(imgNames[i]);
imageModel.setUrl(imgUrls[i]);
data.add(imageModel);

}
mAdapter = new GalleryAdapter(MainActivity.this, data);
mRecyclerView.setAdapter(mAdapter);

在 RecyclerView 行布局中添加一个 TextView R.layout.list_item
在您的适配器中使用此 TextView 来设置图像名称。

public static class MyItemHolder extends RecyclerView.ViewHolder {
ImageView mImg;
TextView mTextView;


public MyItemHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.item_textview);
mImg = (ImageView) itemView.findViewById(R.id.item_img);
}
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

Glide.with(context).load(data.get(position).getUrl())
.thumbnail(0.5f)
.override(200,200)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(((MyItemHolder) holder).mImg);
holder.mTextView.setText(data.get(position).getName());

}

其余代码将是相同的。

关于java - 如何根据android中的图像选择设置图像标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112481/

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