【问题标题】:How can I add item clicks dynamically on navigation drawer如何在导航抽屉上动态添加项目点击
【发布时间】:2017-02-22 20:57:57
【问题描述】:

我在向导航抽屉动态添加项目时遇到问题,我已经解决了像这样添加项目的部分

`for(int i = 0; i<lista.size(); i++){
            SubMenu menuGroup = menu.addSubMenu(Menu.NONE, i, Menu.NONE, lista.get(i));
            for(int j = 0; j<5; j++){
                menuGroup.add(item + j);
            }`

问题出在这里:

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_manage) {
        // Handle the camera action
        //here comes the action for the first item

    } else if (id == R.id.item_2) {
        //here comes the action for item 2 and so on

所以事情是,一旦我动态地创建了项目(已经这样做了),我如何添加项目点击(已经创建的项目的操作)。 我尝试了一个 for 循环,但因为它是一个 if - else if 条件,所以我不能使用 for 循环。 谁能帮帮我?

【问题讨论】:

    标签: android navigation-drawer items


    【解决方案1】:

    我以前遇到过同样的问题,用这些方法解决了

    1. 用 bundle 及其布局创建一个片段
    2. 在 MainActivity.java 中创建方法
    3. 在 onNavigationItemSelected 中调用该方法

    content_main.xml 中添加带有 id frag_layout 的 FrameLayout 后,我​​执行了以下步骤:

    FragDetail.java

    public class FragDetail extends Fragment {
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.frag_detail, container, false);
    
        Bundle bundle = getArguments();
    
        if(bundle != null) {
    
            TextView mText = (TextView) view.findViewById(R.id.txt_detail);
            mText.setText(bundle.getString("drawer_title"));
        }
    
        return view;
    }}
    

    frag_detail.xml

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:text="detail clicked"
        android:layout_width="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/txt_detail" /></RelativeLayout>
    

    MainActivity.java 中的方法

    public FragDetail getFragment(String desc) {
    
        FragDetail frag = new FragDetail();
    
        Bundle bundle = new Bundle();
        bundle.putString("drawer_title", desc);
    
        frag.setArguments(bundle);
    
        return frag;
    }
    

    调用方法

       public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
    
        String nmClient = (String) item.getTitle();
    
        Fragment frag = getFragment(nmClient);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    
        ft.replace(R.id.frag_layout, frag);
        ft.commit();
    
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

    我实际上从link找到了解决方案

    【讨论】:

    • 谢谢老兄,我终于用onNavigationItemSelected做到了,因为所有的项目都做同样的事情,唯一的区别是名字,没有我想象的那么难,但是你的例子会出现方便另一个项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多