如何使用Kotlin在Tab布局中显示recyclerview?

时间:2018-08-30 05:57:09

标签: android android-recyclerview kotlin android-tablayout

我是Android的新手,我有一个由Kotlin语言开发的应用程序。我的活动之一是包含带有碎片的选项卡布局(两个选项卡)。每个选项卡都列出了带有数据的recyclerview。我震惊地显示了我的recyclerview标签视图中显示数据。这是我尝试过的。

main.kt

private fun setupViewPager(viewPager: ViewPager) {
        val adapter = ViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(OneFragment(), "RECEIVE LIST")
        adapter.addFragment(TwoFragment(), "SEND LIST")
        viewPager.adapter = adapter
    }

    internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
        private val mFragmentList = ArrayList<Fragment>()
        private val mFragmentTitleList = ArrayList<String>()

        override fun getItem(position: Int): Fragment {
            return mFragmentList[position]
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }

        fun addFragment(fragment: Fragment, title: String) {
            mFragmentList.add(fragment)
            mFragmentTitleList.add(title)
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mFragmentTitleList[position]
        }
    }

TransactionRecyclerAdapter.kt

enum class TransactionAdapterDirection {
    INCOMING, OUTGOING
}

class TransactionRecyclerAdapter(private val transactionList: List<TransactionEntity>,
                                 val appDatabase: AppDatabase,
                                 private val direction: TransactionAdapterDirection,
                                 val networkDefinitionProvider: NetworkDefinitionProvider) : RecyclerView.Adapter<TransactionViewHolder>() {

    override fun getItemCount() = transactionList.size

    override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) = holder.bind(transactionList[position], appDatabase)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.transaction_item, parent,false)
        return TransactionViewHolder(itemView,direction,networkDefinitionProvider)

OneFragment.kt

class OneFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

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

        val rootView = inflater!!.inflate(R.layout.frag_one, container, false)
        val recyclerView = rootView.findViewById<RecyclerView>(R.id.transaction_recycler_in) as RecyclerView
        recyclerView.setHasFixedSize(true);
        recyclerView
                .setLayoutManager(LinearLayoutManager(getActivity()));//Linear Items

        recyclerView.adapter = TransactionRecyclerAdapter()// struck with here 
        recyclerView.layoutManager = LinearLayoutManager(context)
        return rootView
    }
}

1 个答案:

答案 0 :(得分:0)

更改:

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(LinearLayoutManager(getActivity()));

        recyclerView.adapter = TransactionRecyclerAdapter()// struck with here 
        recyclerView.layoutManager = LinearLayoutManager(context)

收件人:

linearLayoutManager = LinearLayoutManager(getActivity(), LinearLayout.VERTICAL, false)
        recyclerView?.layoutManager = linearLayoutManager
        recyclerView?.adapter = TransactionRecyclerAdapter() // Your adapter
        recyclerView?.setHasFixedSize(true);

别忘了添加:

private var linearLayoutManager: LinearLayoutManager? = null
onCreateView()上方的

。您实际上使它太复杂了。