gpt4 book ai didi

android - 使用加载 gif 将项目动态添加到 NavigationView

转载 作者:行者123 更新时间:2023-11-30 01:52:12 24 4
gpt4 key购买 nike

我有一个导航 View (边栏),如下所示:

enter image description here

列表中的前 4 项是静态的(它们永远不会改变)。但是,我需要将项目动态添加到上图中分隔线下方的侧边栏(从我的 REST API 获取)。几个问题:

  1. 如何在分隔线下方的新子标题下向边栏动态添加新项目?
  2. 在添加项目时,如何在分隔线下方显示加载(微调器)gif 以通知用户正在发生某些事情?

这是我的 Activity 中的 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>

最佳答案

这个答案使用库 MaterialDrawer .

如@Ravi Gadipudi 所述,您可以使用 MaterialDrawer图书馆。

作为MaterialDrawer默认情况下不包含加载进度的功能我已经修改了DrawerActivitysample 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
}
}

关于android - 使用加载 gif 将项目动态添加到 NavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com