隐藏指示符当组在可扩展列表视图中没有子项时,对于组?

时间:2014-02-01 06:37:39

标签: android expandablelistview indicator

我希望在群组没有孩子时隐藏群组指示符我放下代码 我使用自定义图像作为指标可扩展列表视图 如果组没有孩子而不是指示器隐藏如何隐藏此

  

Home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

   <RelativeLayout 
       android:id="@+id/header"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent"
       android:layout_alignParentTop="true"
       >

       <ImageView
        android:id="@+id/imagemenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu"
        android:layout_alignParentLeft="true"
         />

      <RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/imagemenu"
          android:background="@color/ColorGray" >


            <ImageView
                android:id="@+id/imagemenu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/bridgelogo"
                android:layout_alignParentLeft="true"
            />



      </RelativeLayout>

   </RelativeLayout>


      <ExpandableListView
            android:id="@+id/expandablelistview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header"
            android:layout_above="@+id/footer"
            android:indicatorRight="45sp"
            android:divider="@android:color/white"
            android:dividerHeight="3sp"
            android:childDivider="@android:color/white"
            android:groupIndicator="@drawable/selector"
        >

      </ExpandableListView>



   <ImageView 
       android:id="@+id/footer"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/footer"
       android:layout_alignParentBottom="true"

       />


</RelativeLayout>
  

Selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/empty" android:state_empty="true"/>
   <item android:drawable="@drawable/expand" android:state_expanded="true"/>
   <item android:drawable="@drawable/empty"></item>   
</selector>
  

HomeListActivity.java

package com.bridge.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class HomeListActivity extends Activity 
{

     WebView webview;    

     ExpandableListAdapter listAdapter;
     ExpandableListView expListView;

     ArrayList<HashMap<String,String>> headerarraylist;
     ArrayList<HashMap<String,String>> childarraylist;

     List<String> listDataHeader;
     HashMap<String, List<String>> listDataChild;    

    List<String> submenu0 = new ArrayList<String>();
    List<String> submenu1 = new ArrayList<String>();
    List<String> submenu2 = new ArrayList<String>();
    List<String> submenu3 = new ArrayList<String>();
    List<String> submenu4 = new ArrayList<String>();
    List<String> submenu5 = new ArrayList<String>();
    List<String> submenu6 = new ArrayList<String>();
    List<String> submenu7 = new ArrayList<String>();
    List<String> submenu8 = new ArrayList<String>();
    List<String> submenu9 = new ArrayList<String>();
    List<String> submenu10 = new ArrayList<String>();
    List<String> submenu11= new ArrayList<String>();
    List<String> submenu12= new ArrayList<String>();
    List<String> submenu13= new ArrayList<String>();

    int lastExpandedGroupPosition = 0; 

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homelist);


        expListView = (ExpandableListView) findViewById(R.id.expandablelistview);


        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        headerarraylist=new ArrayList<HashMap<String,String>>();
        childarraylist=new ArrayList<HashMap<String,String>>();


        expListView.setGroupIndicator(null);

          // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() 
            {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) 
                {

                    return false;

                }
            });

            // Listview Group expanded listener
            expListView.setOnGroupExpandListener(new OnGroupExpandListener() 
            {
                @Override
                public void onGroupExpand(int groupPosition) 
                {

                    if (groupPosition != lastExpandedGroupPosition) 
                    {
                        expListView.collapseGroup(lastExpandedGroupPosition);

                    }

                    lastExpandedGroupPosition = groupPosition;

                    Toast.makeText(getApplicationContext(),listDataHeader.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show();

                }

            });

            // Listview Group collasped listener
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() 
            {
                @Override
                public void onGroupCollapse(int groupPosition) 
                {

                    Toast.makeText(getApplicationContext(),listDataHeader.get(groupPosition) + " Collapsed",Toast.LENGTH_SHORT).show();



                }
            });

            // Listview on child click listener
            expListView.setOnChildClickListener(new OnChildClickListener() 
            {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) 
                {

                    Toast.makeText(getApplicationContext(),listDataHeader.get(groupPosition)+ " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();

                    String child=listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);

                    for(int i=0;i<childarraylist.size();i++)
                    {


                        String uid=childarraylist.get(i).get(ClassVariable.SUBMENU.UID).toString();
                        String title=childarraylist.get(i).get(ClassVariable.SUBMENU.TITLE).toString();
                        String pid=childarraylist.get(i).get(ClassVariable.SUBMENU.PID).toString();

                        if(child.equalsIgnoreCase(title))
                        {   

                            Intent intent = new Intent(HomeListActivity.this,HomeListClickActivity.class);
                            intent.putExtra("title", title);
                            intent.putExtra("uid", uid);
                            intent.putExtra("pid", pid);
                            startActivity(intent);

                            Log.e("Child Value uid","--------->"+uid);
                            Log.e("Child Value pid ","--------->"+pid);
                            Log.e("Child Value title","--------->"+title);
                        }

                    }

                    Log.e("Child Value","--------->"+child);

                    return false;
                }
            });



        if (CheckConnection.getInstance(HomeListActivity.this).isOnline(HomeListActivity.this)) 
        {

            new HomeListAsynctask().execute("");

        }
        else 
        {

        }


    }


    // Asynctask for getting the home data from url
    public class HomeListAsynctask extends AsyncTask<String, List<String>,String>
    {
        String detail;

        @Override
        protected void onPreExecute() 
        {



        }

        @Override
        protected String doInBackground(String... params) 
        {
            try 
            {
                JsonParser jparser = new JsonParser();              
                String menuurl="http://www.bridge.co.at/webservices/services.php?method=menu";              
                String menudata=jparser.getdata(menuurl);

                JSONObject jobject = new JSONObject(menudata);
                JSONArray jarray =jobject.getJSONArray(ClassVariable.MENU.MENU);

                listDataHeader.clear();
                for(int i=0;i<jarray.length();i++)
                {
                    listDataHeader.add(jarray.getJSONObject(i).get(ClassVariable.MENU.TITLE).toString());

                    // For Getting Main Menu in ArrayList

                    String title=jarray.getJSONObject(i).get(ClassVariable.MENU.TITLE).toString();
                    String uid=jarray.getJSONObject(i).get(ClassVariable.MENU.UID).toString();
                    String pid=jarray.getJSONObject(i).get(ClassVariable.MENU.PID).toString();

                    HashMap<String,String> map = new HashMap<String, String>();
                    map.put(ClassVariable.MENU.TITLE, title);
                    map.put(ClassVariable.MENU.UID, uid);
                    map.put(ClassVariable.MENU.PID, pid);
                    headerarraylist.add(map);



                    // For Getting SubMenu in ArrayList

                    String submenuurl="http://www.bridge.co.at/webservices/services.php?method=submenu&uid="+uid;
                    String submenudata=jparser.getdata(submenuurl);

                    JSONObject jobjectsubmenu = new JSONObject(submenudata);
                    JSONArray jarraysubmenu =jobjectsubmenu.getJSONArray(ClassVariable.SUBMENU.SUBMENU);

                    for(int j=0;j<jarraysubmenu.length();j++)
                    {
                        String submenutitle=jarraysubmenu.getJSONObject(j).get(ClassVariable.SUBMENU.TITLE).toString();
                        String submenuuid=jarraysubmenu.getJSONObject(j).get(ClassVariable.SUBMENU.UID).toString();
                        String submenupid=jarraysubmenu.getJSONObject(j).get(ClassVariable.SUBMENU.PID).toString();

                        if(i==0)
                        {
                            submenu0.add(submenutitle);
                        }
                        if(i==1)
                        {
                            submenu1.add(submenutitle);
                        }
                        if(i==2)
                        {
                            submenu2.add(submenutitle);
                        }
                        if(i==3)
                        {
                            submenu3.add(submenutitle);
                        }
                        if(i==4)
                        {
                            submenu4.add(submenutitle);
                        }
                        if(i==5)
                        {
                            submenu5.add(submenutitle);
                        }
                        if(i==6)
                        {
                            submenu6.add(submenutitle);
                        }
                        if(i==7)
                        {
                            submenu7.add(submenutitle);
                        }
                        if(i==8)
                        {
                            submenu8.add(submenutitle);
                        }
                        if(i==9)
                        {
                            submenu9.add(submenutitle);
                        }
                        if(i==10)
                        {
                            submenu10.add(submenutitle);
                        }
                        if(i==11)
                        {
                            submenu11.add(submenutitle);
                        }
                        if(i==12)
                        {
                            submenu12.add(submenutitle);
                        }                       


                        HashMap<String,String> map1 = new HashMap<String, String>();
                        map1.put(ClassVariable.SUBMENU.TITLE, submenutitle);
                        map1.put(ClassVariable.SUBMENU.UID, submenuuid);
                        map1.put(ClassVariable.SUBMENU.PID, submenupid);
                        childarraylist.add(map1);

                    }

                }

                Log.e("Child Array List","----->"+childarraylist);

                listDataChild.put(listDataHeader.get(0), submenu0); // Header, Child data
                listDataChild.put(listDataHeader.get(1), submenu1);
                listDataChild.put(listDataHeader.get(2), submenu2);
                listDataChild.put(listDataHeader.get(3), submenu3); // Header, Child data
                listDataChild.put(listDataHeader.get(4), submenu4);
                listDataChild.put(listDataHeader.get(5), submenu5);
                listDataChild.put(listDataHeader.get(6), submenu6); // Header, Child data
                listDataChild.put(listDataHeader.get(7), submenu7);
                listDataChild.put(listDataHeader.get(8), submenu8);
                listDataChild.put(listDataHeader.get(9), submenu9); // Header, Child data
                listDataChild.put(listDataHeader.get(10), submenu10);
                listDataChild.put(listDataHeader.get(11), submenu11);
                listDataChild.put(listDataHeader.get(12), submenu12); // Header, Child data


                Log.e("HeaderList","---->"+listDataHeader);
                Log.e("ChildList","---->"+listDataHeader);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();

            }


            return detail;
        }

        @Override
        protected void onPostExecute(String result) 
        {

            listAdapter = new ExpandableListAdapter(HomeListActivity.this, listDataHeader, listDataChild);
            expListView.setAdapter(listAdapter);

        }

    }

}
  

可扩展列表Adapter.java

package com.bridge.app;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter 
{
    private Context _context;
    private List<String> _listDataHeader; // header titles

    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) 
    {
        this._context = 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(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) 
    {

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

        if (convertView == null) 
        {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        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._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.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;
    }

}

1 个答案:

答案 0 :(得分:1)

定义自定义状态并将其设置在正确的代码位置。

How to add a custom button state