GirdLayoutManager 不工作,kotlin,android

GirdLayoutManager not working, kotlin, android

我会尽量简短。

我有一个 MutableListOf<ItemsSale>,我想在 GirdLayoutManager 中显示它 我在添加或删除 ItemsSale 后调用此函数:

private fun showListSale(){
        RV_sales.layoutManager = GridLayoutManager(context, 4)
        //RV_sales.hasFixedSize() I've tried with and without this
        RV_sales.adapter = SalesAdapter(context, listSale)

        //for (item in listSale) I use this code so I know the MutableListOf<ItemsSale> is working and storing the items properly
          //  Toast.makeText(context,"${item.description}, QTY: ${item.quantity}, $${item.price}, $${item.subtotal}", Toast.LENGTH_SHORT).show()
}

我的适配器是这样的:

    import android.content.Context
    import android.support.v7.widget.RecyclerView
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import kotlinx.android.synthetic.main.listview_item.view.*


    class SalesAdapter (var context: Context, var list: MutableList<ItemsSale>):
            RecyclerView.Adapter<SalesAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder?{
            //var v = LayoutInflater.from(context).inflate(R.layout.listview_item, parent, false)
            //return Item(v)
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.listview_item, parent, false))
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
            //(holder as Item).bindData(list[position])
            holder?.tvDescription?.text = list[position].description
            holder?.tvPrice?.text = list[position].price.toString()
            holder?.tvQuantity?.text = list[position].quantity.toString()
            holder?.tvSubtotal?.text = list[position].subtotal.toString()
        }

        class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){

            val tvDescription = itemView.description
            val tvPrice = itemView.price
            val tvQuantity = itemView.quantity
            val tvSubtotal = itemView.subtotal

//I've also tried with this code but still doesn't wokr
            /*fun bindData (item: ItemsSale){
                itemView.description.text = item.description
                itemView.price.text = item.price.toString()
                itemView.quantity.text = item.quantity.toString()
                itemView.subtotal.text = item.subtotal.toString()
            }*/
        }
    }

但它不显示网格。我也尝试了 this, this, , also this, and this 和其他几个,但似乎没有任何效果。我知道我做错了什么,但我就是看不到它。

我想在每次调用 showLIstSale() 时刷新列表。是销售清单,因此如果添加或删除项目,我需要更新它。

提前致谢

编辑:根据请求添加布局

fragment_sale1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <AutoCompleteTextView
                android:id="@+id/ET_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:hint="@string/search_hint"
                android:maxLines="1"
                android:layout_marginLeft="@dimen/half_margin"
                android:imeOptions="actionNext"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingLeft="2dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/total"
                    android:textSize="15sp"
                    android:gravity="end"
                    android:layout_weight="0"/>

                <TextView
                    android:id="@+id/TV_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:paddingLeft="2dp"
                    android:text="@string/total_hint"
                    android:textSize="40sp"
                    android:gravity="right"/>

            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:baselineAligned="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="@dimen/half_margin">

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.5"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/description"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Descripción"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingRight="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/price"
                    android:layout_width="0dp"
                    android:layout_height="30dp"
                    android:layout_weight="0.75"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Precio"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/quantity"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.5"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Cantidad"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/subtotal"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.5"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Subtotal" />
            </TableRow>

        </LinearLayout>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RV_sales"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

</LinearLayout>

listview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:paddingBottom="1dip"
        android:paddingLeft="1dip"
        android:paddingTop="1dip" >

        <TextView
            android:id="@+id/description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:paddingBottom="1dip"
        android:paddingLeft="1dip"
        android:paddingRight="1dip"
        android:paddingTop="1dip" >

        <TextView
            android:id="@+id/price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.75"
            android:gravity="right|center_vertical"
            android:paddingRight="5dp"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:paddingBottom="1dip"
        android:paddingLeft="1dip"
        android:paddingTop="1dip" >

        <TextView
            android:id="@+id/quantity"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:gravity="center"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:paddingBottom="1dip"
        android:paddingLeft="1dip"
        android:paddingRight="1dip"
        android:paddingTop="1dip" >

        <TextView
            android:id="@+id/subtotal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.75"
            android:gravity="right|center_vertical"
            android:paddingRight="5dp"
            android:textColor="#000000" />
    </TableRow>



</LinearLayout>

您的线性布局似乎覆盖了您的回收站视图。试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <AutoCompleteTextView
                android:id="@+id/ET_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:hint="@string/search_hint"
                android:maxLines="1"
                android:layout_marginLeft="@dimen/half_margin"
                android:imeOptions="actionNext"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingLeft="2dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/total"
                    android:textSize="15sp"
                    android:gravity="end"
                    android:layout_weight="0"/>

                <TextView
                    android:id="@+id/TV_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:paddingLeft="2dp"
                    android:text="@string/total_hint"
                    android:textSize="40sp"
                    android:gravity="right"/>

            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:baselineAligned="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="@dimen/half_margin">

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.5"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/description"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="2"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Descripción"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingRight="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/price"
                    android:layout_width="0dp"
                    android:layout_height="30dp"
                    android:layout_weight="0.75"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Precio"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/quantity"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.5"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Cantidad"/>
            </TableRow>

            <TableRow
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="0.2"
                android:paddingBottom="1dip"
                android:paddingLeft="1dip"
                android:paddingTop="1dip" >

                <TextView
                    android:id="@+id/subtotal"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.5"
                    android:gravity="center"
                    android:paddingRight="3dp"
                    android:text="Subtotal" />
            </TableRow>

        </LinearLayout>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RV_sales"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

</LinearLayout>