如何通过onClicking更改适配器的数据工厂?

时间:2019-06-10 13:15:17

标签: android kotlin onclick adapter

我想制作一个像File Manager这样的应用程序。我已经有了初始文件夹,并使用KDelegateAdapter将它们保存在Data Factory(FolderDataFactory,PrivateFolderDataFactory和CheckFolderDataFactory)中。我想要的是通过单击打开文件夹。为此,我将通过设置适配器数据因子来更改它们。但是我不能。我有两种选择。

我使用lambda方法处理click事件的第一种方法。但是我不能逻辑地写它。 (在我下面输入代码) 我尝试的第二种方法是通过传递全局整数变量来指示单击哪个文件夹的状态,从而在另一个类中使用重写方法。它正在正常工作,但没有将Data Factory设置为适配器。 (包括代码)

第一种方式:

    //some code

    val clickTest: (FolderModel) -> Unit = {
           when {
               (it.title == "Личные документы") -> { adapterFolders.swapData(PrivateFolderDataFactory().generateFolderList()) 
//cannot resolve adapterFolders. Yes, it is obvious. But anyway how to pass //it?}
               (it.title == "Чеки") -> { adapterFolders.swapData(CheckFolderDataFactory().generateFolderList()) }
           } }

val adapterFolders = DiffUtilCompositeAdapter.Builder()
            .add(FolderDelegateAdapter(clickTest))
            .build()
        rv_folders.layoutManager = GridLayoutManager(this, 3)
        rv_folders.adapter = adapterFolders
        adapterFolders.swapData(FolderDataFactory().generateFolderList())

第二种方式:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    //some code

    var openFolder: Int = -1
    val folderState = FolderState()

    //some code

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    //some code

    val clickTest: (FolderModel) -> Unit = {
           when {
               (it.title == "Личные документы") -> { openFolder = 0 }
               (it.title == "Чеки") -> { openFolder = 1 }
           openFolder.toast(this)  //variable is changing
           } }

val adapterFolders = DiffUtilCompositeAdapter.Builder()
            .add(FolderDelegateAdapter(clickTest))
            .build()
        rv_folders.layoutManager = GridLayoutManager(this, 3)
        rv_folders.adapter = adapterFolders
        adapterFolders.swapData(FolderDataFactory().generateFolderList())

        folderState.folderState(openFolder, adapterFolders)

        //some code }

0 个答案:

没有答案