在侧边抽屉导航器中动态添加新组件

问题描述 投票:0回答:1

我有一个“ ADD”按钮,当按下该按钮时,应将新项目添加到侧面导航抽屉中。我的抽屉中已经有两个项目,并且每次按下“ ADD”按钮都想添加一个项目。我该怎么办?

android react-native react-native-navigation use-state react-navigation-drawer
1个回答
0
投票
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/header" layout="@layout/your_nav_header" android:layout_width="match_parent" android:layout_height="176dp"/> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/tvVersion" android:layout_below="@+id/header"> <LinearLayout android:id="@+id/dynamically_added_items" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="right" android:orientation="vertical"/> </ScrollView> </RelativeLayout>

然后您认为:

val li = LayoutInflater.from(context)
val mainView = li.inflate(R.layout.nav_drawer, null)
val linearLayout = mainView.findViewById<LinearLayout>(R.id.dynamically_added_items)

items.forEach {
    val navItem = MyNavigationItem(li)
    linearLayout.addView(navItem.getView())
}

哪些项目是要添加到抽屉中的项目列表,可以是列表,也可以是如下所示的一项:

val navItem = MyNavigationItem(li)
linearLayout.addView(navItem.getView())

哪个MyNavigationItem是用于设置抽屉中每个项目的视图的类:

class NavigationItemWidget(
    item: MyItem,
    li: LayoutInflater
) {

    private val mainView = li.inflate(R.layout.item_nav, null)

    fun getView(): View {
        mainView.setOnClickListener {

        }
    }

}

在上述类中,通过getView()函数设置了自定义item_nav布局的视图。

© www.soinside.com 2019 - 2024. All rights reserved.