【问题标题】:Adding text and images to a ListView?将文本和图像添加到 ListView?
【发布时间】:2019-01-14 15:54:52
【问题描述】:

我想为ListView 的每个列表项添加图像和文本。让我给你看我的代码:

ArrayAdapter<String> arrayadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayListString);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener){
@override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
...
}
}

我该如何解决这个问题?

感谢您的耐心和考虑。

【问题讨论】:

标签: android listview android-listview


【解决方案1】:

请考虑使用 RecyclerView,要使用 recyclerview,您需要有一个图像列表(或 URL)和一个文本列表,或者更好的是包含两者的对象。

关于你的活动或片段

RecyclerViewAdapter adapter = new RecyclerViewAdapter();
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);

其中适配器是 RecyclerviewAdapter 和 RecyclerViewHolder '因为您想要做的方法仅适用于字符串,没有自定义视图。 请查看此官方文档:https://developer.android.com/guide/topics/ui/layout/recyclerview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多