在我的应用程序中实现了导航抽屉。一切正常,只是无法从抽屉物品中触发任何活动
这是我的 MainActivity.java
public class MainActivity extends SherlockFragmentActivity implements AdapterView.OnItemClickListener, View.OnClickListener, Animation.AnimationListener {
private Animation slideRight, slideLeft;
private FrameLayout myLayout, myLayoutt, myLayouth, myLayoutf, mylayoutfi;
private Button showView, hideView;
private Animation animFadeIn, animFadeOut;
// Declare Variables
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment fragment1 = new Fragment1();
private CharSequence mDrawerTitle;
private CharSequence mTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from drawer_main.xml
setContentView(R.layout.drawer_main);
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
animFadeIn.setAnimationListener(this);
animFadeOut.setAnimationListener(this);
myLayout = (FrameLayout) findViewById(R.id.elo);
myLayoutt = (FrameLayout) findViewById(R.id.elf);
myLayouth = (FrameLayout) findViewById(R.id.elg);
myLayoutf = (FrameLayout) findViewById(R.id.elco);
mylayoutfi = (FrameLayout) findViewById(R.id.ele);
slideRight = AnimationUtils.loadAnimation(this, R.anim.slide_right);
slideLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left);
slideLeft.setAnimationListener(animationListener);
myLayoutt.startAnimation(slideLeft);
myLayoutt.setVisibility(View.VISIBLE);
myLayout.startAnimation(slideRight);
myLayout.setVisibility(View.VISIBLE);
myLayoutf.startAnimation(slideLeft);
myLayoutf.setVisibility(View.VISIBLE);
myLayouth.startAnimation(slideRight);
myLayouth.setVisibility(View.VISIBLE);
mylayoutfi.startAnimation(slideRight);
mylayoutfi.setVisibility(View.VISIBLE);
// Get the Title
mTitle = mDrawerTitle = getTitle();
// Generate title
title = new String[] { "News", "Videos", "Photos" };
};
// Generate icon
icon = new int[] { R.drawable.action_about, R.drawable.action_about,
R.drawable.collections_cloud};
// Locate DrawerLayout in drawer_main.xml
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Locate ListView in drawer_main.xml
mDrawerList = (ListView) findViewById(R.id.listview_drawer);
// Set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Pass string arrays to MenuListAdapter
mMenuAdapter = new MenuListAdapter(MainActivity.this, title,
icon);
// Set the MenuListAdapter to the ListView
mDrawerList.setAdapter(mMenuAdapter);
// Capture listview menu item click
mDrawerList.setOnItemClickListener(MainActivity.this);
// Enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
// Set the title on the action when drawer open
getSupportActionBar().setTitle(mDrawerTitle);
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
// ListView click listener in the navigation drawer
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
selectItem(position);
mDrawerLayout.closeDrawer(mDrawerList);
switch(position){
case 0 :{
Toast.makeText(getApplicationContext(), "Please Mention Quantity",
Toast.LENGTH_SHORT).show();
return;
}
case 1:{
Intent t2l = new Intent(MainActivity.this, Fragment2.class);
startActivity(t2l);
return;
}
case 2 :{
Toast.makeText(getApplicationContext(), "Second Item",
Toast.LENGTH_SHORT).show();
break;
}
}
}
private void selectItem(int position) {
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
private Animation.AnimationListener animationListener = new Animation.AnimationListener(){
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}};
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// if animation is fade out hide them after completing animation
if (animation == animFadeOut) {
}
if(animation == animFadeIn){
// do something after fade in completed
// set visibility of fade in element
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
这是 drawer_main.xml 文件
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="1dp" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fff">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fff">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/clo"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<RelativeLayout
android:id="@+id/newsl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cloth">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/newsimone"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_toRightOf="@id/newsimone"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/newsl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/newsl">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/newsimtwo"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_toRightOf="@id/newsimtwo"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/catl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/newsl2"
android:layout_marginBottom="5dp">
<View android:id="@+id/strut"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_margin="1dp"/>
<View android:id="@+id/strut2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_margin="1dp"/>
<FrameLayout android:id="@+id/elo"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/eloc"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/al"
android:layout_margin="2dp"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elf"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/eloc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elfc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/bl"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/ele"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/elfoodc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elec"
android:layout_margin="2dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="@color/bg"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elg"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/elec"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elgc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/bla"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elco"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/eleduc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elcoc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/r"></ExpandableListView>
</FrameLayout>
</RelativeLayout>
<TextView
android:layout_below="@id/catl"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/dg"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textColor="@color/wh"
android:textStyle="bold"
android:textSize="30sp"/>
</RelativeLayout></ScrollView>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout></LinearLayout>
我已尝试过以前相关问题的所有建议,但似乎没有任何效果。任何建议都会有很大帮助。提前致谢
答案 0 :(得分:0)
问题出在XML文件中。希望这个答案有助于某人
这是我更新的 drawer_main.xml 文件
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fff">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fff">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/clo"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<RelativeLayout
android:id="@+id/newsl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cloth">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/newsimone"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_toRightOf="@id/newsimone"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/newsl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/newsl">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/newsimtwo"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:background="@drawable/job" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_toRightOf="@id/newsimtwo"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/catl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/newsl2"
android:layout_marginBottom="5dp">
<View android:id="@+id/strut"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_margin="1dp"/>
<View android:id="@+id/strut2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_margin="1dp"/>
<FrameLayout android:id="@+id/elo"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/eloc"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/al"
android:layout_margin="2dp"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elf"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/eloc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elfc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/bl"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/ele"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/elfoodc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elec"
android:layout_margin="2dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="@color/bg"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elg"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/elec"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elgc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/bla"></ExpandableListView>
</FrameLayout>
<FrameLayout android:id="@+id/elco"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:layout_below="@id/eleduc"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/cell_shape" android:visibility="gone">
<ExpandableListView
android:id="@+id/elcoc"
android:layout_margin="2dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_toRightOf="@id/strut"
android:layout_toLeftOf="@id/strut2"
android:background="@color/r"></ExpandableListView>
</FrameLayout>
</RelativeLayout>
<TextView
android:layout_below="@id/catl"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/dg"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textColor="@color/wh"
android:textStyle="bold"
android:textSize="30sp"/>
</RelativeLayout></ScrollView>
</LinearLayout>
<ListView
android:id="@+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
只需绑定linearlayout中的所有其他内容,并将listview置于线性布局下方。它有效,无需在 MainActivity.java 文件中进行任何更改。希望它有所帮助