在RecyclerView已经出了几年了,为什么还要用ListView而不用RecyclerView,因为RecyclerView为了扩展性更好,它将许多功能都抽取了出去,让用户自己去实现。所以有时候为了快速实现一个功能,我们应该采取,最快速的方法。这里,我使用ListView的原因是ListView自己有添加Header和Ffooter的方法,可以直接使用。好接下来就是代码了:
MainActivity的布局代码,没什么就一个ListView
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:scrollbars="none">
</ListView>
MainActivity
在代码里面添加头部和尾部
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private List<RiskTitleBean> jsonBeen;
private QuestionAdapter mQuestionAdapter;
private ListView mLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLv = (ListView) findViewById(R.id.lv);
initData();
bindAdapter();
}
private void bindAdapter() {
TextView textView = new TextView(this);
textView.setTextColor(Color.parseColor("#7B7873"));
textView.setBackgroundColor(Color.parseColor("#e7991c"));
textView.setText(" 本问卷调查表旨在协助投资人了解自身的投资状况," +
"主要包括投资偏好、风险承受能力及风险认知能力等方面" +
",问卷结果不能完全呈现投资人自身投资状况及在面对投资风险时的真正态度," +
"但是可以向投资人提供一些衡量自身风险属性的指标。");
textView.setPadding(DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
View inflate = View.inflate(this, R.layout.question_foot_item, null);
TextView tv_Ceshi = (TextView) inflate.findViewById(R.id.tv_ceshi);
TextView tv_Zhong = (TextView) inflate.findViewById(R.id.tv_zhong);
TextView tv_Mian = (TextView) inflate.findViewById(R.id.tv_mian);
TextView tv_Submit = (TextView) inflate.findViewById(R.id.tv_submit);
tv_Submit.setOnClickListener(this);
mLv.addHeaderView(textView);
mLv.addFooterView(inflate);
mQuestionAdapter = new QuestionAdapter(this, jsonBeen);
mLv.setAdapter(mQuestionAdapter);
}
private void initData() {
String questionStr = Util.getStringFormAsset(this, "question.json");
jsonBeen = JSON.parseArray(questionStr, RiskTitleBean.class);
}
@Override
public void onClick(View v) {
submitResult();
}
//获取答案
private void submitResult() {
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < jsonBeen.size(); i++) {
for (int j = 0; j < jsonBeen.get(i).getAnswerList().size(); j++) {
if (jsonBeen.get(i).getAnswerList().get(j).isChecked()) {
stringBuilder = stringBuilder.append(jsonBeen.get(i).getAnswerList().get(j).getOption_());
Log.e("sele", i + "--" + j);
}
}
}
if (stringBuilder.length() == 10) {
//发送请求
//TODO
Toast.makeText(this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请完成所有题目", Toast.LENGTH_SHORT).show();
}
}
}
//这是question.json中的内容,选择题通常内容比较多,我是建议放到资源文件里面去。
![QQ图片20180212103848.png](http://upload-images.jianshu.io/upload_images/6156764-ef11f5c93ca46983.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
接下来时Adapter,根据选项的个数,动态创建radioButton
public class QuestionAdapter extends BaseAdapter {
private Context mContext;
private List<RiskTitleBean> jsonBeen = new ArrayList<>();
public QuestionAdapter(Context context, List<RiskTitleBean> jsonBeen) {
this.mContext = context;
this.jsonBeen = jsonBeen;
}
@Override
public int getCount() {
return jsonBeen.size();
}
@Override
public Object getItem(int i) {
return jsonBeen.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
//判断view是否为空
if (view == null) {
holder = new ViewHolder();
view = View.inflate(mContext, R.layout.item_risktitle, null);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
holder.rg = (RadioGroup) view.findViewById(R.id.rg);
//设置tag
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
//设置数据
holder.tvTitle.setText(jsonBeen.get(position).getQuestion());
//根据选项创建单选按钮
RadioButton radioButton = null;
//这里需要每次都移除原先的单选按钮对象,
holder.rg.removeAllViews();
for (int i = 0; i < jsonBeen.get(position).getAnswerList().size(); i++) {
//创建单选按钮
radioButton = new RadioButton(mContext);
radioButton.setText(jsonBeen.get(position).getAnswerList().get(i).getContent());
holder.rg.addView(radioButton);
if (jsonBeen.get(position).getAnswerList().get(i).isChecked()) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}
final int finalI = i;
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//需要把其他的状态设置成false
for (int j = 0; j < jsonBeen.get(position).getAnswerList().size(); j++) {
jsonBeen.get(position).getAnswerList().get(j).setChecked(false);
}
jsonBeen.get(position).getAnswerList().get(finalI).setChecked(true);
//Toast.makeText(mContext, "这是第 " + position + "--" + finalI + "--"
// + "个被点击了", Toast.LENGTH_SHORT).show();
}
});
}
return view;
}
class ViewHolder {
public RadioGroup rg;
public TextView tvTitle;
}
}
编写这个其实是很简单的,其中容易出错的点是在选了前面的题目的时候,下拉,会发现下面的题目也有的被选中了,所以,我在对象里面添加了个boolean的字段,作为是否选中的状态判断。ListView和RecyclerView中的给控件设置数据那一块,一定要注意两种状态的情况下的if else ,else很重要,不然,你可能会发现数据错乱等显示问题。