动态创建自定义列表视图问题

时间:2016-01-11 06:31:33

标签: android android-layout listview android-studio android-listview

我们正在开发一个有问卷调查部分的项目。问题及其选项是动态生成的。为了测试,我们创建了一个主类和一个自定义适配器。在适配器中,我们尝试动态创建单选按钮。 问题是在滚动listview时,选项会重复或合并。为什么会发生这种情况。我已经显示了以下代码。

主要班级:

public class MainActivity extends AppCompatActivity {
    CustomAdapter cadp;
    ListView list_questions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list_questions= (ListView) findViewById(R.id.list_question);
        cadp=new CustomAdapter(MainActivity.this);
        list_questions.setAdapter(cadp);


    }

}

自定义适配器:

public class CustomAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    SingleHolder sh;



    public CustomAdapter(Context context) {
        this.context=context;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

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

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_choice_layout, null);
            sh = new SingleHolder();
            sh.single_question_name = (TextView) convertView.findViewById(R.id.single_question_name);
            sh.single_radiogroup = (RadioGroup) convertView.findViewById(R.id.single_radiogroup);
            convertView.setTag(sh);

        }
        else
        {
            sh = (SingleHolder) convertView.getTag();
        }
        sh.single_question_name.setText("question name");

        for (int j = 0; j < 3; j++)
        {
            RadioButton rb = new RadioButton(convertView.getContext());
            rb.setText("value");
            sh.single_radiogroup.addView(rb);
        }


        return convertView;
    }
    private   class SingleHolder
    {
        TextView single_question_name;
        RadioGroup single_radiogroup;
    }
}

单个列表元素布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_question_name"
        />
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_radiogroup"
        android:orientation="vertical"></RadioGroup>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

  

问题是在滚动列表视图时,选项会重复   或者合并

下面:

for (int j = 0; j < 3; j++)
  {
    RadioButton rb = new RadioButton(convertView.getContext());
    rb.setText("value");
    sh.single_radiogroup.addView(rb);
  }

这些行会导致问题,因为当convertView is not equals to null表示RadioButton's中的single_radiogroup已经可用时,无需再次添加。

RadioButton

时添加convertView is null
 if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_choice_layout, null);
            sh = new SingleHolder();
           ....
         // add RadioButton here
        }
        else
        {
            sh = (SingleHolder) convertView.getTag();
        }

编辑:

根据您的要求,在添加新内容之前从RadioGroup中删除旧视图:

sh.single_radiogroup.clearCheck();
sh.single_radiogroup.removeAllViews();
for (int j = 0; j < 3; j++)
   {
      RadioButton rb = new RadioButton(convertView.getContext());
      rb.setText("value");
      sh.single_radiogroup.addView(rb);
   }

答案 1 :(得分:0)

我认为这是因为您正在直接修改传递给“getView”方法的“convertView”。

尝试在变量中保存“convertView”并执行“setTag”&amp;对该变量进行“getTag”操作。

这是链接怎么做(这是Xamarin的android代码,我希望这是有用的)

https://blog.xamarin.com/creating-highly-performant-smooth-scrolling-android-listviews/

在xamarin中有一个设施,我们可以用radio&amp; amp;生成listview。选择复选框。 这对你有帮助吗?请查看此链接一次

http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_3_-_customizing_a_listview“s_appearance /