kotlin 实现viewpager图片切换

通过kotlin 来实现viewpager案例,代码部分如下
1.class ViewPagersActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_view_pagers)
    //获取碎片集合,下面是获取集合的一种写法 也可以使用java代码
    /**
     *下面这两句代码体现了与java的100%交互,可以使用java里面的用法创建集合
    var list = ArrayList()
    list.add(OneFragment())
     */
    val fragmentList = listOf(OneFragment(), TwoFragment(), ThreeFragment())
    //为viewpager设置适配器,kotlin中可以不用findviewbyid ,而是可以直接用设置的id.属性的方式书写
    viewpager.adapter = viewPagerAdapter(supportFragmentManager, fragmentList)
    //为viewpager设置页面滑动监听
    viewpager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
        override fun onPageScrollStateChanged(state: Int) {
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            Log.e("ViewPager","position is $position")
            //判断滑动后选择的页面设置相应的RadioButton被选中
            when (position) {
            //这里的btn_one two the 分别是布局中的三个RadioButton的id,直接调用其方法进行设置
                0 -> btn_one.isChecked = true
            //当只是写btn_one.isChecked获取的是它的选中状态,如果让它 =true 就会默认调用setChecked()方法进行改变状态
                1 -> btn_two.isChecked = true
                2 -> btn_three.isChecked = true
            }
        }

    })
    //对RadioGroup设置监听
    btn_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
        override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
            Log.e("ViewPager","checkedId is $checkedId")
            when (checkedId) {
            //当点击按钮时,调用如下方法选择viewpager要显示的哪个页面
                R.id.btn_one -> viewpager.setCurrentItem(0, true)
                R.id.btn_two -> viewpager.setCurrentItem(1, true)
                R.id.btn_three -> viewpager.setCurrentItem(2, true)
            }
        }
    })
}

//是为了设置第一次进入默认选中的按钮
override fun onStart() {
    super.onStart()
    btn_one.isChecked = true
}

//继承 FragmentPagerAdapter 创建适配器,利用主构造函数传值
class viewPagerAdapter(fm: FragmentManager?, var list: List) : FragmentPagerAdapter(fm) {
    override fun getItem(position: Int): Fragment {
        return list.get(position)
    }

    override fun getCount(): Int {
        return list.size
    }
}

}
2. fragment 部分

class OneFragment:Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val inflate = inflater!!.inflate(R.layout.fragment_one, container, false)
    var textView =inflate.findViewById(R.id.fragment_txt) as TextView
    textView.text="第一页"
    var img = inflate.findViewById(R.id.fragment_img)
    var bitmap = BitmapFactory.decodeResource(resources,R.drawable.writeboard_nor)
    img.setImageBitmap(bitmap)
    return inflate
}

}

class TwoFragment:Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val inflate = inflater!!.inflate(R.layout.fragment_one, container, false)
    var textView =inflate.findViewById(R.id.fragment_txt) as TextView
    textView.text="第二页"
    var img = inflate.findViewById(R.id.fragment_img)
    var bitmap = BitmapFactory.decodeResource(resources,R.drawable.wireless_tap)
    img.setImageBitmap(bitmap)
    return inflate
}

}

class ThreeFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val inflate = inflater!!.inflate(R.layout.fragment_three, container, false)
    //初始化数据源
    var data = ArrayList()
    //循环遍历添加数据  i in 0..15 表示循环区间[0,15]前后包括,0-15
    for (i in 0..15){
        data.add("条目 "+ i)
    }
    //给listview设置适配器  ,可直接通过id调用。这里的适配器使用的是系统自带
    inflate.listview.adapter = ArrayAdapter(activity,android.R.layout.simple_expandable_list_item_1,data)
    return inflate
}

}

你可能感兴趣的:(kotlin和ndk)