根据Android Viewpager和适配器中的条件显示Radiogroup

时间:2018-12-10 07:09:47

标签: java android android-viewpager

问题: 我有一些数据从api显示到viewpager中,并且可以正常工作。 我想根据基于api的anwser类型r_type显示一种无线电组的类型。我必须隐藏其余的广播组。

这是Web服务的结果:

{  
   "RESPONSECODE":200,
   "RESPONSEDATA":[  
      {  
         "id_Question":"1",
         "Content":"How many items needed to assembly",
         "Description":"How many items needed to assembly",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"items_needed_number",
         "r_type":"numeric_range",
         "Activated":"1",
         "DateCreated":"2017-11-18 12:20:46",
         "DateModified":"2017-11-18 12:20:46"
      },
      {  
         "id_Question":"2",
         "Content":"What kind of furniture would you like assembled?",
         "Description":"What kind of furniture would you like assembled?",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"kind_of_furnitures",
         "r_type":"kind_of_furnitures",
         "Activated":"1",
         "DateCreated":"2017-11-18 12:20:46",
         "DateModified":"2017-11-18 12:20:46"
      },
      {  
         "id_Question":"3",
         "Content":"Are you able to provide assembly instruction or make and model information?",
         "Description":"Are you able to provide assembly instruction or make and model information?",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"capability_to_provide",
         "r_type":"yes_or_no",
         "Activated":"1",
         "DateCreated":"2017-11-18 12:34:32",
         "DateModified":"2017-11-18 12:34:32"
      },
      {  
         "id_Question":"4",
         "Content":"Would you like any additional services?",
         "Description":"Would you like any additional services?",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"additionnal_services",
         "r_type":"yes_or_no",
         "Activated":"1",
         "DateCreated":"2017-11-18 12:34:32",
         "DateModified":"2017-11-18 12:34:32"
      },
      {  
         "id_Question":"5",
         "Content":"When would you like the furniture assembler?",
         "Description":"When would you like the furniture assembler?",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"like_furniture_assembler",
         "r_type":"yes_or_no",
         "Activated":"1",
         "DateCreated":"2017-11-18 12:34:32",
         "DateModified":"2017-11-18 12:34:32"
      },
      {  
         "id_Question":"41",
         "Content":"Location",
         "Description":"",
         "Category_id":"2",
         "User_id":"1",
         "q_alias":"Location.",
         "r_type":"user_input",
         "Activated":"1",
         "DateCreated":"2018-03-25 07:02:13",
         "DateModified":"2018-03-25 07:02:13"
      }
   ],
   "Success":true
}

我尝试过的事情: 布局如下

view_page1.xml,此布局显示无线电组

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/questionview"
            android:layout_width="match_parent"
            android:layout_height="133dp"
            android:layout_margin="5dp"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp">
            <TextView
                android:id="@+id/questionContent"
                android:layout_width="0dp"
                android:layout_height="103dp"
                android:layout_weight="1"

                android:padding="5dp"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_blue_dark"
                android:textStyle="bold" />

        </LinearLayout>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/answers_id"
                android:layout_width="match_parent"
                android:layout_height="367dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                android:layout_marginBottom="3dp"
                android:gravity="center"
                android:orientation="horizontal">

                <RadioGroup
                    android:id="@+id/numeric"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <RadioButton
                        android:id="@+id/radio1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/One" />

                    <RadioButton
                        android:id="@+id/radio2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/Two" />

                    <RadioButton
                        android:id="@+id/radio3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/Three" />

                    <RadioButton
                        android:id="@+id/radio4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/Four" />

                    <RadioButton
                        android:id="@+id/radio5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/Five" />
                </RadioGroup>

                <RadioGroup
                    android:id="@+id/numericrange"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <RadioButton
                        android:id="@+id/zerotothree"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/zerotothree" />

                    <RadioButton
                        android:id="@+id/threetosix"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/threetosix" />

                    <RadioButton
                        android:id="@+id/seventoten"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/seventoten" />

                    <RadioButton
                        android:id="@+id/tentofifteen"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/tentofifteen" />

                    <RadioButton
                        android:id="@+id/fifteentotwenty"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:text="@string/fifteentotwenty" />
                </RadioGroup>
    </LinearLayout>
    </android.support.v7.widget.CardView>
        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:gravity="center"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/replytype"
                android:layout_width="0dp"
                android:layout_height="10dp"
                android:layout_weight="1"
                android:padding="5dp"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_blue_dark"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

viewpagers.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

viewpager适配器: 我在这里根据r_type调用无线电组,我在显示和隐藏其余无线电组的类型。似乎只有第一个广播组还在显示,我该如何解决。如果您进行检查,可以看到我有一个r_type数字,并且每个r_type必须显示 numberic 单选组,并且 numberRange 不可见。

public QuestionsAdapter(Context mContext, List<Questions> questionsList) {
        this.mContext = mContext;
        this.questionsList = questionsList;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override
    public int getCount() {
        return questionsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container,  int position) {
        Questions questionObject = questionsList.get(position);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.view_item1, container, false);
        //ViewGroup view = (ViewGroup) inflater.inflate(R.layout.view_item1, container, false);
        questionView = view.findViewById(R.id.questionContent);
        questionView.setText(questionObject.getContent());
        replytype = view.findViewById(R.id.replytype);
        replytype.setText(questionObject.getRtype());
        numberic =view.findViewById(R.id.numeric);
       numberRange = view.findViewById(R.id.numericrange);
        buttonChecked = numberic.getCheckedRadioButtonId();

        if(replytype.equals("numeric"))
        {
            numberRange.setVisibility(View.INVISIBLE);
            numberic.setVisibility(View.VISIBLE);
            if(buttonChecked == R.id.radio1 || buttonChecked == R.id.radio2 || buttonChecked == R.id.radio3||buttonChecked == R.id.radio4||buttonChecked == R.id.radio5 ){
                numberic.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

                    @Override
                    public  void onCheckedChanged(RadioGroup group,int checkedId){
                        if(checkedId == R.id.radio1){
                            //  Toast.makeText(QuestionsActivity.this,"choice: One Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.radio2){
                            //  Toast.makeText(QuestionsActivity.this,"choice: Two Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.radio3){
                            //  Toast.makeText(QuestionsActivity.this,"choice: Three Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.radio4){
                            //   Toast.makeText(QuestionsActivity.this,"choice: Four Chosen",Toast.LENGTH_LONG).show();
                        }
                        else{
                            // Toast.makeText(QuestionsActivity.this,"choice: Five Chosen",Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
        if(replytype.equals("numeric_range"))
        {
            numberic.setVisibility(View.INVISIBLE);
            numberRange.setVisibility(View.VISIBLE);
            numberRange.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

                    @Override
                    public  void onCheckedChanged(RadioGroup group,int checkedId){
                        if(checkedId == R.id.zerotothree){
                            //  Toast.makeText(QuestionsActivity.this,"choice: One Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.threetosix){
                            //  Toast.makeText(QuestionsActivity.this,"choice: Two Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.seventoten){
                            //  Toast.makeText(QuestionsActivity.this,"choice: Three Chosen",Toast.LENGTH_LONG).show();
                        }
                        else  if(checkedId == R.id.tentofifteen){
                            //   Toast.makeText(QuestionsActivity.this,"choice: Four Chosen",Toast.LENGTH_LONG).show();
                        }
                        else{
                            // Toast.makeText(QuestionsActivity.this,"choice: Five Chosen",Toast.LENGTH_LONG).show();
                        }
                    }
                });

        }



        container.addView(view);
        return view;
    }


         private CharSequence getQuestionCOntent(int position)
        {
            Questions questions = questionsList.get(position);;
            return mContext.getString(questions.getQuestionId());
        }

}

0 个答案:

没有答案