我尝试过类似的问题,但没有一个解决我的问题。在我迁移到androidx之前,此行
slideViewPager.setAdapter(pagerAdapter)
现在很好,它会引发错误
无法解析方法'setAdapter(com.fundi.passenger.home.submenu.home.HomeFragment.MyPagerAdapter)'
还有slideIndicator.setViewPager(slideViewPager)
setViewPager CircleIndicator中的(androidx.viewpager.widget.ViewPager)无法应用 到(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;
}
}
}
答案 0 :(得分:0)
我相信这是因为外部库与AndroidX不兼容。 (假设查看它的GitHub历史记录)
我发现此库具有AndroidX兼容性: https://github.com/siralam/LoopingViewPager
一种方法是使用Android的本机库和import 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);
您将必须更新代码以实现此目的。