代码之家  ›  专栏  ›  技术社区  ›  Vector

Kotlin嵌套RecyclerView添加数据

  •  0
  • Vector  · 技术社区  · 6 年前

    我们正试图设计一个嵌套的RecyclerView,我们得到了使用两个适配器的想法。我们不理解的是数据源的构造。我们正在使用sqlite db作为数据源。我们的设计是一个父文本字段,用于描述杂货店中的一个部门。比如农产品和酒,这些部门的儿童用品是番茄、鳄梨和啤酒。
    如果我们使用两个模型和两个DB表,那么如何将子项与部门相关联?< BR> 我们想到了一张DB表,用这种格式记录1生产番茄记录2零鳄梨记录3烈性啤酒。这似乎不太明智。接下来,我们考虑使用join或union call从两个表中创建一个新表,一个表包含dept和其他项。
    我们如何安排这两个表,以便它们将项目与各自的部门关联起来?< BR> 我们还在这里猜测,我们的视窗持有者需要是一个与父适配器和子适配器对话的类。 我们将发布一张我们试图模仿的设计照片(副本)
    我们的问题是如何设计数据库表?< BR> 我们需要一个与两个适配器接口的viewholder类吗?< BR> 如何创建这两个表以及要调用什么类型的联接来创建新表?

    我们已经看过这个链接,这个想法很好,但是他的代码没有相同的数据源。一个是日期,另一个可能是sqlite kotlin nested

    好的,我们有一个工作数据库,两个适配器deptadapter和itemadapter工作,但不是同时工作。两个表dept_table和item_table都有数据
    两个表的视图显示在带有activity_list.xml的list activity中。 listactivity不能同时提供两个表视图
    我们认为错误的是,recyclerview_dept.xml中声明的recyclerview不涉及,并且所有工作或视图都由活动_list.xml中的recyclerview提供,id rvlistactivity

    下面发布的代码带有一个问题

    class listactivity:appcompatactivity()。{
    
    私有变量回收适配器:Deptadapter?=空
    私有var recycleradapter2:itemadapter?=空
    private var recyclerview:recyclerview?=空
    private var recyclerview 2:recyclerview?=空
    private val db=dbhelper(this)
    private var dept list:list<dept>=arraylist()。
    private var item list:list<item>=arraylist()。
    私有var linearlayoutmanager:linearlayoutmanager?=空
    私有var linearlayoutmanager2:linearlayoutmanager?=空
    
    覆盖fun oncreate(savedinstancestate:bundle?){
    super.oncreate(保存到状态)
    setContentView(r.layout.activity_列表)
    
    InVIEW()
    
    }//结束OnCreate
    
    覆盖fun onresume()。{
    超级.onresume())
    
    内部数据库()
    }
    
    //仅当Activity处于OnResume状态时调用此函数
    私人娱乐initdb()。{
    deptlist=db.queryalldept()。
    //itemlist=db.queryallitem()。
    if(deptlist.isEmpty()){
    title=“数据库中无记录”
    }否则{
    title=“联系人列表”
    }
    println("########################################### onSTART")
    recycleradapter=deptadapter(deptlist=deptlist,context=applicationContext)
    //recycleradapter2=itemadapter(itemlist=itemlist,context=applicationContext)
    (recyclerview as recyclerview).adapter=recycleradapter
    //(recyclerview 2作为recyclerview).adapter=recycleradapter2
    }
    
    private fun initviews()。{
    
    recyclerview=this.findViewByID(r.id.rvListActivity)
    recycleradapter=deptadapter(deptlist=deptlist,context=applicationContext)
    linearlayoutManager=linearlayoutManager(应用上下文)
    (recyclerview as recyclerview).layoutManager=linearlayoutManager!!
    
    //RecyclerView 2=this.findViewByID(r.id.rvListActivity)
    //recycleradapter2=itemadapter(itemlist=itemlist,context=applicationContext)
    //linearlayoutmanager2=linearlayoutmanager(applicationContext)
    //(RecyclerView 2作为RecyclerView)。LayoutManager=LinearLayoutManager2!!
    }
    < /代码> 
    
    

    上述活动的XML文件

    <android.support.constraint.constraintlayout xmlns:android=“http://schemas.android.com/apk/res/android”
    xmlns:app=“http://schemas.android.com/apk/res-auto”
    xmlns:tools=“http://schemas.android.com/tools”
    android:layout_width=“匹配父级”
    android:layout_height=“匹配父级”
    工具:context=“listactivity”>
    
    <android.support.v7.widget.recyclerview
    安卓:id=“@+id/rvlistactivity”
    android:layout_width=“0dp”
    android:layout_height=“0dp”
    app:layout_constraintBottom_tobottomof=“父级”
    app:layout_constrainted_toendoff=“父级”
    app:layout_constraintStart_toStartof=“父级”
    app:layout_constraintop_totopof=“parent”>
    
    </android.support.v7.widget.recyclerview>
    < /代码> 
    
    

    带有其他RecyclerView的XML文件

    <linearlayout
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:orientation=“垂直”>
    
    <android.support.v7.widget.cardview
    android:layout_width=“匹配父级”
    android:layout_height=“匹配父级”
    android:layout_margin=“5dp”
    android:id=“@+id/list_new_card”>
    
    <linearlayout xmlns:android=“http://schemas.android.com/apk/res/android”
    xmlns:app=“http://schemas.android.com/apk/res-auto”
    android:orientation=“垂直”
    android:layout_width=“匹配父级”
    android:layout_height=“wrap_content”>
    
    文本视图
    安卓:id=“@+id/tvdept”
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:layout_marginlet=“16dp”
    android:layout_margintop=“16dp”
    android:layout_marginright=“16dp”
    android:gravity=“中心垂直”
    android:text=“我是TV部门”
    android:textcolor=“@color/color\u黑色”
    安卓:textsize=“18sp”
    android:textstyle=“bold”/>
    
    <android.support.v7.widget.recyclerview
    安卓:id=“@+id/rvdept”
    android:layout_width=“匹配父级”
    android:layout_height=“包装内容”
    android:layout_marginend=“8dp”
    android:layout_marginstart=“8dp”
    app:layout_constraintBottom_tobottomof=“父级”
    app:layout_constrainted_toendoff=“父级”
    app:layout_constraintStart_toStartof=“父级”
    app:layout_constraintop_totopof=“parent”>
    
    </android.support.v7.widget.recyclerview>
    
    </linearlayout>
    
    </android.support.v7.widget.cardview>
    < /代码> 
    
    

    两个适配器

    class dept adapter(dept list:list<dept>,internal var context:context):recyclerview.adapter<dept adapter.deptview holder>()。{
    
    private var dept list:list<dept>=arraylist()。
    init this.deptlist=deptlist
    
    覆盖fun onCreateViewHolder(父级:视图组,视图类型:int):deptViewHolder{
    val view=layoutInflator.from(context.inflat)(r.layout.recyclerview_dept,parent,false)
    返回DEPTVIEWHOLDER(视图)
    }
    
    重写fun getitemcount():int{
    返回深度列表大小
    }
    
    覆盖绑定视图保持架上的fun(保持架:DeptView保持架,位置:int){
    VAL项目=深度列表[位置]
    holder.item.text=项目部门
    }
    
    内部类deptviewholder(视图:视图):recyclerview.viewholder(视图){
    var item:textview=view.findviewbyid(r.id.tvdept)as textview
    }
    < /代码> 
    
    

    }/P>

    儿童适配器,如果您愿意

    class item adapter(item list:list<item>,var context:context):recyclerview.adapter<item adapter.itemviewholder>()。{
    
    private var item list:list<item>=arraylist()。
    init this.itemlist=项目列表
    
    覆盖fun onCreateViewHolder(父:视图组,视图类型:int):itemViewHolder{
    
    val view=layoutInflator.From(context.inflat)(r.layout.RecyclerView_item,parent,false)
    返回itemviewholder(视图)
    }
    
    重写fun getitemcount():int{
    返回itemlist.size
    }
    
    覆盖bindViewholder上的fun(holder:itemViewholder,position:int){
    VAL项目=项目列表[位置]
    holder.item.text=项目.gitem
    }
    
    内部类itemviewholder(视图:视图):recyclerview.viewholder(视图){
    var item:textview=view.findview byid(r.id.tvitem)作为textview
    }
    < /代码> 
    
    

    }/P>

    表设计在单独的模型中都是简单的ID和字符串

    我们的问题是如何在listactivity中显示这两个表? 如果我们使用两个模型和两个DB表,如何将子项与部门关联?
    我们想到了一张DB表,用这种格式记录1生产番茄记录2零鳄梨记录3烈性啤酒。这似乎不太明智。接下来,我们考虑使用join或union call从两个表中创建一个新表,一个表包含dept和其他项。
    我们如何安排这两个表,以便它们将项目与各自的部门关联起来?
    我们还在这里猜测,我们的视窗持有者需要是一个与父适配器和子适配器对话的类。 我们将张贴一张我们试图模仿的设计的照片(副本)
    我们的问题是如何设计数据库表?
    我们需要一个与两个适配器接口的viewholder类吗?
    如何创建这两个表以及要调用什么类型的联接来创建新表?Nested Layout

    我们已经看过这个链接,这个想法很好,但是他的代码没有相同的数据源。一个是日期,另一个可能是sqliteKotlin Nested

    好的,我们有一个工作数据库,两个适配器deptadapter和itemadapter工作,但不是同时工作。两个表dept_table和item_table都有数据
    两个表的视图显示在带有activity_list.xml的list activity中。
    ListActivity不能同时提供两个表视图
    我们认为错误的是,recyclerview_dept.xml中声明的recyclerview不涉及,并且所有工作或视图都由活动_list.xml中的recyclerview提供,ID为rvlistactivity

    下面发布的代码一个问题

    class ListActivity : AppCompatActivity() {
    
    private var RecyclerAdapter: DeptAdapter? = null
    private var RecyclerAdapter2:ItemAdapter? =null
    private var recyclerView: RecyclerView? = null
    private var recyclerView2: RecyclerView? = null
    private val db = DBHelper(this)
    private var deptList:List<DEPT> = ArrayList()
    private var itemList:List<ITEM> = ArrayList()
    private var linearLayoutManager: LinearLayoutManager? = null
    private var linearLayoutManager2: LinearLayoutManager? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)
    
        initViews()
    
    }// end onCreate
    
    override fun onResume() {
        super.onResume()
    
        initDB()
    }
    
    // This is ONLY called when Activity is in onResume state
    private fun initDB() {
        deptList = db.queryAllDEPT()
        //itemList = db.queryAllITEM()
        if(deptList.isEmpty()){
            title = "No Records in DB"
        }else{
            title = "Contact List"
        }
        println("########################################### onSTART")
        RecyclerAdapter = DeptAdapter(deptList = deptList, context = applicationContext)
        //RecyclerAdapter2 = ItemAdapter(itemList = itemList, context = applicationContext)
        (recyclerView as RecyclerView).adapter = RecyclerAdapter
        //(recyclerView2 as RecyclerView).adapter = RecyclerAdapter2
    }
    
    private fun initViews() {
    
        recyclerView = this.findViewById(R.id.rvListActivity)
        RecyclerAdapter = DeptAdapter(deptList = deptList, context = applicationContext)
        linearLayoutManager = LinearLayoutManager(applicationContext)
        (recyclerView as RecyclerView).layoutManager = linearLayoutManager!!
    
        //recyclerView2 = this.findViewById(R.id.rvListActivity)
        //RecyclerAdapter2 = ItemAdapter(itemList = itemList, context = applicationContext)
        //linearLayoutManager2 = LinearLayoutManager(applicationContext)
        //(recyclerView2 as RecyclerView).layoutManager = linearLayoutManager2!!
    }
    

    上述活动的XML文件

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListActivity">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvListActivity"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
    </android.support.v7.widget.RecyclerView>
    

    带有其他RecyclerView的XML文件

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:id="@+id/list_new_card">
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/tvDEPT"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="16dp"
                android:gravity="center_vertical"
                android:text="I am tv tvDept"
                android:textColor="@color/color_Black"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvDEPT"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
            </android.support.v7.widget.RecyclerView>
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    两个适配器

    class DeptAdapter(deptList:List<DEPT>,internal var context: Context):RecyclerView.Adapter<DeptAdapter.DeptViewHolder>() {
    
    private var deptList:List<DEPT> = ArrayList()
    init{this.deptList = deptList}
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeptViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.recyclerview_dept,parent,false)
        return DeptViewHolder(view)
    }
    
    override fun getItemCount(): Int {
        return deptList.size
    }
    
    override fun onBindViewHolder(holder: DeptViewHolder, position: Int) {
        val items = deptList[position]
        holder.item.text = items.dept
    }
    
    inner class DeptViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var item: TextView = view.findViewById(R.id.tvDEPT) as TextView
    }
    

    }

    儿童适配器,如果您愿意

    class ItemAdapter(itemList:List<ITEM>,var context: Context):RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
    
    private var itemList:List<ITEM> = ArrayList()
    init{this.itemList = itemList}
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
    
        val view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item,parent,false)
        return ItemViewHolder(view)
    }
    
    override fun getItemCount(): Int {
        return itemList.size
    }
    
    override fun onBindViewHolder(holder:ItemViewHolder, position: Int) {
        val items = itemList[position]
        holder.item.text = items.gitem
    }
    
    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var item: TextView = view.findViewById(R.id.tvITEM) as TextView
    }
    

    }

    表设计在单独的模型中都是简单的ID和字符串

    我们的问题是如何在listactivity中显示这两个表?

    1 回复  |  直到 6 年前
        1
  •  1
  •   James_Duh    6 年前

    所以这是非常巧妙的代码。有一个问题我还没有将它绑定到sqlite数据库。
    当我有时间连接到数据库时,我会发布一个更新。
    您有许多IDES正确的两个适配器是必须的
    这里使用列表中的列表,将子模型作为 List<ChildModel>
    此代码属于 Navendra Jha

    你需要做一些小的操作,因为他认为让孩子们左右滚动是很有趣的,独立于家长,所以在显示数据的主活动中,这一行从水平到垂直的变化。

    layoutManager = LinearLayoutManager(this@MainActivity, LinearLayout.VERTICAL, false)
    

    Navendra Jha在他的代码中使用了一个图像视图,我们评论说,一旦我们理解了引擎盖下发生的事情。这是 真正地 每个Kotlin开发人员都希望在其工具箱中使用超级充电的全过程代码。