Android 如何显示导航栏以打开片段上的链接
我想在运行时在导航抽屉中显示一个列表或某种菜单。我有一个单独的活动,在其中我使用框架布局放置片段。此片段包含Webview。每当用户单击导航项时,webview就会打开其可选链接。我成功地收到了改装2.3的响应,但现在我不知道如何实现这一点Android 如何显示导航栏以打开片段上的链接,android,android-fragments,retrofit2,navigation-drawer,json-api,Android,Android Fragments,Retrofit2,Navigation Drawer,Json Api,我想在运行时在导航抽屉中显示一个列表或某种菜单。我有一个单独的活动,在其中我使用框架布局放置片段。此片段包含Webview。每当用户单击导航项时,webview就会打开其可选链接。我成功地收到了改装2.3的响应,但现在我不知道如何实现这一点 public void onResponse(Call<List<NavBarResult>> call, Response<List<NavBarResult>> response)
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
}
我希望所有菜单项都能自动工作,即使我们更改了API中的数据。提前感谢。可能重复
您可以从导航视图中动态获取菜单,然后使用JSON响应添加新菜单项
在OnNavigationItemSelectedListener中,您可以更改Web视图的URL
编辑1
也许我误解了什么。是否要用JSON响应填充导航栏?在这种情况下,我上面的链接可以帮助你
是否要用JSON响应填充导航抽屉?
在这种情况下,您应该开始阅读以下内容:
编辑活动布局:
<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>
当您得到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());
}
希望这次能对你有所帮助 您可以在导航抽屉中添加导航视图,然后当您从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);
}
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());
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
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);
}
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);
}