Java Android抽屉导航点击事件
我根据互联网上的一个教程制作了一个抽屉导航,我遵循了所有操作,但当我打开我的应用程序时,除了我的onclick事件外,所有操作都正常。我对安卓相当陌生(2周),我试着自己解决这个问题,但没有成功。我尝试了一个听众,但那个人没有给我任何积极的反馈 我如何制作一个点击事件,将我带到另一个活动 我的代码:Java Android抽屉导航点击事件,java,android,onclick,onclicklistener,navigation-drawer,Java,Android,Onclick,Onclicklistener,Navigation Drawer,我根据互联网上的一个教程制作了一个抽屉导航,我遵循了所有操作,但当我打开我的应用程序时,除了我的onclick事件外,所有操作都正常。我对安卓相当陌生(2周),我试着自己解决这个问题,但没有成功。我尝试了一个听众,但那个人没有给我任何积极的反馈 我如何制作一个点击事件,将我带到另一个活动 我的代码: public class LayoutOneActivity extends ActionBarActivity { String[] menu; DrawerLayout dLay
public class LayoutOneActivity extends ActionBarActivity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
Resources res = getResources();
String [] menu_items = res.getStringArray(R.array.menu_items); // String array where the menu items will be stored
menu = menu_items; // Variable for the menu items
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Looking for the id "drawer_layout" and apply as layout
dList = (ListView) findViewById(R.id.left_drawer); // Looking for the listview where the items will be stored
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);// Making a new adapter
dList.setAdapter(adapter);// Give the list-layout the variable "adapter" which is an adapter (obviously)
dList.setSelector(R.drawable.back);// Sets the colour of the list-layout
dList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
dLayout.closeDrawers();// The layout will be clossed when clicked outside the layout
Bundle args = new Bundle();// New bundle which will parse the data between various activities
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
公共类LayoutOnActivity扩展了ActionBarActivity{
字符串[]菜单;
抽屉式布局;
列表视图数据列表;
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
Resources res=getResources();
String[]menu_items=res.getStringArray(R.array.menu_items);//存储菜单项的字符串数组
menu=菜单项;//菜单项的变量
dLayout=(DrawerLayout)findViewById(R.id.drawer\u布局);//查找id“drawer\u布局”并作为布局应用
dList=(ListView)findViewById(R.id.left_drawer);//查找将存储项目的ListView
adapter=newarrayadapter(这是android.R.layout.simple_list_item_1,菜单);//制作一个新适配器
dList.setAdapter(adapter);//为列表布局提供变量“adapter”,它显然是一个适配器
dList.setSelector(R.drawable.back);//设置列表布局的颜色
dList.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView arg0,视图v,内部位置,长id)
{
dLayout.closeDrawers();//当在布局外部单击时,布局将被关闭
Bundle args=new Bundle();//将在各种活动之间解析数据的新Bundle
args.putString(“菜单”,菜单[位置]);
片段细节=新的DetailFragment();
detail.setArguments(args);
FragmentManager FragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,detail.commit();
}
});
}
布局图1
<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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity1"
android:text="@string/clickActivity1" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity2"
android:text="@string/clickActivity2" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity3"
android:text="@string/clickActivity3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity4"
android:text="@string/clickActivity4"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity5"
android:text="@string/clickActivity5"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity6"
android:text="@string/clickActivity6"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </FrameLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/> </android.support.v4.widget.DrawerLayout>
菜单\详细信息\片段
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#ffffff"
android:layout_gravity="center"
android:id="@+id/detail"/> </LinearLayout>
要在ClickListener上设置导航菜单,必须在活动上实现NavigationView.OnNavigationItemSelectedListener界面 在onCreate方法上,必须定义上下文活动的导航onClickListener,如下所示:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
然后必须重写该方法
@Override
public boolean onNavigationItemSelected(MenuItem item)
我写了一个如何实现以下方法的示例:
@Override
public boolean onNavigationItemSelected(MenuItem item){
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Urban");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_gallery) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Cosmos");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_slideshow) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Sink");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_share) {
ShopListFragment intent = new ShopListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, intent).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(START);
return true;
}
请描述什么不起作用以及如何起作用。我更新了您询问的信息