开发者

Draw inside ListView item

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 s开发者_运维技巧lightly 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜