我正在使用yuyakaido cards作为卡片堆栈回收器视图,但是当列表中没有剩余的项目时,它仅显示一个空的视图持有者(在我的情况下是实物卡)。我想知道如何删除最后一张空卡,以便显示图像或其他方式让用户知道没有更多可用的卡。我已经知道这是可行的,我只是不知道该怎么做。
class CardStackAdapter(private var parties: List<GameParty>?, private var games: List<Game>?) :
RecyclerView.Adapter<CardStackAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardStackAdapter.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.game_cards, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return if (parties != null) parties!!.size else 0
}
override fun onBindViewHolder(holder: CardStackAdapter.ViewHolder, position: Int) {
if(parties != null && games != null){
val party = parties!!.get(position)
val game = games!!.filter { g -> g.id == party.gameId }[0]
holder.partyWhen.text = party.date.toString()
holder.gameName.text = game.name
holder.gameDescription.text = game.description
// holder.partyWhere.text =
// Glide.with(holder.gameImage).load().into(holder.gameImage)
} else {
//let the user know that there are not parties
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val gameImage: ImageView = view.findViewById(R.id.img_game)
val gameName: TextView = view.findViewById(R.id.txt_party_name)
val gameDescription:TextView = view.findViewById(R.id.txt_game_description)
val partyWhen:TextView = view.findViewById(R.id.txt_party_when)
val partyWhere: TextView = view.findViewById(R.id.txt_party_where)
}
fun getParties(): List<GameParty>? {
return parties
}
fun setParties(parties: List<GameParty>) {
this.parties = parties
}
}
class CardStackViewModel(application: Application): AndroidViewModel(application) {
private val viewModelJob = Job()
private val coroutineScope = CoroutineScope(viewModelJob + Dispatchers.Main)
private val _status = MutableLiveData<ApiStatus>()
val status: LiveData<ApiStatus>
get() = _status
private val database = getInstance(application)
private val gameRepository = GameRepository(database)
private val partyRepository = PartyRepository(database)
val parties = partyRepository.parties
val games = gameRepository.games
init {
coroutineScope.launch {
partyRepository.getPartiesNearYou(10, 50.0, 50.0)
gameRepository.getGames()
}
}
fun getPartiesNearYouMock(): List<GameParty> {
return listOf<GameParty>(
GameParty("1", "Temp1", Date(), 4, arrayOf("", "", ""), "2"),
GameParty("2", "Temp2", Date(), 4, arrayOf("", "", ""), "2"),
GameParty("3", "Temp2", Date(), 4, arrayOf("", "", ""), "2")
)
// ovelropen van u object, gehaald ID op, deze geef je mee aan viewmodel en die roept een repository op en returned de gameobject
}
class Factory(val app: Application) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(CardStackViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return CardStackViewModel(app) as T
}
throw IllegalArgumentException("Unable to construct viewmodel")
}
}
}
class MainActivity : AppCompatActivity(), CardStackListener {
private lateinit var binding: ActivityMainBinding
private lateinit var drawerLayout: DrawerLayout
private lateinit var viewModel: CardStackViewModel
private lateinit var cardStackView: CardStackView
private lateinit var manager: CardStackLayoutManager
private lateinit var adapter: CardStackAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(CardStackViewModel::class.java)
cardStackView = findViewById(R.id.card_stack_view)
manager = CardStackLayoutManager(this, this)
adapter = CardStackAdapter(viewModel.parties.value, viewModel.games.value)
initialize()
drawerLayout = binding.drawerLayout
val navController = this.findNavController(R.id.nav_hostView)
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
NavigationUI.setupWithNavController(binding.navView, navController)
navController.addOnDestinationChangedListener { navCont: NavController, navDest: NavDestination, _ ->
if (navDest.id == navCont.graph.startDestination) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_hostView)
return NavigationUI.navigateUp(navController, drawerLayout)
}
override fun onCardDisappeared(view: View?, position: Int) {
Timber.d("position = $position")
}
override fun onCardDragging(direction: Direction?, ratio: Float) {
Timber.d("onCardDragging: d = ${direction?.name}, r = $ratio")
}
override fun onCardSwiped(direction: Direction?) {
Timber.d("onCardDragging: d = ${direction?.name}")
if(direction?.name == "left") {
}
if(direction?.name == "right"){
}
if (manager.topPosition == 5) { // if 5 left, fetch new (max 25) parties --> always a max of 30 parties at once
paginate()
}
}
override fun onCardCanceled() {
Timber.d("onCardCanceled: ${manager.topPosition}")
}
override fun onCardAppeared(view: View?, position: Int) {
Timber.d("position = $position")
}
override fun onCardRewound() {
Timber.d("onCardCanceled: ${manager.topPosition}")
}
private fun initialize() {
manager.setStackFrom(StackFrom.Top)
manager.setVisibleCount(3)
manager.setTranslationInterval(8.0f)
manager.setScaleInterval(0.95f)
manager.setSwipeThreshold(0.3f)
manager.setMaxDegree(20.0f)
manager.setDirections(Direction.FREEDOM)
manager.setCanScrollHorizontal(true)
manager.setCanScrollVertical(true)
manager.setSwipeableMethod(SwipeableMethod.AutomaticAndManual)
manager.setOverlayInterpolator(LinearInterpolator())
cardStackView.layoutManager = manager
cardStackView.adapter = adapter
cardStackView.itemAnimator.apply {
if (this is DefaultItemAnimator) {
supportsChangeAnimations = false
}
}
}
private fun paginate() {
val old = adapter.getParties()
val new = old.plus(viewModel.getPartiesNearYouMock())
val callback = PartyDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setParties(new)
result.dispatchUpdatesTo(adapter)
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/nav_hostView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
<com.yuyakaido.android.cardstackview.CardStackView
android:id="@+id/card_stack_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button_container"
android:padding="4dp"
android:clipToPadding="false">
</com.yuyakaido.android.cardstackview.CardStackView>
<!-- <FrameLayout-->
<!-- android:id="@+id/left_overlay"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<!-- <!– Set your left overlay –>-->
<!-- </FrameLayout>-->
<!-- <FrameLayout-->
<!-- android:id="@+id/right_overlay"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<!-- <!– Set your left overlay –>-->
<!-- </FrameLayout>-->
<!-- <FrameLayout-->
<!-- android:id="@+id/top_overlay"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<!-- <!– Set your left overlay –>-->
<!-- </FrameLayout>-->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/navdrawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>