{"id":589,"date":"2020-06-24T16:33:53","date_gmt":"2020-06-24T08:33:53","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=589"},"modified":"2020-06-24T16:34:25","modified_gmt":"2020-06-24T08:34:25","slug":"android-galleryviewpager-%e9%a1%af%e7%a4%ba%e5%85%a9%e5%81%b4%e7%9a%84view","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-galleryviewpager-%e9%a1%af%e7%a4%ba%e5%85%a9%e5%81%b4%e7%9a%84view\/","title":{"rendered":"Android Gallery(ViewPager \u986f\u793a\u5169\u5074\u7684View)"},"content":{"rendered":"<h3>1.Layout\u8072\u660eViewPager \u4e26\u6dfb\u52a0\u5c6c\u6027<\/h3>\n<pre><code class=\"language-XML line-numbers\">&lt;androidx.viewpager.widget.ViewPager\n        android:id=\"@+id\/viewPager\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"0dp\"\n        android:layout_marginTop=\"8dp\"\n        android:layout_marginBottom=\"8dp\"\n        android:clipToPadding=\"false\"     \/\/\u6700\u5fc5\u8981\u7684\u5c6c\u6027\n        android:paddingLeft=\"15dp\"        \/\/\u6700\u5fc5\u8981\u7684\u5c6c\u6027\n        android:paddingRight=\"15dp\"       \/\/\u6700\u5fc5\u8981\u7684\u5c6c\u6027\n        app:layout_constraintBottom_toTopOf=\"@+id\/guideline\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n<\/code><\/pre>\n<h3>2.\u65b0\u589e\u4e09\u500bLayout \u76f4\u63a5\u585eImageView\u5c31\u597d\u4e86<\/h3>\n<pre><code class=\"language-XML line-numbers\"> &lt;ImageView\n        android:scaleType=\"centerCrop\"\n        android:id=\"@+id\/imageView\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\"\n        app:srcCompat=\"@drawable\/bg1\" \/&gt;\n<\/code><\/pre>\n<pre><code class=\"language-XML line-numbers\"> &lt;ImageView\n        android:scaleType=\"centerCrop\"\n        android:id=\"@+id\/imageView\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\"\n        app:srcCompat=\"@drawable\/bg2\" \/&gt;\n<\/code><\/pre>\n<pre><code class=\"language-XML line-numbers\"> &lt;ImageView\n        android:scaleType=\"centerCrop\"\n        android:id=\"@+id\/imageView\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\"\n        app:srcCompat=\"@drawable\/bg3\" \/&gt;\n<\/code><\/pre>\n<h3>3.\u5c073\u500bLayout\u585e\u5165List\u4e2d,\u4e26\u6dfb\u52a0\u81ea\u5b9a\u7fa9Adapter<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">import android.annotation.SuppressLint\nimport android.os.Bundle\nimport android.view.LayoutInflater\nimport android.view.View\nimport android.view.ViewGroup\nimport androidx.appcompat.app.AppCompatActivity\nimport androidx.viewpager.widget.PagerAdapter\nimport kotlinx.android.synthetic.main.activity_main.*\n\n\nclass MainActivity : AppCompatActivity() {\n\n    var list = mutableListOf&lt;View&gt;()\n\n    @SuppressLint(\"InflateParams\")\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        list.add(LayoutInflater.from(this).inflate(R.layout.tab1, null))\n        list.add(LayoutInflater.from(this).inflate(R.layout.tab2, null))\n        list.add(LayoutInflater.from(this).inflate(R.layout.tab3, null))\n\n        viewPager.pageMargin = 15  \/\/\u5716\u7247\u8207\u5716\u7247\u4e4b\u9593\u7684\u8ddd\u96e2\u5927\u5c0f\n        viewPager.adapter = MyPageAdapter()\n    }\n\n    inner class MyPageAdapter : PagerAdapter() {\n\n        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {\n            container.removeView(list[position])\n        }\n\n        override fun instantiateItem(container: ViewGroup, position: Int): Any {\n            container.addView(list[position])\n            return list[position]\n        }\n\n        override fun isViewFromObject(view: View, `object`: Any): Boolean {\n            return view == `object`\n        }\n\n        override fun getCount() = list.size\n    }\n}\n<\/code><\/pre>\n<h3>4.\u6548\u679c\u5c55\u793a<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/06\/Android-GalleryViewPager-\u986f\u793a\u5169\u5074\u7684View.gif\" alt=\"\" \/><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.Layout\u8072\u660eViewPager \u4e26\u6dfb\u52a0\u5c6c\u6027 &lt;androidx.viewpager.widget &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,91,92],"class_list":["post-589","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-gallery","tag-viewpager"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":591,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/589\/revisions\/591"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}