0

我想在運行時顯示導航抽屜中的列表或某種菜單。我有一個單獨的活動,我使用框架佈局放置片段。這個片段包含Webview。每當用戶點擊導航項時,webview將打開其替代鏈接。我成功接收響應從改造2.3,但現在我不知道如何實現這一目標如何顯示導航欄打開片段鏈接

  public void onResponse(Call<List<NavBarResult>> call, Response<List<NavBarResult>> response) { 
       List<Result> result = response.body(); 
//what to do here ? 
      } 
      @Override 
      public void onFailure(Call<List<NavBarResult>> call, Throwable t) { 
//error messages here 
      } 

我的JSON API的樣子:

[ 
{ 
menulist_id: "1", 
mname: "Item 1", 
mlink: "http://example.com/technology/", 
}, 
{ 
menulist_id: "2", 
mname: "Item 2", 
mlink: "http://example.com/sports/", 
} 
] 

我想這樣的工作的所有菜單的項目自動即使工作我們更改來自API的數據。提前致謝。

回答

1

可能的重複。

Android - Add bottom navigation view dynamically

How can I add a menu dynamically to bottom navigation view?

您可以動態地得到您的導航視圖菜單,然後添加一個新的菜單項與您JSON響應。

在您的OnNavigationItemSelectedListener中,您可以更改您的webview的URL。

編輯1

也許我誤解的東西。你想用JSON響應填充導航欄嗎?在這種情況下,我上面的鏈接可以幫助你。

您是否想用JSON響應填充導航抽屜? 在你應該開始閱讀的話:https://developer.android.com/training/implementing-navigation/nav-drawer.html

編輯你的活動佈局:

<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"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- The navigation drawer --> 
    <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="#111"/> 
</android.support.v4.widget.DrawerLayout> 
  • content_frame將包含有你的web視圖片段。
  • left_drawer將包含您的動態鏈接。

獲取left_drawer:

mDrawerList = (ListView) findViewById(R.id.left_drawer); 

當你得到你的JSON響應只需設置一個適配器將mDrawerList:

public void onResponse(Call<List<NavBarResult>> call, Response<List<NavBarResult>> response) { 
    // result is a private List<Result> attribute of the class. 
    result = response.body(); 

    // Set the adapter for the list view 
    mDrawerList.setAdapter(new ArrayAdapter<Result>(this, 
    android.R.layout.simple_list_item_1, result)); 

    // Set the list's click listener 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
} 

這樣,你的抽屜佈局將與您的JSON填充動態響應。

然後你只需要定義你的OnItemClickListener,當你點擊你的列表中的一個項目時調用一個新的片段。

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     selectItem(position); 
    } 
} 

MyFragment沒有developp在這裏,但它會包含網頁視圖並等待ARG_LINK應該是一個URL在web視圖中顯示。

private void selectItem(int position) { 
    // Create a new fragment and specify the link to show based on position 
    Fragment fragment = new MyFragment(); 
    Bundle args = new Bundle(); 
    args.putString(MyFragment.ARG_LINK, result.get(position).getUrl()); 
    fragment.setArguments(args); 

    // Insert the fragment by replacing any existing fragment 
    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, fragment) 
        .commit(); 

    // Highlight the selected item and close the drawer 
    mDrawerList.setItemChecked(position, true); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

希望它能幫助你這次!

0

您可以在導航抽屜中添加導航視圖,然後當您從API接收項目時,您可以將項目添加到菜單中,請參閱下面的代碼以從菜單中添加項目。

final Menu menu = navigationView.getMenu(); 
//Here you should add the length of your json array instead 'i' . 
for (int i = 1; i <= 3; i++) { 
    menu.add("Runtime item "+ i); 
}