解决方法:
dependencies {
implementation 'com.android.volley:volley:1.2.0'
}
Image
的Java类,用于存储图像的URL和标题。代码示例:public class Image {
private String imageUrl;
private String title;
public Image(String imageUrl, String title) {
this.imageUrl = imageUrl;
this.title = title;
}
public String getImageUrl() {
return imageUrl;
}
public String getTitle() {
return title;
}
}
ImageAdapter
的Java类,用于在RecyclerView中显示图像。代码示例:public class ImageAdapter extends RecyclerView.Adapter {
private Context context;
private List imageList;
public ImageAdapter(Context context, List imageList) {
this.context = context;
this.imageList = imageList;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
Image image = imageList.get(position);
holder.textViewTitle.setText(image.getTitle());
Glide.with(context)
.load(image.getImageUrl())
.into(holder.imageView);
}
@Override
public int getItemCount() {
return imageList.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textViewTitle;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
textViewTitle = itemView.findViewById(R.id.text_view_title);
}
}
}
MainActivity
的Java类,用于获取PHP MySQL中的图像数据并将其显示在RecyclerView中。代码示例:public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ImageAdapter imageAdapter;
private List imageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
imageList = new ArrayList<>();
imageAdapter = new ImageAdapter(this, imageList);
recyclerView.setAdapter(imageAdapter);
// 发送网络请求获取图像数据
fetchImages();
}
private void fetchImages() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://your-php-mysql-api-url.com",
new Response.Listener() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String imageUrl = jsonObject.getString("image_url");
String title = jsonObject.getString("title");
Image image = new Image(imageUrl, title);
imageList.add(image);
}
imageAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
item_image.xml
的布局文件,用于定义每个图像项的外观。代码示例:
以上就是使用Android全图像与RecyclerView、CardView和V