我试过滑行...但无法对它进行处理......“https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/”在本教程中他们使用了图像类。它是必要的还是Glide或picaso可以解决的?我是新来的android请帮忙。 我的主要活动
String name = persons.getString("name");
String skills = persons.getString("skills");
String image = persons.getString("image");
// tmp hash map for single person
HashMap<String, String> person = new HashMap<>();
// adding each child node to HashMap key => value
person.put("name", name);
person.put("skills", skills);
/* Glide.with(getApplicationContext ())
.load(image)
.into();*/
// adding person toperson list
personList.add(person);
ListAdapter adapter = new SimpleAdapter (
MainActivity.this, personList,
R.layout.persons_list, new String[]{"name", "skills"}, new int[]{R.id.name,
R.id.skills});
答案 0 :(得分:1)
首先,是的Glide或Picasso是在listviews和recyclerviews中处理图像的简单方法。您需要有一个自定义适配器,因为每个人都有一个要加载的图像。
创建如下的模型类;
public class Person {
private String name;
private String skills;
private String image;
// getters and setters
}
然后你需要一个自定义适配器类,如下所示;
public class PersonAdapter extends ArrayAdapter<Person> {
private Context mContext;
private List<Person> mData;
public PersonAdapter(@NonNull Context context, @NonNull List<Person> data) {
super(context, 0, data);
mContext = context;
mData = data;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if(convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.person_list_item, null);
holder = new ViewHolder();
holder.image = convertView.findViewById(R.id.imgPerson);
holder.name = convertView.findViewById(R.id.tvName);
holder.skills = convertView.findViewById(R.id.tvSkills);
convertView.setTag(holder);
}
Person person = getItem(position);
holder.name.setText(person.getName());
holder.skills.setText(person.getSkills());
// set image with picasso
Picasso.with(mContext).load(person.getImage()).into(holder.image);
return convertView;
}
static class ViewHolder {
ImageView image;
TextView name;
TextView skills;
}
}
使用ViewHolder来保存数据总是好的,因为这样可以轻松处理内存
然后在活动中创建适配器实例并将其附加到列表视图
PersonAdapter adapter = new PersonAdapter(MainActivity.this, persons);
mListView.setAdapter(adapter);
我使用过Picasso,你也可以使用Glide:)
答案 1 :(得分:0)
为人创建一个pojo类,然后就可以了 使用Gson进行数据解析如下:
Gson gson = new Gson();
Persons persons = gson.fromJson(productList, Persons.class);
然后你可以使用这样的滑行:
requestOptions = new RequestOptions();
requestOptions
.placeholder(placeholder)
.error(error)
.dontTransform()
.dontAnimate();
Glide.with(mContext).load(url).apply(requestOptions).into(imageView);