展开样式答题写法

先看看实现的效果:
这里写图片描述

首先看到的第一反应就是一个ListView或者RecyclerView,但是这个里面要求只能按照顺序答题,也就是说,假设3题没有答,第四题是不能点击的。所以这里我用了LinearLayout模拟了一个ListView。

1、自定义一个View,继承LinearLayout实现ListView的功能

public class SelfTestView extends LinearLayout {

    private BaseAdapter adapter;

    public SelfTestView(Context context) {
        this(context, null);
    }

    public SelfTestView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelfTestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setAdapter(BaseAdapter adapter){
        this.adapter = adapter;
        for(int i = 0;i<adapter.getCount();i++){
            View view = adapter.getView(i,null,this);
            addView(view);
            if(i == 0){
                startAnswer(false,0);
            }
        }
    }

    public void startAnswer(boolean isAlreadyAnswer,int currentQuestion){

        if(isAlreadyAnswer || adapter.getCount()<=currentQuestion){
            return;
        }
        View view = getChildAt(currentQuestion);

        TextView mTextView = (TextView) view.findViewById(R.id.text_question);
        TextView noneView = (TextView) view.findViewById(R.id.btn_none);
        TextView littleView = (TextView) view.findViewById(R.id.btn_little);
        TextView someView = (TextView) view.findViewById(R.id.btn_some);
        TextView oftenView = (TextView) view.findViewById(R.id.btn_often);
        TextView alwaysView = (TextView) view.findViewById(R.id.btn_always);

        mTextView.setTextColor(getResources().getColor(R.color.color_3183e9));

        noneView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click);
        littleView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click);
        someView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click);
        oftenView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click);
        alwaysView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click);

        noneView.setTextColor(getResources().getColor(R.color.color_3183e9));
        littleView.setTextColor(getResources().getColor(R.color.color_3183e9));
        someView.setTextColor(getResources().getColor(R.color.color_3183e9));
        oftenView.setTextColor(getResources().getColor(R.color.color_3183e9));
        alwaysView.setTextColor(getResources().getColor(R.color.color_3183e9));

        noneView.setEnabled(true);
        littleView.setEnabled(true);
        someView.setEnabled(true);
        oftenView.setEnabled(true);
        alwaysView.setEnabled(true);
    }
}

2、既然是是模拟ListView,那就少不了Adapter

public class TestQuestionAdapter extends BaseAdapter {

    private List<String> questions;//题
    private Context mContext;
    private SelfTestView selfTestView;
    private TextView mAnswerCountView;//答题进度
    private boolean alreadyAnswer[];//纪录题是否已经答过
    private TextView mSubmitButton;//提交按钮(因为答完题后,提交按钮由灰色变为蓝色,所以要在这个里面处理)
    private Map<String, Object> answers;//存放答案的容器

    public void setData(Context mContext, List<String> questions, SelfTestView selfTestView, TextView mSubmitButton, TextView answerCount) {
        this.questions = questions;
        this.mContext = mContext;
        this.selfTestView = selfTestView;
        this.mAnswerCountView = answerCount;
        this.mSubmitButton = mSubmitButton;
        alreadyAnswer = new boolean[questions.size()];
        answers = new HashMap<>();
        mAnswerCountView.setText(0 + "/" + questions.size());//一开始答题进度为0
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TestQuestionAdapter.ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new TestQuestionAdapter.ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_self_test, parent, false);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.text_question);
            viewHolder.mNoneView = (TextView) convertView.findViewById(R.id.btn_none);
            viewHolder.mLittleView = (TextView) convertView.findViewById(R.id.btn_little);
            viewHolder.mSomeView = (TextView) convertView.findViewById(R.id.btn_some);
            viewHolder.mOftenView = (TextView) convertView.findViewById(R.id.btn_often);
            viewHolder.mAlwaysView = (TextView) convertView.findViewById(R.id.btn_always);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (TestQuestionAdapter.ViewHolder) convertView.getTag();
        }
        final TextView[] btns = {viewHolder.mNoneView, viewHolder.mLittleView, viewHolder.mSomeView, viewHolder.mOftenView, viewHolder.mAlwaysView};

        viewHolder.textView.setText((position + 1) + "." + questions.get(position));//由于题没有给编号,所以这里可以自己添加

        viewHolder.mNoneView.setEnabled(false);
        viewHolder.mLittleView.setEnabled(false);
        viewHolder.mSomeView.setEnabled(false);
        viewHolder.mOftenView.setEnabled(false);
        viewHolder.mAlwaysView.setEnabled(false);


        viewHolder.mNoneView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e("-------->", "none" + position);
                initBtn(btns, 0);
                judgeStatus(position);
                //收集答案
                //我这里用Map存放的答案,修改的时候根据key相同直接替换,如果用List和对象的形势,要记得先在list中移除要改的那个题,再添加答案,否则可能答5个题出现8答案的情况

               /* answers是list<对象>的情况
               if(answers.size()>position){//这个判断是说修改不是新答的题
                    answers.remove(position);//移除上一次的答案
                }
                answers.add(对象);//重新添加答案
                */

                answers.put(position + "", "答案id");
            }
        });

        viewHolder.mLittleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("-------->", "little" + position);
                initBtn(btns, 1);
                judgeStatus(position);
                answers.put(position + "", "答案id");
            }
        });

        viewHolder.mSomeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initBtn(btns, 2);
                judgeStatus(position);
                answers.put(position + "", "答案id");
            }
        });

        viewHolder.mOftenView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("-------->", "often" + position);
                initBtn(btns, 3);
                judgeStatus(position);
                answers.put(position + "", "答案id");
            }
        });

        viewHolder.mAlwaysView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("-------->", "always" + position);
                initBtn(btns, 4);
                judgeStatus(position);
                answers.put(position + "", "答案id");
            }
        });

        return convertView;
    }

    private void judgeStatus(int position) {
        //如果新答题,答题进度会修改,否则不修改
        if (!alreadyAnswer[position]) {
            mAnswerCountView.setText((position + 1) + "/" + questions.size());
        }
        //设置题为已答状态
        alreadyAnswer[position] = true;

        //答的题不是最后一个就变下一个题为可答题状态,如果是最后一个就改变提交按钮的状态
        if (position != questions.size() - 1) {
            selfTestView.startAnswer(alreadyAnswer[position + 1], position + 1);
        } else {
            changeSubmitButtonStatus();
        }
    }

    private void changeSubmitButtonStatus() {
        mSubmitButton.setEnabled(true);
        mSubmitButton.setBackgroundResource(R.drawable.bg_btn_submit_test_select);

    }

    //将答案返回供提交
    public Map<String, Object> getAnswers() {
        return answers;
    }

    //选中答案后修改界面上被点击的button背景色
    private void initBtn(TextView[] btns, int current) {
        for (int i = 0; i < btns.length; i++) {
            if (i == current) {
                btns[current].setBackgroundResource(R.drawable.btn_bg_self_test_select);
                btns[current].setTextColor(mContext.getResources().getColor(R.color.color_ffffff));
            } else {
                btns[i].setBackgroundResource(R.drawable.btn_bg_self_test_can_click);
                btns[i].setTextColor(mContext.getResources().getColor(R.color.color_3183e9));
            }
        }
    }

    class ViewHolder {
        TextView textView;
        TextView mNoneView;
        TextView mLittleView;
        TextView mSomeView;
        TextView mOftenView;
        TextView mAlwaysView;
    }
}

3、每个Item的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/bg_self_test"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1、你的脸比别人大吗"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp"
        android:lineSpacingExtra="5dp"
        android:textColor="@color/color_666666"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:gravity="center">

        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/btn_none"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="没有"
            android:textSize="14sp"
            android:textColor="@color/color_666666"
            android:clickable="true"
            android:background="@drawable/btn_bg_self_test_not_click"/>

        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/btn_little"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="很少"
            android:textSize="14sp"
            android:textColor="@color/color_666666"
            android:clickable="true"
            android:background="@drawable/btn_bg_self_test_not_click"/>

        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/btn_some"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="有时"
            android:textSize="14sp"
            android:textColor="@color/color_666666"
            android:clickable="true"
            android:background="@drawable/btn_bg_self_test_not_click"/>

        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/btn_often"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="经常"
            android:textSize="14sp"
            android:textColor="@color/color_666666"
            android:clickable="true"
            android:background="@drawable/btn_bg_self_test_not_click"/>

        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/btn_always"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="总是"
            android:textSize="14sp"
            android:textColor="@color/color_666666"
            android:clickable="true"
            android:background="@drawable/btn_bg_self_test_not_click"/>
        <View
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"/>
    </LinearLayout>

</RelativeLayout>

注意: drawable都是自定义的shape

bg_btn_submit_test_select.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="22dp"/>
    <solid android:color="@color/color_3183e9"/>
</shape>

bg_self_test.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="3dp"/>
    <solid android:color="@color/color_ffffff"/>
    <stroke android:color="@color/color_dddddd"
        android:width="0.5dp"/>
</shape>

btn_bg_self_test_can_click.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke
        android:width="1dp"
        android:color="@color/color_3183e9" />
    <solid android:color="@color/color_ffffff"/>
</shape>

btn_bg_self_test_not_click.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke
        android:width="1dp"
        android:color="@color/color_3183e9" />
    <solid android:color="@color/color_ffffff"/>
</shape>

btn_bg_self_test_select.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="@color/color_3183e9"/>

</shape>

4、使用

public class TestQuestionAcitivity extends AppCompatActivity {

    private SelfTestView mSelfTextView;
    private TextView mSubmitTestView;
    private TextView mAnswerCountView;
    private TestQuestionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_question_acitivity);
        mSelfTextView = (SelfTestView) findViewById(R.id.self_question_view);
        mSubmitTestView = (TextView) findViewById(R.id.submit_test);
        mAnswerCountView = (TextView) findViewById(R.id.text_answer_count);
        mSubmitTestView.setEnabled(false);

        adapter = new TestQuestionAdapter();
        initDatas();

    }

    private void initDatas() {

        List<String> list = new ArrayList<>();
        for(int i = 0;i<30;i++){
            list.add("你手脚经常凉吗?");
        }

        adapter.setData(TestQuestionAcitivity.this, list, mSelfTextView, mSubmitTestView,mAnswerCountView);
        mSelfTextView.setAdapter(adapter);

    }
}

activity_test_question_acitivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_test_question_acitivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.trtpre.www.demo.activitys.TestQuestionAcitivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <com.trtpre.www.demo.view.SelfTestView
                android:id="@+id/self_question_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </com.trtpre.www.demo.view.SelfTestView>


            <TextView
                android:id="@+id/submit_test"
                android:layout_width="280dp"
                android:layout_height="45dp"
                android:layout_marginLeft="49dp"
                android:layout_marginRight="49dp"
                android:textSize="16sp"
                android:textColor="@color/color_ffffff"
                android:text="提交"
                android:background="@color/color_dddddd"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="60dp"
                />

        </LinearLayout>

    </ScrollView>
    <TextView
        android:id="@+id/text_answer_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值