Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (1.4k points)

用适配器模式实现ChipGroup

我的片段包含一个ChipGroup,并chips根据用户选择动态添加。我希望完成的是一个适配器模式,其中alist暴露于chips正在创建的适配器中,用户可以在列表中添加元素或从列表中删除元素。

我找不到任何办法,现在,每当用户与vist进行交互时,都会重新创建所有筹码。

为了简洁起见,代码已被裁剪

XML文件

// capturing the user's choice

      <RadioGroup

          android:id="@+id/vehicles_radio_group"

          android:onCheckedChanged="@{vehicleViewModel::radioCheckedChanged}">

视图模型

fun radioCheckedChanged(radioGroup: RadioGroup, checkedId: Int) {

        //validating the user's choice

            if (condition) {

                //code for adding the choice to a List

                vehicleDispatched(selectedVehicle, checkedId)   

            } else {

                selectionError()

            }

    }

    // adding the user's choice to the list

    // _dispatchingUnits is a LiveData

    private fun vehicleDispatched(selectedVehicle: DomainVehicle, checkedId: Int) {

     // appending the selected choice to the list(Type : List<Pair<Vehicle,Planet>>)

        _dispatchingUnits.value =

            _dispatchingUnits.value?.plus(selectedVehicle to _currentPlanet.value!!)

                ?: listOf(selectedVehicle to _currentPlanet.value!!)

    }

分段

// register an observer and take action

        myViewModel.dispatchingUnits.observe(viewLifecycleOwner) { allPairs ->

            allPairs?.apply {

                with(binding) {

                   // remove the existing chips if any

                    if (selectedPairChipGroup.isNotEmpty()) {

                        selectedPairChipGroup.removeAllViews()

                    }

                  // create new chips

                    allPairs.forEach { chipPair ->

                        createChips(chipPair)

                    }

                }

            }

        }

期待这样的事情,

// In fragment class

val adapter = ChipAdapter()

binding.chipGroup.setAdapter(adapter)

// In bindingUtils file

@BindingAdapter("fill_data")

fun RecyclerChipGroup.setData(list : MyListType){

val adapter = this.adapter as ChipAdapter

adapter.submitChipList(list)

}

//XML File

<RecyclerChipGroup

....

app:fill_data = "@{viewModel.dispatchingUnits}"

....

/>

怎么继续呢?


这个人很懒,什么也没留下...
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...