gpt4 book ai didi

java - 迁移到 Androidx 后无法解析方法 setAdapter

转载 作者:行者123 更新时间:2023-11-30 05:02:59 27 4
gpt4 key购买 nike

我尝试过类似的问题,但没有一个能解决我的问题。在我迁移到 androidx 这一行之前

slideViewPager.setAdapter(pagerAdapter)

很好,现在它抛出一个错误

Cannot resolve method 'setAdapter(com.fundi.passenger.home.submenu.home.HomeFragment.MyPagerAdapter)'

slideIndicator.setViewPager(slideViewPager)

setViewPager (androidx.viewpager.widget.ViewPager)in CircleIndicator cannot be applied to (cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager)

波纹管是我的代码的一部分

   import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
public class HomeFragment extends Fragment {
public ArrayList<Banner> banners = new ArrayList<>();
AutoScrollViewPager slideViewPager;

@Override
public void onResponse(Call<GetBannerResponseJson> call, Response<GetBannerResponseJson> response) {
if (response.isSuccessful()) {
banners = response.body().data;
Log.e("Image", response.body().data.get(0).foto);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager(), banners);
slideViewPager.setAdapter(pagerAdapter);
slideIndicator.setViewPager(slideViewPager);
slideViewPager.setInterval(6000);
slideViewPager.startAutoScroll(6000);
}
}

public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 5;
public ArrayList<Banner> banners = new ArrayList<>();

public MyPagerAdapter(FragmentManager fragmentManager, ArrayList<Banner> banners) {
super(fragmentManager);
this.banners = banners;
}

@Override
public int getCount() {
return banners.size();
}

@Override
public Fragment getItem(int position) {
return SlideFragment.newInstance(banners.get(position).id, banners.get(position).foto);
}

@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}


}

}

最佳答案

我认为这是因为外部库与 AndroidX 不兼容。 (假设通过查看它的 GitHub 历史记录)

I found this library having AndroidX compatibility: https://github.com/siralam/LoopingViewPager

还有一种方法是使用 Android 的 native 库并导入 androidx.viewpager.widget.ViewPager

示例:

Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(first, "First");
adapter.addFragment(second, "Second");
adapter.addFragment(third, "Third");
myViewPager.setAdapter(adapter);

// Auto Scroll
Handler handler = new Handler();

Runnable update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES - 1) {
currentPage = 0;
}
myViewPager.setCurrentItem(currentPage++, true);
}
};


new Timer().schedule(new TimerTask() {

@Override
public void run() {
handler.post(update);
}
}, 100, 500);

您必须更新您的代码来实现它。

关于java - 迁移到 Androidx 后无法解析方法 setAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895858/

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