可能的重複。
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);
}
希望它能幫助你這次!