在 ListView 项目内绘制

发布于 2024-10-24 07:52:20 字数 750 浏览 1 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-10-31 07:52:20

您需要将 ListView 子类化为自定义实现。

public class CustomListAdapter extends ArrayAdapter<CustomRowData> {

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      CustomRowData currentRow = getItem(position);
      View customRowView = .... // such as CustomRowView below.
      ...
      ...
      return customRowView;
  }
}

您可以通过将 ... 替换为适当的 View 代码来更改 ListView 中每一行的视图。

例如,您可以使用 SurfaceView 在其上绘图。

class CustomRowView extends SurfaceView {
  ...
  ...
  @Override
  public void onDraw(Canvas canvas) {
    // Use |canvas| to draw on..
  }
}

上面的代码未经测试,但希望能引导您走向正确的方向。确保优化绘图,例如在可用时使用缓存、预处理等。

You would need to subclass your ListView to a custom implementation.

public class CustomListAdapter extends ArrayAdapter<CustomRowData> {

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      CustomRowData currentRow = getItem(position);
      View customRowView = .... // such as CustomRowView below.
      ...
      ...
      return customRowView;
  }
}

You can change the view for each row in the ListView by replacing the ... with the appropriate View code.

For example, you can use SurfaceView to draw on it.

class CustomRowView extends SurfaceView {
  ...
  ...
  @Override
  public void onDraw(Canvas canvas) {
    // Use |canvas| to draw on..
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文