如何在android listview中进行就地编辑?

时间:2012-03-29 13:03:40

标签: android listview inplace-editing

基本上我有一个列表视图,每个列表项都有(2个textviews和一个复选框)。单击特定列表项时,我想用新的编辑文本视图和一些按钮替换该行。 我该如何实现? 我应该使用整数变量来存储所选项目的当前位置并加载不同的视图或使用动作动作事件来获取当前所选项目吗?

3 个答案:

答案 0 :(得分:2)

EditViews实际上是从TextView子类化的,所以你实际上可以在任何地方使用EditText,然后根据你的需要设置edditable = true \ false。

只是另一种可能的解决方案,但Tim的回答也是合适的。

答案 1 :(得分:1)

一种可能的解决方案是让你的row.xml文件包含(2个textviews和一个复选框)和(EditText + some Buttons)但是将EditText和Buttons默认为android:visibility="gone"然后你可以设置onItemClickListener ()用于ListView,它将在适当的视图上调用view.setVisibility()以使它们可见/不可见。这对用户来说似乎是新项目正在替换列表中该行的旧项目。

答案 2 :(得分:1)

您的onItemClick()应如下所示:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    MyAdapter myAdapter = (MyAdapter) parent.getAdapter();
    MyItem myItem = (MyItem) myAdapter.getItem(position);
    myItem.setSelected(true);  // set selected flag

    // notify the data has been changed and the view should refresh itself
    myAdapter.notifyDataSetChanged();

    // you can obtain the item view type by calling
    // myAdapter.getItemViewType(position);
    ...
}

现在覆盖适配器中的getViewTypeCount()getItemViewType()getView()方法:

class MyAdapter extends ArrayAdapter<MyItem> {

    private LayoutInflater mInflater;
    private static final int VIEW_ITEM_NORMAL = 0;
    private static final int VIEW_ITEM_SELECTED = 1;
    ...

    @Override
    public int getItemViewType(int position) {
        return getItem(position).isSelected() ? VIEW_ITEM_SELECTED : VIEW_ITEM_NORMAL;
        // implement isSelected()
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        int layoutId;
        int viewType = getItemViewType(position);
        if (v == null) {
            switch (viewType) {
                case VIEW_ITEM_NORMAL:
                    layoutId = R.layout.list_item;
                    break;
                case VIEW_ITEM_SELECTED:
                    layoutId = R.layout.list_item_selected;
                    break;
                default:
                    layoutId = R.layout.list_item;
                    break;
            }
            v = mInflater.inflate(layoutId, parent, false);
        } else {
            v = convertView;
        }
        ...

如果只有一个项目可以同时更改视图,最好将所选标志存储在适配器中:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    MyAdapter myAdapter = (MyAdapter) parent.getAdapter();
    myAdapter.setSelected(position);
    myAdapter.notifyDataSetChanged();
    ...
}

class MyAdapter extends ArrayAdapter<MyItem> {

    private int mSelected = -1;
    ...

    public void setSelected(int position) {
        mSelected = position;
    }

    @Override
    public int getItemViewType(int position) {
        return (mSelected == position) ? VIEW_ITEM_SELECTED : VIEW_ITEM_NORMAL;
    }
    ...

不要忘记应用ViewHolder模式,例如: here