在 ListView 项目内绘制
我正在尝试创建一个 ListView,在其中我可以在 ListView 中的每个项目中绘制一些基本形状。我发现了许多在 ListView 中使用视图/布局来添加图像等的示例,但我不确定如何使用 Canvas 或类似工具绘制每个示例。此外,这些示例的工作方式似乎都略有不同,所以我想知道最好的策略是什么。
目前我只有一个 Main 类,它用基本文本填充 ListView 。
Main.java
public class Main extends Activity {
private ListView listView;
String[] list = {"One","Two","Three"};
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.ListView1);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
}
}
现在我想我需要在这里更改适配器位,并编写另一个可能扩展 View 的类,我可以绘制它,然后将其添加为 ListView 中的项目?
提前感谢您的帮助。
I'm attempting to create a ListView where I can draw some basic shapes into each item in the ListView. I've found many examples using Views/Layouts within a ListView to add an image etc, but am unsure how I would draw into each one using a Canvas or similar. Furthermore these examples seem to all work in slightly different ways, so I was wondering what the best strategy is.
At the moment I just have a Main class which populates the ListView with basic text.
Main.java
public class Main extends Activity {
private ListView listView;
String[] list = {"One","Two","Three"};
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.ListView1);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
}
}
Now I gather I'll need change the Adapter bit here, and also write another class which possibly extends View, which I can draw to, then add this as an item in the ListView?
Thanks for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 ListView 子类化为自定义实现。
您可以通过将
...
替换为适当的View
代码来更改ListView
中每一行的视图。例如,您可以使用 SurfaceView 在其上绘图。
上面的代码未经测试,但希望能引导您走向正确的方向。确保优化绘图,例如在可用时使用缓存、预处理等。
You would need to subclass your ListView to a custom implementation.
You can change the view for each row in the
ListView
by replacing the...
with the appropriateView
code.For example, you can use
SurfaceView
to draw on it.The above code isn't tested, but hopefully will drive you to the right direction. Make sure you optimize your drawing such as use caching when available, preprocess, etc.