Kotlin 中的网格布局?

Posted

技术标签:

【中文标题】Kotlin 中的网格布局?【英文标题】:GridLayout in Kotlin? 【发布时间】:2018-02-06 04:46:59 【问题描述】:

我试图在我的 Kotlin android 应用程序中获得 2x6 (WxH) GridLayout。我为 RecyclerView 设置了我的 xml 和片段/适配器,但对于如何将 GridLayout 应用到此有点不知所措。

如何让我的项目 (listview_row_enrollments.xml) 显示在网格中,而不仅仅是水平列表?

EnrollmentFragment.kt

class EnrollmentsFragment : Fragment() 

    // TODO: Rename and change types of parameters
    private var mParam1: String? = null
    private var mParam2: String? = null

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        if (arguments != null) 
            mParam1 = arguments.getString(ARG_PARAM1)
            mParam2 = arguments.getString(ARG_PARAM2)
        
    

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? 
        // Inflate the layout for this fragment
        var view = inflater!!.inflate(R.layout.fragment_enrollments, container, false)
        loadView(view)
        return view
    

    fun loadView(view: View) 
        var recyclerView: RecyclerView = view.findViewById<RecyclerView>(R.id.recyclerView) as RecyclerView
        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerView.setHasFixedSize(true)
        recyclerView.adapter = EnrollmentsAdapter()
    

//    override fun onAttach(context: Context?) 
//        super.onAttach(context)
//        if (context is OnFragmentInteractionListener) 
//            mListener = context
//         else 
//            throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
//        
//    

    override fun onDetach() 
        super.onDetach()
        mListener = null
    

    interface OnFragmentInteractionListener 
        // TODO: Update argument type and name
        fun onFragmentInteraction(uri: Uri)
    

    companion object 

        private val ARG_PARAM1 = "param1"
        private val ARG_PARAM2 = "param2"

        fun newInstance(): EnrollmentsFragment 
            val fragment = EnrollmentsFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        
    
// Required empty public constructor

EnrollmentsAdapter.kt:

class EnrollmentsAdapter : RecyclerView.Adapter<EnrollmentsAdapter.EnrollmentsViewHolder>() 
    private val items: List<String>

    init 
        this.items = Arrays.asList("Breaches", "Data Leaks", "Hacker Chatter", "Services Monitored", "xxx", "xxx")
    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EnrollmentsViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.listview_row_enrollments, parent, false)

//        view.titleTextView.typeface = Typeface.createFromAsset(view.context.assets, "fonts/Montserrat-Regular.ttf")
//        view.countTextView.typeface = Typeface.createFromAsset(view.context.assets, "fonts/Montserrat-Regular.ttf")
//        view.subtextTextView.typeface = Typeface.createFromAsset(view.context.assets, "fonts/Montserrat-Regular.ttf")
//        view.updatedTextView.typeface = Typeface.createFromAsset(view.context.assets, "fonts/Montserrat-Light.ttf")

        return EnrollmentsViewHolder(view)
    

    override fun onBindViewHolder(holder: EnrollmentsViewHolder, position: Int) 
        holder.bind(getItem(position))
    

    override fun getItemCount(): Int 
        return 6
    

    fun getItem(position: Int): String 
        return items[position]
    

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

        fun bind(value: String) 
//            itemView.titleTextView.text = value
        

    


FragmentEnrollments.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context="xxx.xxx.EnrollmentsFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:background="@color/kLightGray"
        android:paddingTop="8dp"
        android:clipToPadding="false"
        android:layout_
        android:layout_ />

</RelativeLayout>

listview_row_enrollments.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:padding="5dp"
    android:layout_
    android:layout_>

    <TextView
        android:id="@+id/info_text"
        android:layout_
        android:layout_
        android:gravity="center"
        android:background="@color/colorAccent"/>

</LinearLayout>

我现在的看法是这样的:

【问题讨论】:

请使用 Gridlayoutmanager 检查 Recyclerview android4dev.com/… 【参考方案1】:

请改用GridLayoutManager。

替换:

recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

与:

recyclerView.layoutManager = GridLayoutManager(context, 2)

【讨论】:

【参考方案2】:

将您的 recyclerview.layoutManagerLinearLayoutManager 替换为 GridLayoutManger GridLayoutManager doc

【讨论】:

【参考方案3】:

1/-在main_activity.xml中替换你的

recyclerview.layoutManager from LinearLayoutManager to a GridLayoutManger

2-/then 在 MainActivity.kt

add recyclerView.layoutManager = GridLayoutManager(this, 2)

经过长时间的搜索后为我工作

【讨论】:

以上是关于Kotlin 中的网格布局?的主要内容,如果未能解决你的问题,请参考以下文章

网格布局android / kotlin中单元格的高度和宽度相同

安卓网格布局如何用java代码实现跨多行跨多列

小程序中的网格布局

小程序中的网格布局

如何在kotlin的片段内显示网格视图?

iphone中的所有应用程序都以“网格图标”布局出现,我该如何实现这种布局?