markdown [查看寻呼机不完整,添加片段示例] #android_snippet #android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [查看寻呼机不完整,添加片段示例] #android_snippet #android相关的知识,希望对你有一定的参考价值。

A ```ViewPager``` is a ```ViewGroup``` that allows the user to flip left and right through pages of data. You supply an implementation of a ```PagerAdapter``` to generate the pages that the view shows.

Create a custom Pager Adapter

```
// override 4 methods as shown below
class MyPagerAdapter: PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {

        val inflater = LayoutInflater.from(container.context)
        val view = when {
            position == 0 -> inflater.inflate(R.layout.layout1, container, false)
            position == 1 -> inflater.inflate(R.layout.layout2, container, false)
            position == 2 -> inflater.inflate(R.layout.layout3, container, false)
            else -> {
                inflater.inflate(R.layout.layout1, container, false)
            }
        }
        // add the view to the container
        container.addView(view, 0)
        return view
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return 3
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }
}
```

Inside the Activity's ```onCreate``` add the adapter to the ViewPager

```
class MainActivity : AppCompatActivity() {

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

        viewPager.adapter = MyPagerAdapter()
    }
}
```

#### Ref:
- [How to use viewpager in android](https://android--examples.blogspot.in/2015/10/how-to-use-viewpager-in-android.html)

以上是关于markdown [查看寻呼机不完整,添加片段示例] #android_snippet #android的主要内容,如果未能解决你的问题,请参考以下文章

切换到片段选项卡主机中的选项卡时查看寻呼机丢失其内容

Android如何将listview放入查看寻呼机片段

从头开始查看寻呼机图像(片段)滑块

视图重新添加到视图寻呼机中的片段

EditText 软键盘总是在视图寻呼机的片段上打开

更少的 Markdown 语法着色(寻呼机)