Java 可展开的listview默认选择

Java 可展开的listview默认选择,java,android,expandablelistview,default-value,navigation-drawer,Java,Android,Expandablelistview,Default Value,Navigation Drawer,我创建了一个导航抽屉,里面有一个可扩展的列表视图我希望可展开列表视图在活动打开时展开其中一个组并默认选择其中一个子组。用户可以根据自己的要求更改选择 我所创造的一切都不能满足我的要求。可扩展列表允许用户选择多个选项 主要活动: public class MainActivity extends Activity { DrawerLayout mDrawerLayout; ExpandableListView mDrawerList; ExpandableListAdapt

我创建了一个导航抽屉,里面有一个可扩展的列表视图我希望可展开列表视图在活动打开时展开其中一个组并默认选择其中一个子组。用户可以根据自己的要求更改选择

我所创造的一切都不能满足我的要求。可扩展列表允许用户选择多个选项

主要活动

public class MainActivity extends Activity {
    DrawerLayout mDrawerLayout;
    ExpandableListView mDrawerList;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    private ActionBar mActionBar;

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

        // Action bar with collapse icon for drawer menu===================
        mActionBar = getActionBar();
        mActionBar.setHomeButtonEnabled(true);
        mActionBar.setDisplayHomeAsUpEnabled(false);
        // Action bar with collapse icon for drawer menu===================

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);

        // preparing list data
        prepareListData();

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

        // setting list adapter
        mDrawerList.setAdapter(listAdapter);

        // Listview Group click listener
        mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // // Listview Group expanded listener
        // mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
        //
        // @Override
        // public void onGroupExpand(int groupPosition) {
        // Toast.makeText(getApplicationContext(),
        // listDataHeader.get(groupPosition) + " Expanded",
        // Toast.LENGTH_SHORT).show();
        // }
        // });
        //
        // // Listview Group collasped listener
        // mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener()
        // {
        //
        // @Override
        // public void onGroupCollapse(int groupPosition) {
        // Toast.makeText(getApplicationContext(),
        // listDataHeader.get(groupPosition) + " Collapsed",
        // Toast.LENGTH_SHORT).show();
        //
        // }
        // });

        // mDrawerList.expandGroup(2);
        // mDrawerList.expandGroup(2, true);

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

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                mDrawerLayout.closeDrawer(mDrawerList);
                // TODO Auto-generated method stub
                // Toast.makeText(
                // getApplicationContext(),
                // listDataHeader.get(groupPosition)
                // + " : "
                // + listDataChild.get(
                // listDataHeader.get(groupPosition)).get(
                // childPosition), Toast.LENGTH_SHORT)
                // .show();
                 v.setBackgroundColor(Color.BLUE);

                makeAToast("Group: " + groupPosition + " Child: "
                        + childPosition);
                return false;
            }
        });

        // To collapse previously opened group===========
        mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
            int previousItem = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousItem)
                    mDrawerList.collapseGroup(previousItem);
                previousItem = groupPosition;
            }
        });
        // To collapse previously opened group===========
    }

    @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) {
        switch (item.getItemId()) {
        case android.R.id.home:
            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

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

        // Adding header data
        listDataHeader.add("Today");

        // Adding child data
        List<String> today = new ArrayList<String>();
        today.add("Tanushree Guha");
        today.add("Prasenjit Roy");
        today.add("Pan Singh Tomar");
        today.add("Milka Singh");
        today.add("Rohit Ramanujan");
        today.add("Ramesh Bhatt");
        today.add("Debjani Brahma");

        listDataHeader.add("Tomorrow");
        List<String> tomorrow = new ArrayList<String>();
        tomorrow.add("Dipanjan Bhowmik");
        tomorrow.add("Sarmistha Sinha");
        tomorrow.add("Pranay Lalwani");
        tomorrow.add("Mohit Shaw");
        tomorrow.add("Lovelace Agarwal");
        tomorrow.add("Tanmay Banerjee");

        listDataHeader.add("Later");
        List<String> later = new ArrayList<String>();
        later.add("Yusuf Khan");
        later.add("Jitendar Sharma");
        later.add("Debashree Roy");
        later.add("Mainak Ghosh");
        later.add("Olivia Gomes");

        listDataChild.put(listDataHeader.get(0), today); // Header, Child data
        listDataChild.put(listDataHeader.get(1), tomorrow);
        listDataChild.put(listDataHeader.get(2), later);
    }

    // method to show toast message
    public void makeAToast(String str) {
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
公共类MainActivity扩展活动{
抽屉布局mDrawerLayout;
可扩展列表视图mDrawerList;
可扩展列表适配器;
列表列表数据头;
HashMapListDataChild;
私人行动吧;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//带折叠图标的抽屉菜单操作栏===================
mActionBar=getActionBar();
mActionBar.setHomeButtonEnabled(真);
mActionBar.setDisplayHomeAsupaned(false);
//带折叠图标的抽屉菜单操作栏===================
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ExpandableListView)findViewById(R.id.left\u抽屉);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(此,listDataHeader,
listDataChild);
//设置列表适配器
mDrawerList.setAdapter(listAdapter);
//Listview组单击侦听器
mDrawerList.setOnGroupClickListener(新OnGroupClickListener(){
@凌驾
公共布尔值onGroupClick(ExpandableListView父视图,视图v,
int groupPosition,长id){
//Toast.makeText(getApplicationContext(),
//“组单击”+listDataHeader.get(groupPosition),
//吐司。长度(短)。show();
返回false;
}
});
////Listview组扩展侦听器
//mDrawerList.setOnGroupExpandListener(新的OnGroupExpandListener()){
//
//@覆盖
//public void onGroupExpand(int groupPosition){
//Toast.makeText(getApplicationContext(),
//listDataHeader.get(groupPosition)+“Expanded”,
//吐司。长度(短)。show();
// }
// });
//
////Listview组压缩侦听器
//mDrawerList.setOnGroupCollapseListener(新的OnGroupCollapseListener()
// {
//
//@覆盖
//公共void-onGroupCollapse(int-groupPosition){
//Toast.makeText(getApplicationContext(),
//listDataHeader.get(groupPosition)+“折叠”,
//吐司。长度(短)。show();
//
// }
// });
//mDrawerList.expandGroup(2);
//mDrawerList.expandGroup(2,true);
//子单击侦听器上的Listview
setOnChildClickListener(新的OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
mDrawerLayout.closeDrawer(mDrawerList);
//TODO自动生成的方法存根
//Toast.makeText(
//getApplicationContext(),
//listDataHeader.get(groupPosition)
// + " : "
//+listDataChild.get(
//listDataHeader.get(groupPosition)).get(
//儿童位置),吐司。长度(短)
//.show();
v、 setBackgroundColor(颜色:蓝色);
makeAToast(“组:”+groupPosition+“子项:”
+儿童体位);
返回false;
}
});
//折叠以前打开的组===========
mDrawerList.setOnGroupExpandListener(新的OnGroupExpandListener()){
int-previousItem=-1;
@凌驾
public void onGroupExpand(int groupPosition){
if(groupPosition!=上一项)
mDrawerList.collapseGroup(上一项);
previousItem=组位置;
}
});
//折叠以前打开的组===========
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
if(mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
}否则{
mDrawerLayout.openDrawer(mDrawerList);
}
打破
违约:
打破
}
返回super.onOptionsItemSelected(项目);
}
/*
*准备列表数据
*/
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加标题数据
添加(“今天”);
//添加子数据
今天列表=新的ArrayList();
今天。添加(“Tanushree Guha”);
添加(“Prasenjit Roy”);
今天。加上(“潘辛格·托马尔”);
今天。添加(“米尔卡·辛格”);
添加(“Rohit Ramanujan”);
今天。添加(“Ramesh Bhatt”);
今天。添加(“德贾尼婆罗门”);
添加(“明天”);
明天列表=新的ArrayList();
添加(“Dipanjan Bhowmik”);
明天。添加(“萨米莎·辛哈”);
明天。加上(“普拉纳伊·拉尔瓦尼”);
明天。添加(“Mohit Shaw”);
明天。添加(“Lovelace Agarwal”);
明天。加上(“Tanmay Banerjee
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

 <!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     The drawer is given a fixed width in dp and extends the full height of
     the container. A solid background is used for contrast
     with the content view. -->
<ExpandableListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#999999"
    android:dividerHeight="1dp"
    android:background="#ffffff"/>
</android.support.v4.widget.DrawerLayout>

</RelativeLayout>
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;
    }

}
mExpandableListView.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(groupPosition, childPosition);