有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可扩展导航抽屉未移动

我对动作酒吧夏洛克和导航抽屉很陌生。我创建了一个简单的导航抽屉。默认情况下,每当应用程序启动时,抽屉都会打开,与之交互后,无法隐藏抽屉如果我想让用户在点击屏幕左上角时打开导航抽屉,如果用户在里面选择as选项,或者再次点击屏幕左上角的应用图标,导航抽屉应该隐藏,我该怎么办

主要活动

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

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

        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();

                }
            });

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

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(
                            getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                            listDataHeader.get(groupPosition)).get(
                                            childPosition), Toast.LENGTH_SHORT)
                            .show();
                    return false;
                }
            });
    }

    @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;
    }


    /*
     * 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);
    }

}

ExpandableListAdapter类:

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

}

主布局:

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

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

     <!-- 安卓: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
        安卓:id="@+id/left_drawer"
        安卓:layout_width="240dp"
        安卓:layout_height="match_parent"
        安卓:layout_gravity="start"
        安卓:choiceMode="singleChoice"
        安卓:divider="#999999"
        安卓:dividerHeight="1dp"
        安卓:background="#ffffff"/>

</RelativeLayout>

列表组。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="vertical"
    安卓:padding="8dp"
    安卓:background="#CC7A00">


    <TextView
        安卓:id="@+id/lblListHeader"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:paddingLeft="?安卓:attr/expandableListPreferredItemPaddingLeft"
        安卓:textSize="17dp"
        安卓:textColor="#ffffff" />

</LinearLayout>

列表项。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="55dip"
    安卓:orientation="vertical" 
    安卓:background="#FFCC80">

    <TextView
        安卓:id="@+id/lblListItem"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:textSize="17dip"
        安卓:paddingTop="5dp"
        安卓:paddingBottom="5dp"
        安卓:paddingLeft="?安卓:attr/expandableListPreferredChildPaddingLeft" 
        安卓:textColor="#336699"/>

</LinearLayout>

输出

Non expanded

Expanded

请告诉我隐藏导航栏的方法

如果你想看到输出,你只需要在一个新项目中复制粘贴代码


共 (1) 个答案

  1. # 1 楼答案

    将布局更改为:

    <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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>
    

    为了让它在菜单图标上工作,你可以参考源代码here

    代码片段:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            if(mDrawerLayout.isDrawerOpen(mFrameHoldingList)) {
                mDrawerLayout.closeDrawer(mFrameHoldingList);
            }else {
                mDrawerLayout.openDrawer(mFrameHoldingList);
            }
            break;
    
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    

    在您的案例中,mFrameHoldingList将是可扩展的listview

    编辑: 对于按钮点击,如果你的应用程序有一个操作栏,那么

    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);
    
        ->> Add these lines
        mActionBar = getActionBar();
        mActionBar.setHomeButtonEnabled(true);
    mActionBar.setDisplayHomeAsUpEnabled(false);
    

    *编辑:使子对象可选择*

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }