Android ListView自定义适配器动态添加按钮?

时间:2016-06-29 22:54:20

标签: android listview widget custom-adapter programmatically-created

我使用自定义适配器为活动设置ListView(从主活动到达)。我试图动态地添加一个按钮(以及稍后的EditText),因为我没有使用定义的布局文件(只是ListView的每一行的布局)。似乎无法让它发挥作用(最后5行)。这是我的活动代码:

public class SubSelectActivity extends ListActivity {
private List<Subs> list;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayAdapter<Subs> adapter = new DialogAdapter(this, list);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(SubSelectActivity.this, "TEST", Toast.LENGTH_SHORT).show();
            CheckBox checkBox = (CheckBox) view.findViewById(R.id.check);
            checkBox.toggle();
            if (checkBox.isChecked()) {
                imageTF.add(images.get(position));
            } else {
                imageTF.remove(images.get(position));
            }

        }
    });

    Button myButton = new Button(this);
    myButton.setText("Push Me");
    LinearLayout ll = new LinearLayout(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
}

这是我的适配器设置:

public class DialogAdapter extends ArrayAdapter<SubSelectActivity.Subs> {
private final List<SubSelectActivity.Subs> list;
private final Activity context;

static class ViewHolder {
    protected TextView name;
    protected CheckBox checkBox;
}

public DialogAdapter(Activity context, List<SubSelectActivity.Subs> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = null;
    // reuse views
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.row, null);
        // configure view holder
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.name = (TextView) view.findViewById(R.id.text1);
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check);
        view.setTag(viewHolder);
    } else {
        view = convertView;
    }

    final ViewHolder holder = (ViewHolder) view.getTag();
    holder.name.setText(list.get(position).getName());
    holder.checkBox.setChecked(list.get(position).IsChecked());
    return view;
    }
}

任何人都可以帮我弄清楚如何在ListView下面添加小部件吗?

编辑:我的row.xml类型为CheckableRelativeLayout:

public class CheckableRelativeLayout extends RelativeLayout implements
    Checkable {

private boolean isChecked;
private List<Checkable> checkableViews;

public CheckableRelativeLayout(Context context, AttributeSet attrs,
                               int defStyle) {
    super(context, attrs, defStyle);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, int checkableId) {
    super(context);
    initialise(null);
}

/*
  * @see android.widget.Checkable#isChecked()
  */
public boolean isChecked() {
    return isChecked;
}

/*
  * @see android.widget.Checkable#setChecked(boolean)
  */
public void setChecked(boolean checked) {
    if (isChecked != checked) {
        isChecked = checked;
        refreshDrawableState();
    }
}

/*
  * @see android.widget.Checkable#toggle()
  */
public void toggle() {
    this.isChecked = !this.isChecked;
    for (Checkable c : checkableViews) {
        c.toggle();
    }
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    final int childCount = this.getChildCount();
    for (int i = 0; i < childCount; ++i) {
        findCheckableChildren(this.getChildAt(i));
    }
}

/**
 * Read the custom XML attributes
 */
private void initialise(AttributeSet attrs) {
    this.isChecked = false;
    this.checkableViews = new ArrayList<Checkable>(5);
}

/**
 * Add to our checkable list all the children of the view that implement the
 * interface Checkable
 */
private void findCheckableChildren(View v) {
    if (v instanceof Checkable) {
        this.checkableViews.add((Checkable) v);
    }

    if (v instanceof ViewGroup) {
        final ViewGroup vg = (ViewGroup) v;
        final int childCount = vg.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(vg.getChildAt(i));
        }
    }
}
}

SOLUTION:

此链接是适用于我的解决方案Add buttons to a listactivity

2 个答案:

答案 0 :(得分:0)

    //I **** xml
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = new LinearLayout(context);

            TextView tv = new TextView(context);
            tv.setText(list.get(position).getName());

            CheckBox ch =new CheckBox(context);
            ch.setChecked(list.get(position).IsChecked());      

            ((LinearLayout)convertView).addView(tv);
            ((LinearLayout)convertView).addView(ch);
        } 
        return convertView ;
    }

答案 1 :(得分:0)

我个人认为你应该为ListView使用Xml Layout,你可以添加按钮和EditText Hidden(visibility =“GONE”),然后在需要时显示它。

,但 根据我的个人经验和我读过的其他帖子,不建议在ListView中使用EditText,EditText无法保留焦点,因此,当在ListView上显示时,Text不可编辑。

所以我的建议是使用LinearLayout并动态添加自定义视图,它的工作量稍微多一些,但你会遇到麻烦。