尝试在DrawerLayout中创建具有可扩展列表的应用程序

时间:2013-09-27 22:19:23

标签: java android expandablelistview drawerlayout

尝试在DrawerLayout中创建具有可展开列表的应用。我把DrawerLayout正常工作,只需从strings.xml中的数组中提取列表。

自从我添加ExpandableListAdapter以来它已经崩溃,或者即使它运行它也只是不让我做任何事情。

这是我的代码:

package com.alpha.omegaRSS;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;




public class MainActivity extends Activity {

private DrawerLayout navDrawerLayout;
private ActionBarDrawerToggle navDrawerToggle;
//private ListView navDrawerList;
private CharSequence navDrawerTitle;
private CharSequence navTitle;
private String[] leftItems;
private ExpandableListView navDrawerList;


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

    navTitle=navDrawerTitle=getTitle();
    //leftItems = getResources().getStringArray(R.array.left_drawer_array);
    navDrawerLayout=(DrawerLayout) findViewById(R.id.nav_layout);
    //navDrawerList= (ListView) findViewById(R.id.left_drawer);
    navDrawerList=(ExpandableListView)findViewById(R.id.left_drawer);


     ArrayList<Parent> arrayParents = new ArrayList<Parent>();
     ArrayList<String> arrayChildren = new ArrayList<String>();

        //here we set the parents and the children
        for (int i = 0; i < 10; i++){
            //for each "i" create a new Parent object to set the title and the children
            Parent parent = new Parent();
            parent.setTitle("Parent " + i);

            arrayChildren = new ArrayList<String>();
            for (int j = 0; j < 10; j++) {
                arrayChildren.add("Child " + j);
            }
            parent.setArrayChildren(arrayChildren);

            //in this array we add the Parent object. We will use the arrayParents at the setAdapter
            arrayParents.add(parent);
        }

     // set a custom shadow that overlays the main content when the drawer opens
    navDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    //set up the drawer's list view with items and click listener
   navDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, leftItems));
   // HERE I AM      navDrawerList.setAdapter(new CustomListAdapter(MainActivity.this, arrayParents));

    //navDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    navDrawerToggle=new ActionBarDrawerToggle(this, navDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)

    //called when the drawer is completely closed
    {
    public void onDrawerClosed(View mainView) {
        getActionBar().setTitle(navTitle);
        invalidateOptionsMenu();
    }

    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(navDrawerTitle);
        invalidateOptionsMenu();
    }
    };



    navDrawerLayout.setDrawerListener(navDrawerToggle);


    /*
    //swiping controller

     @Override
     public boolean dispatchTouchEvent(MotionEvent event) {
         super.dispatchTouchEvent(event);
         return gestureDetector.onTouchEvent(event);
     }


    SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
         @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {

                float sensitvity = 50;
                if ((e1.getX() - e2.getX()) > sensitvity) {
                    SwipeLeft();
                } else if ((e2.getX() - e1.getX()) > sensitvity) {
                    SwipeRight();
                }

                return true;
            }
    };


     GestureDetector gestureDetector = new GestureDetector(this,
                simpleOnGestureListener);



    */
}


/*  if(savedInstanceState==null){
        selectItem(0);
    }*/





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (navDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}



/*private void SwipeLeft() {

navDrawerLayout.closeDrawer(Gravity.RIGHT);

}


private void SwipeRight() { 

navDrawerLayout.openDrawer(Gravity.LEFT);

}


*/

}

这是我的ExpandedlistAdapter类。

 package com.alpha.omegaRSS;

 import java.util.ArrayList;

 import android.content.Context;
 import android.database.DataSetObserver;
 import android.view.LayoutInflater;
 Import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseExpandableListAdapter;
 import android.widget.Button;
 import android.widget.TextView;

 public class CustomListAdapter extends BaseExpandableListAdapter {


private LayoutInflater inflater;
private ArrayList<Parent> mParent;

public CustomListAdapter(Context context, ArrayList<Parent> parent){
    mParent = parent;
    inflater = LayoutInflater.from(context);
}


@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
    return mParent.size();
}

@Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
    return mParent.get(i).getArrayChildren().size();
}

@Override
//gets the title of each parent/group
public Object getGroup(int i) {
    return mParent.get(i).getTitle();
}

@Override
//gets the name of each item
public Object getChild(int i, int i1) {
    return mParent.get(i).getArrayChildren().get(i1);
}

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

@Override
public long getChildId(int i, int i1) {
    return i1;
}

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

@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    ViewHolder holder = new ViewHolder();
    holder.groupPosition = i;

    if (view == null) {
        view = inflater.inflate(R.id.left_drawer, viewGroup,false);
    }

    TextView textView = (TextView) view.findViewById(R.layout.list_item_parent);
    //"i" is the position of the parent/group in the list
    textView.setText(getGroup(i).toString());


    view.setTag(holder);

    //return the entire view
    return view;
}

@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

    ViewHolder holder = new ViewHolder();
    holder.childPosition = i1;
    holder.groupPosition = i;

    if (view == null) {
        view = inflater.inflate(R.layout.drawer_list_item, viewGroup,false);
    }

    TextView textView = (TextView) view.findViewById(R.id.child_item);
    //"i" is the position of the parent/group in the list and
    //"i1" is the position of the child
    textView.setText(mParent.get(i).getArrayChildren().get(i1));

    view.setTag(holder);

    //return the entire view
    return view;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    /* used to make the notifyDataSetChanged() method work */
    super.registerDataSetObserver(observer);
}


/*  @Override
    public void onClick(View view) {
        ViewHolder holder = (ViewHolder)view.getTag();
        if (view.getId() == holder.button.getId()){

           // DO SOMETHING
        }
    }*/



 protected class ViewHolder {
        protected int childPosition;
        protected int groupPosition;
        protected Button button;
    }
}

1 个答案:

答案 0 :(得分:0)

@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    ViewHolder holder = new ViewHolder();
    holder.groupPosition = i;

    if (view == null) {
        // You're inflating a id instead of a layout
        // you should inflate your R.layout.[drawer_group_list_item]
        view = inflater.inflate(R.id.left_drawer, viewGroup,false); 
    }

让我知道这是否解决了!