Android 通过加载gif向NavigationView动态添加项目
我有一个如下所示的导航视图(侧栏): 列表中的前4项是静态的(它们永远不会更改)。但是,我需要动态地将项目添加到上图中分隔符下方的侧栏(从RESTAPI获取)。有几个问题:Android 通过加载gif向NavigationView动态添加项目,android,android-layout,android-activity,android-xml,android-navigation,Android,Android Layout,Android Activity,Android Xml,Android Navigation,我有一个如下所示的导航视图(侧栏): 列表中的前4项是静态的(它们永远不会更改)。但是,我需要动态地将项目添加到上图中分隔符下方的侧栏(从RESTAPI获取)。有几个问题: 如何动态地将新项目添加到侧边栏中分隔符下方的新子标题下 在添加项目时,如何在分隔符下方显示加载(微调器)gif以通知用户发生了什么 以下是“我的活动”中的NavigationView: <android.support.design.widget.NavigationView android:id="@+id
<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"
/>
看看这座图书馆吧,它是一座很好的抽屉式图书馆。或者,我的建议是将菜单替换为列表视图项,您可以动态添加/删除此项以列表视图此答案使用库 如@Ravi Gadipudi所述,您可以使用该库 由于默认情况下不包含加载进度的功能,我已将的修改为功能,并展示您尝试实现的功能 下面的示例代码创建了一个
抽屉
,默认情况下该抽屉是空的(只有配置文件),带有一个不确定的进度条
。只要您单击配置文件图像(您可以将此代码移动到您需要的位置),它将添加一些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
}
}
我已经查看了那个库,但我没有发现动态加载项目或如何显示加载的gif。我希望您添加一个自定义抽屉,并在那里添加自定义布局。查看本教程以添加自定义布局。在抽屉布局中添加GIF必须使用
NavigationView
而不是自定义抽屉来完成此操作…@user5387210您可以动态添加项目,而不会出现任何问题。默认情况下不支持加载gif,但可以添加gif而不会带来任何大的麻烦。MaterialDrawer库不使用NavigationView,因为它受到限制,MaterialDrawer提供了许多附加功能(包括自定义drawerItems…),您可以在导航视图中使用listview
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
}
}