我有一个导航视图(侧边栏),如下所示:
列表中的前4项是静态的(它们永远不会改变)。但是,我需要动态地将项添加到上图中分隔符下方的侧边栏(从我的REST API获取)。几个问题:
以下是我的活动中的NavigationView:
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/menu_drawer"
/>
这是我的menu_drawer
(带有静态项目):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:icon="@drawable/ic_first"
android:title="Inbox"
android:checked="true"/>
<item
android:icon="@drawable/ic_second"
android:title="Starred" />
<item
android:icon="@drawable/ic_third"
android:title="Sent mail" />
<item
android:icon="@drawable/ic_fourth"
android:title="Drafts" />
</group>
</menu>
答案 0 :(得分:0)
查看这个库,它是一个用于抽屉https://github.com/mikepenz/MaterialDrawer的精心构建的库。或者我的建议是用列表视图项替换菜单,你可以添加/删除它以动态列出视图
答案 1 :(得分:0)
此答案使用库MaterialDrawer。
如@Ravi Gadipudi所述,您可以使用MaterialDrawer库。
由于MaterialDrawer默认情况下不包含加载进度的功能,因此我修改了DrawerActivity的sample app功能,并展示了您尝试实现的功能。
以下是创建Drawer
的示例代码,默认情况下为空(仅使用配置文件),且不确定ProgressBar
。只要您点击个人资料图片(您可以将此代码移至您需要的位置),它就会添加一些DrawerItems
,而ProgressBar
会隐藏。
public class DrawerActivity extends AppCompatActivity {
//save our header or result
private LinearLayout progressBarContainer;
private AccountHeader headerResult = null;
private Drawer result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_dark_toolbar);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create a few sample profile
// NOTE you have to define the loader logic too. See the CustomApplication for more details
final IProfile profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460").withIdentifier(100);
// Create the AccountHeader
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.addProfiles(
profile
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean current) {
addDrawerItems();
return false;
}
})
.withSelectionListEnabledForSingleProfile(false)
.withSavedInstance(savedInstanceState)
.build();
//Create the drawer
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withHasStableIds(true)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(true)
.build();
//THIS CODE WILL ADD THE PROGRESS TO THE DRAWER AND CENTER IT BELOW THE HEADER
//Create the ProgressBar to display
ProgressBar progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
//create the container which contains the progressBar and centers it
progressBarContainer = new LinearLayout(this);
progressBarContainer.setGravity(Gravity.CENTER);
//define the size for the progressBar
int size = (int) com.mikepenz.materialize.util.UIUtils.convertDpToPixel(72, this);
progressBarContainer.addView(progressBar, size, size);
//add the padding to display the ProgressBar below the header (centered)
progressBarContainer.setPadding(0, (int) (DrawerUIUtils.getOptimalDrawerWidth(this) * 9d / 16d), 0 , 0);
//add the progressBarContainer to the layout
result.getSlider().addView(progressBarContainer, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
public void addDrawerItems() {
//HIDE THE PROGRESS
progressBarContainer.setVisibility(View.GONE);
//add the drawerItems as you need them
result.addItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_compact_header).withDescription(R.string.drawer_item_compact_header_desc).withIcon(GoogleMaterial.Icon.gmd_wb_sunny),
new PrimaryDrawerItem().withName(R.string.drawer_item_action_bar_drawer).withDescription(R.string.drawer_item_action_bar_drawer_desc).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName(R.string.drawer_item_multi_drawer).withDescription(R.string.drawer_item_multi_drawer_desc).withIcon(FontAwesome.Icon.faw_gamepad),
new PrimaryDrawerItem().withName(R.string.drawer_item_non_translucent_status_drawer).withDescription(R.string.drawer_item_non_translucent_status_drawer_desc).withIcon(FontAwesome.Icon.faw_eye).withSelectable(false).withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_red_700)),
new PrimaryDrawerItem().withName(R.string.drawer_item_advanced_drawer).withDescription(R.string.drawer_item_advanced_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_adb),
new PrimaryDrawerItem().withName(R.string.drawer_item_keyboard_util_drawer).withDescription(R.string.drawer_item_keyboard_util_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_style),
new PrimaryDrawerItem().withName(R.string.drawer_item_embedded_drawer).withDescription(R.string.drawer_item_embedded_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_battery_charging_full),
new PrimaryDrawerItem().withName(R.string.drawer_item_fullscreen_drawer).withDescription(R.string.drawer_item_fullscreen_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_style)
); // add the items we want to use with our Drawer
}
}