ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在Kotlin的GridLayout?

2019-06-11 07:22:43  阅读:317  来源: 互联网

标签:android xml android-recyclerview kotlin grid-layout


我想在我的Kotlin Android应用程序中获得2×6(WxH)GridLayout.我为RecyclerView设置了我的xml和fragment / adapter,但是对于如何将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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
        android:layout_height="match_parent" />

</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_width="150dp"
    android:layout_height="150dp">

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

</LinearLayout>

我的观点现在看起来像什么:
enter image description here

解决方法:

请改用GridLayoutManager.

更换:

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

附:

recyclerView.layoutManager = GridLayoutManager(context, 2)

标签:android,xml,android-recyclerview,kotlin,grid-layout
来源: https://codeday.me/bug/20190611/1216987.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有