扩展列表未展开

时间:2015-09-16 09:55:47

标签: android expandablelistview

我有一个可扩展的列表视图。我已经放了一些虚拟数据,并且evrything似乎没问题,但是当我运行应用程序时,所有组都处于折叠模式,当我点击它们时它们不会生效。还有一个问题是,只有点击列表而不是扩展,才能加载groupView上的图片。我希望图像默认加载。

ContactsFragment.java

public class ContactsFragment extends Fragment implements ExpandableListView.OnChildClickListener {
@SuppressWarnings("unused")

private ProgressDialog pDialog;
HashMap<String, String> listDataHeader;
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();


ArrayList<HashMap<String, String>> inboxList;

// products JSONArray
JSONArray inbox = null;

// Inbox JSON url
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView lv;
ExpandableListAdapter adapter;
Button button1,buuton2;
/*HashMap<String, List<String>> listDataChild;*/

public static final String TAG = LibraryPagerAdapter.class.getSimpleName();
protected JSONArray mTasksData; 
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
private ArrayList<Object> childItems = new ArrayList<Object>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = 
                new StrictMode.ThreadPolicy.Builder().permitAll().build();      
        StrictMode.setThreadPolicy(policy);
    }
    View rootView = inflater.inflate(R.layout.main, container, false);

    expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);


    bindListView();
    setChildData();
    /*System.out.println("Oncreate view songslist"+songsList);
    System.out.println("Oncraeteview childitems"+childItems);*/
    listAdapter = new ExpandableListAdapter(getActivity().getApplicationContext(), songsList, childItems);

    // setting list adapter
    expListView.setAdapter(listAdapter);
    expListView.setOnChildClickListener(this);
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
             Log.d("onGroupClick:", "worked");
                parent.expandGroup(groupPosition);

            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            System.out.println("inside group expand");
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            System.out.println("inside group collapse");

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            System.out.println("inside child click");
            return false;
        }
    });
    return rootView;

}

private void setChildData() {
    System.out.println("I m in setChildData");
    // TODO Auto-generated method stub
    ArrayList<String> child = new ArrayList<String>();
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    for (int i = 0; i < nl.getLength(); i++){
        child.add("Audio");
        child.add("Video");


        childItems.add(child);

    }
    /*System.out.println(childItems);*/
}

public void bindListView() {
    System.out.println("Inside bindListView");

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap

        listDataHeader = new HashMap<String, String>();

        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        listDataHeader.put(KEY_ID, parser.getValue(e, KEY_ID));
        listDataHeader.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        listDataHeader.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        listDataHeader.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        listDataHeader.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));



        // adding HashList to ArrayList
        songsList.add(listDataHeader);


    }

    System.out.println(songsList);
    adapter = new ExpandableListAdapter(getActivity(), songsList, R.layout.inbox_list_item,
            new String[] {  KEY_THUMB_URL, KEY_TITLE,KEY_ARTIST,KEY_DURATION },
            new int[] { R.id.list_image, R.id.title,R.id.artist,R.id.duration});   
    System.out.println("Before setting adapter"+adapter);
/*  expListView.setAdapter(adapter); 
    System.out.println("after"+adapter);*/


}

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    // TODO Auto-generated method stub
    return false;
}


/*class LoadInbox extends AsyncTask<String, String, String> {
    protected String doInBackground(String... args) {
        // Building Parameters

    }

 *//**
 * After completing background task Dismiss the progress dialog
 * **//*
    protected void onPostExecute(String file_url) {

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                try{
                    // dismiss the dialog after getting all proKEY_THUMB_URLducts
                    adapter = new LazyAdapter(getActivity(), songsList, R.layout.inbox_list_item,
                            new String[] {  KEY_THUMB_URL, KEY_TITLE,KEY_ARTIST,KEY_DURATION },
                            new int[] { R.id.list_image, R.id.title,R.id.artist,R.id.duration});   

                    lv.setAdapter(adapter); 
                    System.out.println(adapter);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

    }

    protected JSONArray doInBackground(Object... params) {
        // TODO Auto-generated method stub
        return null;
    }*/

}

ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private int inbox;

String[] arr;
int[] is1;
private Context context;
private Activity activity;
private ArrayList<HashMap<String, String>> songsList; // header titles
// child data in format of header title, child title
private ArrayList<Object> childItems;
private ArrayList<Object> childtems1;
private LayoutInflater inflater1;
private ArrayList<String> parentItems, child;

private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public ExpandableListAdapter(FragmentActivity activity2,
        ArrayList<HashMap<String, String>> songsList, int inboxListItem,
        String[] strings, int[] is) {
    System.out.println("inside constructor");

    activity = activity2;
    data=songsList;
    inbox=inboxListItem;
    arr=strings;
    is1=is;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}



public ExpandableListAdapter(Context context, ArrayList<HashMap<String, String>> songsList,
        ArrayList<Object> childItems) {
    System.out.println("inside second constructor");
    this.context=context;
    data = songsList;
    this.childItems = childItems;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(context.getApplicationContext());
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return null;
}

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

@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    System.out.println("Inside child view");
    child = (ArrayList<String>) childtems1.get(groupPosition);

    TextView textView = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    // get the textView reference and set the value
    textView = (TextView) convertView.findViewById(R.id.textViewChild);
    textView.setText(child.get(childPosition));

    // set the ClickListener to handle the click event on child item
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(activity, child.get(childPosition),
                    Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
    /*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);
    }

    Button ListChild = (Button) convertView
            .findViewById(R.id.lblListItem);
    Button ListChild1 = (Button) convertView
            .findViewById(R.id.lblListItem1);

    ListChild.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            System.out.println("Inside onclick");

        }
    });
   ListChild1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            System.out.println("Inside onclick");

        }
    });
    return convertView;*/
}

@SuppressWarnings("unchecked")

/*public int getChildrenCount(int groupPosition) {
    System.out.println(childItems.size());
    return childItems.size();
}*/

@Override
public Object getGroup(int groupPosition) {
    return null;
}

@Override
public int getGroupCount() {
    System.out.println("The group count is"+data.size());
    return data.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);
    System.out.println("Inside group view");
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(groupPosition);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
    /*  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 void onGroupCollapsed(int groupPosition) 
{
    Toast.makeText( context, "Group collapse)",
            Toast.LENGTH_LONG).show();
    System.out.println("group collapsed");
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition)
{
    Toast.makeText( context, "Group expanded)",
            Toast.LENGTH_LONG).show();
    System.out.println("Group expanded");
    super.onGroupExpanded(groupPosition);
}
@Override
public boolean hasStableIds() {
    return false;
}

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



@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 0;
}

 }

list_row.java

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<!-- ListRow Left sied Thumbnail image -->

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:background="@drawable/image_bg"
    android:padding="3dip" >

    <ImageView
          android:focusable="false"
          android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/rihanna" />
</LinearLayout>

<!-- Title Of Song -->

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
      android:focusable="false"
      android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:textSize="15dip"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- Artist Name -->

<TextView
    android:id="@+id/artist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
      android:focusable="false"
      android:text="Just gona stand there and ..."
    android:textColor="#343434"
    android:textSize="10dip" />

<!-- Rightend Duration -->

<TextView
    android:id="@+id/duration"
     android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:layout_marginRight="5dip"
    android:gravity="right"
    android:text="5:45"
    android:textColor="#10bcc9"
    android:textSize="10dip"
    android:textStyle="bold" />

<!-- Rightend Arrow -->

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:focusable="false"
    android:layout_centerVertical="true"
    android:src="@drawable/arrow" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

请参考这个与urs类似的问题,关于可关注的项目,

expandablelistview is not getting expanded when checkbox is added in parent xml

并尝试将android:descendantFocusability =&#34; blocksDescendants&#34;在你的相对布局