欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中fragment+viewpager实现布局

程序员文章站 2023-11-30 22:00:34
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 1.先布局实现mian.xml

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

1.先布局实现mian.xml

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context="com.bwei.fragment.mainactivity"> 
 
  <android.support.v4.view.viewpager 
    android:id="@+id/pager" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
 
  <radiogroup 
    android:id="@+id/radiogroup1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignbottom="@+id/pager" 
    android:layout_centerhorizontal="true" 
    android:orientation="horizontal" 
    android:background="#ccc" 
    > 
 
    <radiobutton 
      android:id="@+id/radio0" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:checked="true" 
      android:button="@null" 
      android:layout_weight="1" 
      android:text="微信" 
      android:drawabletop="@mipmap/ic_launcher_round" 
      android:gravity="center" 
      /> 
 
    <radiobutton 
      android:id="@+id/radio1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:button="@null" 
      android:layout_weight="1" 
      android:text="通讯录" 
      android:drawabletop="@mipmap/ic_launcher_round" 
      android:gravity="center" 
      /> 
 
    <radiobutton 
      android:id="@+id/radio2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:button="@null" 
      android:layout_weight="1" 
      android:text="发现" 
      android:drawabletop="@mipmap/ic_launcher_round" 
      android:gravity="center" /> 
    <radiobutton 
      android:id="@+id/radio3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:button="@null" 
      android:layout_weight="1" 
      android:text="我的" 
      android:drawabletop="@mipmap/ic_launcher_round" 
      android:gravity="center"/> 
  </radiogroup> 
</relativelayout> 

2.创建3个fragment 要继承fragment类v4包下的

public class fragmentthree extends fragment { 
  @nullable 
  @override 
  public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { 
     //引入布局文件 
    view view = inflater.inflate(r.layout.fragmentthree, null); 
    return view; 
  } 
} 

3.创建fragment 相对应的布局文件

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
 
  <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="230dp" 
    android:text="one" /> 
</relativelayout> 

4.创建适配器继承fragmentpageradapter

package com.bwei.fragment; 
 
import android.content.context; 
import android.support.v4.app.fragment; 
import android.support.v4.app.fragmentmanager; 
import android.support.v4.app.fragmentpageradapter; 
 
import java.util.list; 
 
 
public class myadapter extends fragmentpageradapter { 
  private list<fragment> fragments; 
  private context context; 
  //构造方法 
  public myadapter(fragmentmanager fm, list<fragment> fragments, context context) { 
    super(fm); 
    this.fragments = fragments; 
    this.context = context; 
  } 
  //得到item条目 
  @override 
  public fragment getitem(int position) { 
    return fragments.get(position); 
  } 
 
  //得到数量 
  @override 
  public int getcount() { 
    return fragments.size(); 
  } 
} 

5.在mainactivity实现效果

package com.bwei.fragment; 
 
import android.graphics.color; 
import android.os.bundle; 
import android.support.annotation.idres; 
import android.support.v4.app.fragment; 
import android.support.v4.app.fragmentmanager; 
import android.support.v4.view.viewpager; 
import android.support.v7.app.appcompatactivity; 
import android.widget.radiobutton; 
import android.widget.radiogroup; 
import android.widget.toast; 
 
import java.util.arraylist; 
import java.util.list; 
 
public class mainactivity extends appcompatactivity implements radiogroup.oncheckedchangelistener,viewpager.onpagechangelistener { 
 
  private viewpager vpager; 
  private list<fragment> fragments; 
  private fragmentmanager fm; 
  private radiogroup mradiogroup; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    //初始化控件 
    initview(); 
    initdate(); 
  } 
 
  private void initview() { 
    vpager=(viewpager) findviewbyid(r.id.pager); 
    vpager.setonpagechangelistener(this); 
    mradiogroup=(radiogroup) findviewbyid(r.id.radiogroup1); 
    mradiogroup.setoncheckedchangelistener(this); 
  } 
 
  private void initdate() { 
    fragments=new arraylist<fragment>(); 
    //实例化fragment 
    fragmentone fragmentone = new fragmentone(); 
    fragmenttwo fragmenttwo = new fragmenttwo(); 
    fragmentthree fragmentthree = new fragmentthree(); 
 
    //添加到集合 
    fragments.add(fragmentone); 
    fragments.add(fragmenttwo); 
    fragments.add(fragmentthree); 
 
    //得到getsupportfragmentmanager()的管理器 
    fm = getsupportfragmentmanager(); 
    //得到适配器 
    myadapter myadapter = new myadapter(fm, fragments, this); 
    //设置适配器 
    vpager.setadapter(myadapter); 
  } 
 
  //viewpager.onpagechangelistener监听事件 
  @override 
  public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { 
 
  } 
 
  @override 
  public void onpageselected(int position) { 
 
    for (int i = 0; i <fragments.size() ; i++) { 
      radiobutton radiobutton = (radiobutton) mradiogroup.getchildat(i); 
      if (i==position) { 
        radiobutton.setchecked(true); 
        //设置选中的颜色 
        radiobutton.setbackgroundcolor(color.red); 
      }else { 
        radiobutton.setchecked(false); 
        radiobutton.setbackgroundcolor(color.black); 
        radiobutton.setbackgroundcolor(color.black); 
      } 
    } 
  } 
 
  @override 
  public void onpagescrollstatechanged(int state) { 
 
  } 
 
  //radiogroup的监听事件 
 
  @override 
  public void oncheckedchanged(radiogroup radiogroup, @idres int i) { 
 
    for (int j = 0; j <fragments.size() ; j++) { 
      //得到radiobutton 
      radiobutton radiobutton = (radiobutton) mradiogroup.getchildat(j); 
      int id = radiobutton.getid(); 
      toast.maketext(this,id+"", toast.length_short).show(); 
      //判断radiobutton的id是否等于选中的id 
      if (radiobutton.getid()==i) { 
        //设置当前页 
        vpager.setcurrentitem(j); 
      } 
    } 
  } 
} 

6.最后的效果图

Android中fragment+viewpager实现布局

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。