回收视图,包括多个布局ListView中的单选按钮

时间:2014-06-27 07:34:49

标签: android listview radio-button recycle

我在这里尝试了很多不同的帖子,但没有给我解决方案。 无论我尝试过哪种风格都没关系:没有ViewHolder,ViewHolder,有2个ViewHolders ......每次点击一个RadioButton并滚动,视图都会重复。

checklist.xml

         <ListView
         android:id="@+id/lv_cl"
         android:choiceMode="singleChoice"
         android:layout_below="@id/iv_cl_bar"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:layout_marginBottom="25dp"
         />

listitem_header.xml

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/tv_item_nr"
    style="@style/tv_cl"
    android:layout_marginLeft="@dimen/customList_marginLeft"
    android:layout_width="@dimen/checklist_nr_width"
    android:text="1"
     />

<TextView
    android:id="@+id/tv_item_issue"
    style="@style/tv_cl"
    android:layout_width="@dimen/checklist_issue_width"
    android:layout_toRightOf="@id/tv_item_nr"
    android:layout_marginLeft="@dimen/checklist_tv_bar_marginLeft"
    />
</LinearLayout>

listitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/tv_item_nr"
    style="@style/tv_cl"
    android:layout_marginLeft="@dimen/customList_marginLeft"
    android:layout_width="@dimen/checklist_nr_width"
/>


<TextView
    android:id="@+id/tv_item_issue"
    style="@style/tv_cl"
    android:layout_width="@dimen/checklist_issue_width"
    android:layout_marginLeft="@dimen/checklist_tv_bar_marginLeft"
    android:layout_toRightOf="@id/tv_item_nr" />

<RadioGroup
    android:id="@+id/rg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/checklist_tv_bar_marginLeft"
    android:layout_toRightOf="@id/tv_item_issue"
    android:orientation="horizontal" >

    <!--android:background="@drawable/checkbox_none"
    android:button="@null"
    -->
    <RadioButton
        android:id="@+id/rb_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <RadioButton
        android:id="@+id/rb_open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

         />
</RadioGroup>
</LinearLayout>

CustomAdapter

    static class ViewHolder {
    TextView tv_nr;
    TextView tv_issue;
    RadioButton rb_ok, rb_open;
}

public class ListViewCustomAdapter extends BaseAdapter {

    ArrayList<Item> checklistitems;
    public LayoutInflater inflater;
    public Activity context;

    public ListViewCustomAdapter(Activity context, ArrayList<Item> itemList) {
        super();
        this.context = context;
        this.checklistitems = itemList;

    }

    private static final int QUESTION = 0;
    private static final int HEADER = 1;
    private static final int TYPE_MAX_COUNT = 2;

    @Override
    public int getItemViewType(int position) {
        Item item = (Item) getItem(position);
        if (item.isQuestion()) {
            return QUESTION;
        } else {
            return HEADER;
        }
    }

    @ItemClick
    void lv_cl(Item item) {

    }

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

    @Override
    public int getCount() {
        return checklistitems.size();
    }

    @Override
    public Object getItem(int position) {
        return checklistitems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        ViewHolder viewHolder;

        int type = getItemViewType(position);
        final Item item = (Item) getItem(position);

        if (convertView == null) {

            this.inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new ViewHolder();

            switch (type) {
                case HEADER:
                    convertView = inflater.inflate(
                            R.layout.checklist_listitem_header, null);
                    break;

                case QUESTION:
                    convertView = inflater.inflate(R.layout.checklist_listitem,
                            null);
                    viewHolder.rb_ok = (RadioButton) convertView
                            .findViewById(R.id.rb_ok);
                    viewHolder.rb_ok.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            item.setSelectYes(true);
                        }
                    });
                    viewHolder.rb_open = (RadioButton) convertView
                            .findViewById(R.id.rb_open);

                    convertView.setTag (viewHolder);

                    break;
            }
            viewHolder.tv_nr = (TextView) convertView
                    .findViewById(R.id.tv_item_nr);
            viewHolder.tv_issue = (TextView) convertView
                    .findViewById(R.id.tv_item_issue);
            convertView.setTag (viewHolder);
        }

        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_nr.setText(item.getGeneratedNumber());
        viewHolder.tv_issue.setText(item.getQuestion());

        switch (type){
            case QUESTION:
                viewHolder.rb_ok.setSelected(item.isSelectYes());
                break;
        }
        return convertView;
    }
}

0 个答案:

没有答案