本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下
1。先布局实现面。xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " xmlns:应用=" http://schemas.android.com/apk/res-auto " xmlns:工具=" http://schemas.android.com/tools " android: layout_width=" match_parent " android: layout_height=" match_parent " 工具:上下文=癱om.bwei.fragment.MainActivity”比; & lt; android.support.v4.view.ViewPager android: id=癅 + id/寻呼机” android: layout_width=" wrap_content " android: layout_height=" wrap_content "/比; & lt; RadioGroup android: id=癅 + id/radioGroup1” android: layout_width=" match_parent " android: layout_height=" wrap_content " android: layout_alignBottom=癅 + id/寻呼机” android: layout_centerHorizontal=" true " 面向android:="水平" android:背景=" # ccc” 比; & lt; RadioButton android: id=癅 + id/radio0” android: layout_width=" 0 dp” android: layout_height=" match_parent " android:检查=" true " android:按钮=" @null " android: layout_weight=" 1 " android:文本="微信" android: drawableTop=" @mipmap/ic_launcher_round” android:重力="中心"/比; & lt; RadioButton android: id=癅 + id/迎合广大” android: layout_width=" 0 dp” android: layout_height=" match_parent " android:按钮=" @null " android: layout_weight=" 1 " android:文本="通讯录” android: drawableTop=" @mipmap/ic_launcher_round” android:重力="中心"/比; & lt; RadioButton android: id=癅 + id/radio2” android: layout_width=" 0 dp” android: layout_height=" match_parent " android:按钮=" @null " android: layout_weight=" 1 " android:文本="发现” android: drawableTop=" @mipmap/ic_launcher_round” android:重力="中心"/比; & lt; RadioButton android: id=癅 + id/radio3” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android:按钮=" @null " android: layout_weight=" 1 " android:文本="我的" android: drawableTop=" @mipmap/ic_launcher_round” android:重力="中心"/比; & lt;/RadioGroup> & lt;/RelativeLayout> >之前2。创建3个片段要继承片段类v4包下的
公开课FragmentThree延伸片段{ @Nullable @Override 公众视野> & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=癿atch_parent”比; & lt; TextView android: id=癅 + id/textView” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: layout_alignParentTop=" true " android: layout_centerHorizontal=" true " android: layout_marginTop=" 230 dp” android:文本=" 1 "/比; & lt;/RelativeLayout> >之前4。创建适配器继承FragmentPagerAdapter
包com.bwei.fragment; 进口android.content.Context; 进口android.support.v4.app.Fragment; 进口android.support.v4.app.FragmentManager; 进口android.support.v4.app.FragmentPagerAdapter; 进口并不知道; 公开课MyAdapter延伸FragmentPagerAdapter { 私人List碎片; 私人上下文语境;//构造方法 公共MyAdapter (FragmentManager调频List 片段,上下文语境){ 超级(fm); this.fragments=碎片; 这一点。上下文=上下文; }//得到条项目 @Override 公共片段getItem (int位置){ 返回fragments.get(位置); }//得到数量 @Override 公共int getCount () { 返回fragments.size (); } } >之前 5。在mainActivity实现效果
包com.bwei.fragment; 进口android.graphics.Color; 进口android.os.Bundle; 进口android.support.annotation.IdRes; 进口android.support.v4.app.Fragment; 进口android.support.v4.app.FragmentManager; 进口android.support.v4.view.ViewPager; 进口android.support.v7.app.AppCompatActivity; 进口android.widget.RadioButton; 进口android.widget.RadioGroup; 进口android.widget.Toast; 进口java.util.ArrayList; 进口并不知道; 公共类MainActivity AppCompatActivity延伸实现RadioGroup.OnCheckedChangeListener: viewpage。OnPageChangeListener { 私人:viewpage vPager; 私人ListAndroid中片段+:viewpage实现布的局