android-fragments 使用Viewpager2和Kotlin在片段之间滑动

62lalag4  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(267)

我有一个用Java写的老项目,它在三个不同的片段之间水平滑动,它们在主活动上被声明。它工作得很完美。

public class MainActivity extends AppCompatActivity {
ViewPager pager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pager = findViewById(R.id.theViewPagerInTheMainActivity);
    pager.setOffscreenPageLimit(3);
    PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());

    Frag_A aFrag = new Frag_A();
    adapter.addItem(aFrag);
    Frag_B bFrag = new Frag_B();
    adapter.addItem(bFrag);
    Frag_C cFrag = new Frag_C();
    adapter.addItem(cFrag);

    pager.setAdapter(adapter);
    pager.setCurrentItem(1);
  }

  class PagerAdapter extends FragmentStatePagerAdapter {
    ArrayList<Fragment> items = new ArrayList<Fragment>();
    public PagerAdapter(FragmentManager fragManager) {
        super(fragManager);
    }
    public void addItem(Fragment item) {
        items.add(item);
    }
    @Override
    public Fragment getItem(int position) {
        return items.get(position);
    }
    @Override
    public int getCount() {
        return items.size();
        }
    }
}

每三个不同的片段,后面跟着不同的代码,工作正常,开始:

public class Frag_A extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_a, container, false);
    }
}

Frag_B和frag_b布局、Frag_C和frag_c布局也是如此。
我现在又做了一遍,但是是在Kotlin中,使用ViewPager2。我没有以编程方式生成任何片段,我希望将它们的三个ID都写在主Activity上。当然,直接在Android Studio中转换代码并不奏效,而且数小时的搜索也无法满足我的需求。根据我的拙见,从一个预先制作的片段创建不同颜色的片段似乎不太适合我的项目,并且所有片段都以红色下划线结束。是否可以在不导入.v4支持库的情况下创建?

wsewodh2

wsewodh21#

自己解决了!

class MainActivity : FragmentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val adapter = DatPagerAdapter(this)
    val fragsList = listOf(Frag_A(), Frag_B(), Frag_C())
    adapter.fragsListHere.addAll(fragsList)
    theVP2InTheMainActivity.adapter = adapter
    theVP2InTheMainActivity.setCurrentItem(1)
  }
  class DatPagerAdapter(fragactivity: FragmentActivity) : FragmentStateAdapter(fragactivity) {
    val fragsListHere = mutableListOf<Fragment>()
    override fun getItemCount():Int = fragListHere.size
    override fun createFragment(position: Int): Fragment = fragListHere[position]
  }
}
93ze6v8z

93ze6v8z2#

由于FragmentPagerAdapter是分离的,因此我将其切换到FragmentStateAdapter(FragmentActivity fragmentActivity),它可以很好地与ViewPager 2配合使用
我的ViewPagerAdapter:

class ViewPagerAdapter(fragmentActivity: FragmentActivity): 
FragmentStateAdapter(fragmentActivity){

override fun getItemCount(): Int {
    return 3
}

override fun createFragment(position: Int): Fragment {
    return when(position){
        0 -> {
            HomeFragment()
        }
        1 -> {
            HomeFragment2()
        }
        2 -> {
            HomeFragment3()
        }
        else -> {
            throw Resources.NotFoundException("Position Not Found")
        }
    }
}

}

我的活动:

class MainActivity5 : AppCompatActivity() {
    
        private lateinit var binding: ActivityMain5Binding
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivityMain5Binding.inflate(layoutInflater)
            val view = binding.root
            setContentView(view)
    
            binding.viewpager.adapter = ViewPagerAdapter(this)
    
        }
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.

    override fun onBackPressed() {
        if (binding.viewpager.currentItem == 0) {
            super.onBackPressed()
        } else {
            // Otherwise, select the previous step.
            binding.viewpager.currentItem = binding.viewpager.currentItem - 1
        }
    }
    }

谢谢你的关心!!注意:我使用绑定来替换findViewById

相关问题