如何使用翻新使导航抽屉动态化?

时间:2019-07-01 12:22:09

标签: android dynamic retrofit navigation-drawer

后端的内部响应我有菜单和子菜单列表要显示在导航抽屉中,我尝试了一下,但我没有成功,这里是我的代码:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <ExpandableListView
        android:id="@+id/navigationmenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="192dp"
        android:background="@android:color/white">
    </ExpandableListView>
</com.google.android.material.navigation.NavigationView>

在.java类中:

public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
JSONObject jsonObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation_view);
    final ActionBar ab = getSupportActionBar();
    /* to set the menu icon image*/
    ab.setHomeAsUpIndicator(android.R.drawable.ic_menu_add);
    ab.setDisplayHomeAsUpEnabled(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    expandableList = (ExpandableListView) findViewById(R.id.navigationmenu);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    if (navigationView != null) {
        setupDrawerContent(navigationView);
    }

    prepareListData();
    mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
    expandableList.setAdapter(mMenuAdapter);
    expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {

            return false;
        }
    });
    expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

            return false;
        }
    });
}

private void prepareListData() {
    listDataHeader = new ArrayList<ExpandedMenuModel>();
    listDataChild = new HashMap<ExpandedMenuModel, List<String>>();

    ExpandedMenuModel item1 = new ExpandedMenuModel();
    item1.setIconName("heading1");
    item1.setIconImg(android.R.drawable.ic_delete);
    // Adding data header
    listDataHeader.add(item1);

    ExpandedMenuModel item2 = new ExpandedMenuModel();
    item2.setIconName("heading2");
    item2.setIconImg(android.R.drawable.ic_delete);
    listDataHeader.add(item2);

    ExpandedMenuModel item3 = new ExpandedMenuModel();
    item3.setIconName("heading3");
    item3.setIconImg(android.R.drawable.ic_delete);
    listDataHeader.add(item3);

    // Adding child data
    List<String> heading1 = new ArrayList<String>();
    heading1.add("Submenu of item 1");

    List<String> heading2 = new ArrayList<String>();
    heading2.add("Submenu of item 2");
    heading2.add("Submenu of item 2");
    heading2.add("Submenu of item 2");

    listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
    listDataChild.put(listDataHeader.get(1), heading2);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setupDrawerContent(NavigationView navigationView) {
    //revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    mDrawerLayout.closeDrawers();
                    return true;
                }
            });
}

 private void getMenuDrawer() {
    ApiInterface apiInterface = RetrofitManager.getClient().create(ApiInterface.class);
    Call<JsonObject> call = apiInterface.getMenuDrawer();
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

            if (response.isSuccessful()) {
                response.body();

                try {
                    jsonObject = new JSONObject(response.body().toString());
                    JSONObject jsonObject1 = jsonObject.getJSONObject("user");
                    JSONArray jsonArray = jsonObject1.getJSONArray("userdata");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        jsonObject1 = (JSONObject) jsonArray.opt(i);

                        Userdatum userdatum = new Userdatum();
                        userdatum.setMenuName(jsonObject1.getString("menuName"));
                        userdatum.setMenuID(jsonObject1.getString("menuID"));
                        userdatum.setIcon(jsonObject1.getString("icon"));
                        userdatum.setParentID(jsonObject1.getString("parentID"));
                        userdatum.setPriority(jsonObject1.getString("parentID"));

                        list.add(userdatum);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("@NO Success", response.message());

            }
        }
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {

            Log.d("@counselingError", "");
        }
    });
}

我获得了成功的响应,但不知道如何获得响应: 响应是这样的格式:

{     “用户”:{         “用户数据”: [             {                 “ menuID”:“ 153”,                 “ menuName”:“注册(1)”,                 “ link”:“注册”,                 “ icon”:“ iniicon-feesreport”,                 “ pullRight”:null,                 “ status”:“ 1”,                 “ parentID”:“ 152”,                 “优先级”:“ 10010”             },             {                 “ menuID”:“ 1”,                 “ menuName”:“仪表板”,                 “ link”:“仪表板”,                 “ icon”:“ fa-laptop”,                 “ pullRight”:“”,                 “ status”:“ 1”,                 “ parentID”:“ 0”,                 “优先级”:“ 10000”             }

1 个答案:

答案 0 :(得分:0)

由于您没有提到成功响应,因此无法完全为您提供帮助,我将让您在告诉您该概念的同时记住您知道json解析 首先,必须首先调用api方法getMenu(),例如

 public class NavigationViewActivity extends AppCompatActivity {

   private DrawerLayout mDrawerLayout;
   ExpandableListAdapter mMenuAdapter;
   ExpandableListView expandableList;
   List<ExpandedMenuModel> listDataHeader;
   HashMap<ExpandedMenuModel, List<String>> listDataChild;
   JSONObject jsonObject;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_navigation_view);
   final ActionBar ab = getSupportActionBar();
   /* to set the menu icon image*/
   ab.setHomeAsUpIndicator(android.R.drawable.ic_menu_add);
   ab.setDisplayHomeAsUpEnabled(true);
   mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
   expandableList = (ExpandableListView) findViewById(R.id.navigationmenu);
   NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);

   if (navigationView != null) {
      setupDrawerContent(navigationView);
    }

    /// Then Call Api Method 
   getMenu()

 }


  private void getMenu() {


 ApiInterface 
apiInterface=RetrofitManager.getClient().create(ApiInterface.class);
  Call<JsonObject> call = apiInterface.getMenuDrawer();  
   call.enqueue(new Callback<JsonObject>() {
   @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

        if (response.isSuccessful()) {
            response.body();

            try {
                jsonObject = new JSONObject(response.body().toString());
                JSONObject jsonObject1 = jsonObject.getJSONObject("user");
                JSONObject jsonObject2 = jsonObject1.getJSONObject("userdata");
            // dont know what response is coming 
         // add items for heading for 0 index  Like that
         listDataHeader = new ArrayList<ExpandedMenuModel>();
           listDataChild = new HashMap<ExpandedMenuModel, List<String>>();

           ExpandedMenuModel item = new ExpandedMenuModel();
          item.setIconName("heading1");
          item.setIconImg(android.R.drawable.ic_delete);//icon can be dynamic 
          // Adding data header
           listDataHeader.add(item1);
         //then add the all child item on 0 in seperate array list  List<String> heading = new ArrayList<String>();
       //then combine them one header and their child like that
       for(int i=0;i<heading.size;i++){
          listDataChild.put(item, heading(i);//adding one by one 
         }
    // Then Last 
     mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
   expandableList.setAdapter(mMenuAdapter);
   expandableList.setOnChildClickListener(new 
   ExpandableListView.OnChildClickListener() {
      @Override
      public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {

            return false;
      }
    });
    expandableList.setOnGroupClickListener(new 
 ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View 
       view, int i, long l) {

             return false;
       }







            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("@NO Success", response.message());

        }
    }
    @Override
    public void onFailure(Call<JsonObject> call, Throwable t) {

        Log.d("@counselingError", "");
    }
});
  }

并在代码中使用所有其他功能并根据您的定义子项的onclick条件。并且请共享jsonstring我会为您完成的。这个概念可以使用。