如何在android中隐藏可扩展列表

时间:2014-04-03 09:50:53

标签: android expandablelistview expandablelistadapter

我在活动中有一个3个可扩展列表。

我在第一个可扩展列表中有两个单选按钮,当我单击第一个单选按钮时,它应该隐藏第二个可扩展列表,如果我选择2单选按钮,它应该隐藏3可扩展。

我该怎么做?

我已经发布了我的代码部分。

public class MyActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Checkout Method");
        listDataHeader.add("Create Account");
        listDataHeader.add("Bill Address");
        listDataHeader.add("Address");
        listDataHeader.add("Order");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
//        top250.add("The Godfather");
//        top250.add("The Godfather: Part II");
//        top250.add("Pulp Fiction");
//       

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("The Conjuring");
//        nowShowing.add("Despicable Me 2");
//        nowShowing.add("Turbo");
//        nowShowing.add("Grown Ups 2");
//        nowShowing.add("Red 2");
//        nowShowing.add("The Wolverine");
// 
        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
//        comingSoon.add("The Smurfs 2");
//        comingSoon.add("The Spectacular Now");
//        comingSoon.add("The Canyons");
//        comingSoon.add("Europa Report");
// 
        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }
}

public class ExpandableListAdapter扩展了BaseExpandableListAdapter {

private Context myContext;

private List<String> _listDataHeader; // header titles

// child data in format of header title, child title

private HashMap<String, List<String>> _listDataChild;

EditText user,email,pass,repass;

String username,emailid,password,repassword,success;

EditText country,fname,lname,comp,addr1,addr2,city,state;

String coun,fn,ln,com,ad1,ad2,ci,st,po;

EditText country1,fname1,lname1,comp1,addr11,addr21,city1,state1;
String coun1,fn1,ln1,com1,ad11,ad21,ci1,st1;

CheckBox ch1; 

RadioButton rbutton;

RadioGroup rg;

int rid;

Button cbtn;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) 
{
    this.myContext = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon)
{
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition)
{
    return childPosition;
}

@Override
public View getChildView(final int groupPosition,  int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
{

    final String childText = (String) getChild(groupPosition, childPosition);

    int itemType=getChildType(groupPosition,childPosition);

    LayoutInflater infalInflater = (LayoutInflater) this.myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   /*
    if (convertView == null) 

    {
       convertView = infalInflater.inflate(R.layout.expandable_list_item, null);

    }
    */
    switch (groupPosition)
        {

        case 0:

            final LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.child_row, null);

            rg=(RadioGroup)convertView.findViewById(R.id.cradioGroup1);

            cbtn=(Button)convertView.findViewById(R.id.continebutton1);

            cbtn.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub
                    //android:groupIndicator="@android:color/transparent"
                    //.setVisibility( View.INVISIBLE );

                }
            });

            rid=rg.getCheckedRadioButtonId();

            rbutton=(RadioButton)convertView.findViewById(rid);

            System.out.println("<------Checked account in expandable adapter----->"+rbutton.getText());

            break;

        case 1:

            LayoutInflater inflater1 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater1.inflate(R.layout.child_row1, null);

            //TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);

            //tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);

            user=(EditText)convertView.findViewById(R.id.editText1);

            email=(EditText)convertView.findViewById(R.id.editText2);

            pass=(EditText)convertView.findViewById(R.id.editText3);

            repass=(EditText)convertView.findViewById(R.id.editText4);

            Button reg;

            reg=(Button)convertView.findViewById(R.id.button1);

            reg.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub
                new Register().execute();   
                }
            });
            break; 

        case 2:

            LayoutInflater inflater2 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater2.inflate(R.layout.child_row2, null);

            country=(EditText)convertView.findViewById(R.id.editcoun);

            fname=(EditText)convertView.findViewById(R.id.editfn);

            lname=(EditText)convertView.findViewById(R.id.editln);

            comp=(EditText)convertView.findViewById(R.id.editco);

            addr1 =(EditText)convertView.findViewById(R.id.editad1);

            addr2=(EditText)convertView.findViewById(R.id.editad2);

            city=(EditText)convertView.findViewById(R.id.editcity);



            Button reg1;

            reg1=(Button)convertView.findViewById(R.id.accbtn);

            reg1.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub
                new B().execute();  
                }
            });
            break;

        case 3:             

            LayoutInflater inflater3 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater3.inflate(R.layout.child_row3, null);

            ch1=(CheckBox)convertView.findViewById(R.id.check1);

            country1=(EditText)convertView.findViewById(R.id.editcoun1);

            fname1=(EditText)convertView.findViewById(R.id.editfn1);

            lname1=(EditText)convertView.findViewById(R.id.editln1);

            comp1=(EditText)convertView.findViewById(R.id.editco1);

            addr11 =(EditText)convertView.findViewById(R.id.editad11);

            addr21=(EditText)convertView.findViewById(R.id.editad21);

            city1=(EditText)convertView.findViewById(R.id.editcity1);




             break;

        case 4:             

            LayoutInflater inflater4 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater4.inflate(R.layout.child_row4, null);

            Button place;

            place=(Button)convertView.findViewById(R.id.obutton1);

            place.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                Toast.makeText(myContext, "Thank you for your Purchase", Toast.LENGTH_SHORT).show();

                }
            });



             break;


        }


   /* TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);*/
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) 
{
    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater) this.myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = infalInflater.inflate(R.layout.expandable_list_group, null);
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);

    lblListHeader.setTypeface(null, Typeface.BOLD);

    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() 
{
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
{
    return true;
}

}

switch (groupPosition)
            {

            case 0:

                final LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = inflater.inflate(R.layout.child_row, null);

                rg=(RadioGroup)convertView.findViewById(R.id.cradioGroup1);

                cbtn=(Button)convertView.findViewById(R.id.continebutton1);

                cbtn.setOnClickListener(new OnClickListener() 
                {

                    @Override
                    public void onClick(View v) 
                    {
                        // TODO Auto-generated method stub
                        //android:groupIndicator="@android:color/transparent"
                        //.setVisibility( View.INVISIBLE );

                    }
                });

                rid=rg.getCheckedRadioButtonId();

                rbutton=(RadioButton)convertView.findViewById(rid);

                System.out.println("<------Checked account in expandable adapter----->"+rbutton.getText());

                break;

            case 1:

                LayoutInflater inflater1 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = inflater1.inflate(R.layout.child_row1, null);

                //TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);

                //tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);

                user=(EditText)convertView.findViewById(R.id.editText1);

                email=(EditText)convertView.findViewById(R.id.editText2);

                pass=(EditText)convertView.findViewById(R.id.editText3);

                repass=(EditText)convertView.findViewById(R.id.editText4);

                Button reg;

                reg=(Button)convertView.findViewById(R.id.button1);

                reg.setOnClickListener(new OnClickListener() 
                {

                    @Override
                    public void onClick(View v) 
                    {
                        // TODO Auto-generated method stub
                    new Register().execute();   
                    }
                });
                break; 

            case 2:

                LayoutInflater inflater2 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = inflater2.inflate(R.layout.child_row2, null);

                country=(EditText)convertView.findViewById(R.id.editcoun);

                fname=(EditText)convertView.findViewById(R.id.editfn);

                lname=(EditText)convertView.findViewById(R.id.editln);

                    break;

}

3 个答案:

答案 0 :(得分:1)

cbtn.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub
               rid=rg.getCheckedRadioButtonId();

               rbutton1=(RadioButton)convertView.findViewById(R.id.rid1);
                  rbutton2=(RadioButton)convertView.findViewById(R.id.rid2); 
                radiomain = (RadioButton) findViewById(rid);
               if(radiomain.getText().toString().equals(rbutton1.gettext().toString)
                     exp1.setVisibility(View.INVISIBLE);
               else
                    exp2.setVisibility(View.INVISIBLE);


                }
            });

在执行此操作之前,请阅读单选按钮教程,以便您可以理解。您必须要做的是findviewbyid()两个单选按钮(rbutton1,rbutton2)中的每一个。然后我要检查哪个单选按钮被选中了.equals()并隐藏相应的可扩展列表views.exp1是第一个的id,exp2是第二个的id。

答案 1 :(得分:0)

您可以轻松隐藏可扩展列表视图,

expandablelist.setvisibility(View.GONE); // in this scenario view are gone complatelly from layout.
expandablelist.setvisibility(View.INVISIBLE); // in this scenario view are cover space in layout, but not visible in layout.

您可以通过此方法显示/隐藏所有Android组件。

答案 2 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.mylistviewlayout);

  tb = (ToggleButton) findViewById(R.id.toggleButton1);
  lv = (ListView) findViewById(R.id.listView1);

  //You could set ListView Adapter here.

  tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

          if(isChecked)
          {
              lv.setVisibility(View.VISIBLE);
          }
          else
          {
              //lv.setVisibility(View.GONE);
              lv.setVisibility(View.INVISIBLE);
          }
      }
  });
}

而不是切换按钮使用您的单选按钮并检查它是否被点击。