0

安卓工作室 3.6

我想将 ViewPager2 与片段(滑动元素)一起使用

应用程序/build.gradle:

implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') { transitive = true; }
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-beta01'
    implementation 'org.altbeacon:android-beacon-library:2.16.3'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0-beta05'

在我的自定义适配器中,我尝试这样做:

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.myproject.fragment.UserFragment

class UserAdapter(private val userList: ArrayList<String>, fragmentManger: FragmentManager) :
    FragmentStateAdapter(fragmentManger) {

    override fun getItem(position: Int): Fragment {
        return UserFragment.getInstance(userList[position])
    }

    override fun getItemCount(): Int {
        return userList.size
    }
}

但我得到两个错误:

Class 'UserAdapter' is not abstract and does not implement abstract base class member @NonNull public abstract fun createFragment(p0: Int): Fragment defined in androidx.viewpager2.adapter.FragmentStateAdapter

None of the following functions can be called with the arguments supplied:
public constructor FragmentStateAdapter(@NonNull p0: Fragment) defined in androidx.viewpager2.adapter.FragmentStateAdapter
public constructor FragmentStateAdapter(@NonNull p0: FragmentActivity) defined in androidx.viewpager2.adapter.FragmentStateAdapter

第二个错误:

'getItem' overrides nothing

我的代码有什么问题?

4

1 回答 1

0

试试这个代码

<android.support.v4.view.ViewPager
        android:id="@+id/my_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


    private void setupViewPager() {
        List<Fragment> fragmentList = new ArrayList<>();
        YourFragment m1 = new YourFragment ();
        YourFragment m2 = new YourFragment ();
        fragmentList.add(m2);
        fragmentList.add(m1);
        viewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), fragmentList));//inside fragment
        viewPager.setCurrentItem(currentItem);
        viewPager.setOffscreenPageLimit(2);
    }


     public class PagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragmentList;

        public PagerAdapter(FragmentManager fm, List<Fragment> fragmentListt) {
            super(fm);
            this.fragmentList = fragmentListt;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

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

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            //do nothing here! no call to super.restoreState(arg0, arg1);
        }
    }

希望对你有帮助

于 2019-10-23T15:31:46.670 回答