gpt4 book ai didi

java - 将 RecyclerView 转换为 ViewPager/PagerAdapter

转载 作者:行者123 更新时间:2023-12-02 04:13:48 25 4
gpt4 key购买 nike

背景 - Android 新手,但对于移动布局相当漂亮,了解 Java、Kotlin 和 XML。然而这个任务似乎远远超出了我的能力范围。

问题 - 我希望将以下 Java 文件 (RecyclerView) 转换为 Kotlin 文件 (ViewPager) - 因为我已经有一个 ViewPager 连接了所需的相同滚动行为。我的印象是,对于经验丰富的开发人员来说,这只是 10 分钟的工作。如果是这种情况,我想知道是否可以寻求社区的帮助?至少要弄清楚从哪里开始。我似乎找不到有关如何将 RecyclerView 转换为 PagerAdapter 或 ViewPager 的指南。

本质上,我正在使用的现有 ViewPager 具有静态数据(5 个项目),而这个可能有数十个项目,因此需要使用单独的数据源(CardItem 的项目)进行动态化。

<小时/>

RecyclerViewAdapter - 当前(Java)

package com.APPNAME.fragments.cards;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import com.APPNAME.R;
import com.APPNAME.databinding.FragmentCardsRecentlyViewedBinding;
import com.APPNAME.model.cardItem.CardItem;



public class RecentlyViewedAdapter extends RecyclerView.Adapter<RecentlyViewedAdapter.RecentlyViewedViewHolder> {

public OnCardClicked listener;
private ArrayList<CardItem> items = new ArrayList<>();

public void addItems(ArrayList<CardItem> list) {
items = list;
notifyDataSetChanged();
}

@Override
public RecentlyViewedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
FragmentCardsRecentlyViewedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_cards_recently_viewed, parent, false);
return new RecentlyViewedViewHolder(binding);
}

@Override
public void onBindViewHolder(RecentlyViewedViewHolder holder, int position) {
holder.binding.setViewModel(new RecentlyViewedViewModel(items.get(position)));
}

@Override
public int getItemCount() {
return items.size();
}

interface OnCardClicked {
void onCardClicked(View view, CardItem cardItem);
}

class RecentlyViewedViewHolder extends RecyclerView.ViewHolder {
FragmentCardsRecentlyViewedBinding binding;

public RecentlyViewedViewHolder(FragmentCardsRecentlyViewedBinding itemView) {
super(itemView.getRoot());
binding = itemView;
binding.cardView.setOnClickListener(v -> {
if (listener != null) {
listener.onCardClicked(v, items.get(getAdapterPosition()));
}
});
}
}
}

ViewPagerAdapter - future (Kotlin)

package com.APPNAME.fragments.cards
import android.support.annotation.DrawableRes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
import kotlinx.android.synthetic.main.fragment_cards_recently_viewed.view.*
import com.APPNAME.R
import com.APPNAME.activities.BaseActivity
import com.APPNAME.activities.cards.NewCardActivity
import com.APPNAME.model.cardItem.CardItem
import com.APPNAME.views.wrapContentViewPager.ObjectAtPositionPagerAdapter


class RecentlyViewedItemAdapter constructor(private val activity: BaseActivity) : ObjectAtPositionPagerAdapter() {

private var items = ArrayList<CardItem>()

override fun instantiateItemObject(container: ViewGroup, position: Int) : Any {
return getImageView(container, R.drawable.placeholder_card_image) { NewCardActivity.start(activity, it) }
}

private fun getImageView(container: ViewGroup, @DrawableRes imageResourceId: Int, onClick: (imageResourceId: Int) -> Unit = {}): View {
val layoutInflater = LayoutInflater.from(container.context)
val layout = layoutInflater.inflate(R.layout.fragment_cards_recently_viewed, container, false)
val image = layout.recentlyViewedImage
image.setImageResource(imageResourceId)
image.setOnClickListener { onClick(imageResourceId) }
container.addView(layout)
return layout
}

override fun isViewFromObject(view: View, anObject: Any) = (view == anObject)
override fun getCount() = 5 //Placeholder

override fun destroyItemObject(container: ViewGroup, position: Int, view: Any) {
container.removeView(view as View)
}
}

最佳答案

如果您有相对较多的项目并且需要回收 View ,那么 RecyclerView 是您的正确选择。这肯定比创建具有固定项目计数和 View 持有者模式的自定义 View 寻呼机适配器更容易。默认/通用 PageAdapter 不会回收任何东西 - 添加它是为了确保您已启动所有 View 并准备好进行滑动。但是,您可以使用 https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html当不再使用或重复使用时,这将销毁/重新创建 fragment 。

关于java - 将 RecyclerView 转换为 ViewPager/PagerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56674476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com