最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用ViewPage+Fragment仿微信界面

 更新時間:2018年04月14日 13:25:12   作者:壁花的花與樹獺的獺  
這篇文章主要為大家詳細介紹了使用ViewPage+Fragment仿微信界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了ViewPage+Fragment仿微信界面的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)效果:

左右滑動可切換界面,點擊也可以實現(xiàn)

界面與碎片:

主界面:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 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" 
 android:orientation="vertical" 
 tools:context="com.example.g160628_android10_viewpagerfragment_zuoye.MainActivity"> 
 <!--設(shè)置ViewPager與單選組--> 
 <android.support.v4.view.ViewPager 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:id="@+id/vp_main_view" 
  android:layout_weight="1" 
  ></android.support.v4.view.ViewPager> 
 <RadioGroup 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:orientation="horizontal" 
  android:id="@+id/rg_main_group" 
  > 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:button="@null" 
   android:background="@drawable/button_selector" 
   android:id="@+id/rb_main_bu1" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:button="@null" 
   android:background="@drawable/button2_selector" 
   android:id="@+id/rb_main_bu2" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:button="@null" 
   android:background="@drawable/button3_selector" 
   android:id="@+id/rb_main_bu3" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_weight="1" 
   android:button="@null" 
   android:background="@drawable/button4_selector" 
   android:id="@+id/rb_main_bu4" 
   /> 
 </RadioGroup> 
 
</LinearLayout> 

在drawable中創(chuàng)建四個選擇器

button_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_checked="false" android:drawable="@drawable/small_weixin"></item> 
 <item android:state_checked="true" android:drawable="@drawable/small_weixin2"></item> 
</selector> 

button2_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_checked="false" android:drawable="@drawable/small_contact"></item> 
 <item android:state_checked="true" android:drawable="@drawable/small_contact2"></item> 
</selector> 

button3_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_checked="false" android:drawable="@drawable/small_find"></item> 
 <item android:state_checked="true" android:drawable="@drawable/small_find2"></item> 
</selector> 

button4_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:state_checked="false" android:drawable="@drawable/small_mine"></item> 
 <item android:state_checked="true" android:drawable="@drawable/small_mine2"></item> 
</selector> 

四個碎片:

fragment_weixin.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
  android:layout_width="470dp" 
  android:layout_height="720dp" 
  android:src="@drawable/weixin" 
  /> 
</LinearLayout> 

fragment_contact.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
  android:layout_width="470dp" 
  android:layout_height="720dp" 
  android:src="@drawable/contact" 
  /> 
</LinearLayout> 

fragment_find.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
  android:layout_width="470dp" 
  android:layout_height="720dp" 
  android:src="@drawable/find" 
  /> 
</LinearLayout> 

fragment_mine.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
  android:layout_width="470dp" 
  android:layout_height="720dp" 
  android:src="@drawable/mine" 
  /> 
</LinearLayout> 

Java代碼

主Activity:

package com.example.g160628_android10_viewpagerfragment_zuoye; 
 
import android.content.res.Resources; 
import android.support.annotation.IdRes; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
 
import java.util.ArrayList; 
import java.util.List; 
 
public class MainActivity extends AppCompatActivity { 
 private List<Fragment> fragments=new ArrayList<>(); 
 private ViewPager vp_main_view; 
 private RadioGroup rg_main_group; 
 private List<View> views; 
 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  //把碎片加入到碎片集合中 
  fragments.add(new WeiXinFragment()); 
  fragments.add(new ContactFragment()); 
  fragments.add(new FindFragment()); 
  fragments.add(new MineFragment()); 
 
  //找到自己的ViewPager 
  vp_main_view = (ViewPager) findViewById(R.id.vp_main_view); 
  vp_main_view.setAdapter(new MyAdapter(getSupportFragmentManager())); 
  //設(shè)置當前的碎片為1 
  vp_main_view.setCurrentItem(1); 
 
  //獲得單選按鈕組 
  rg_main_group = (RadioGroup) findViewById(R.id.rg_main_group); 
  //設(shè)置單選按鈕組的選擇事件 
  rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
   @Override 
   public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
    Resources res=MainActivity.this.getResources(); 
    switch (checkedId) { 
     case R.id.rb_main_bu1: 
      vp_main_view.setCurrentItem(0); 
      break; 
     case R.id.rb_main_bu2: 
      vp_main_view.setCurrentItem(1); 
      break; 
     case R.id.rb_main_bu3: 
      vp_main_view.setCurrentItem(2); 
      break; 
     case R.id.rb_main_bu4: 
      vp_main_view.setCurrentItem(3); 
      break; 
    } 
   } 
  }); 
 
  //獲得所有的單選框 
  views=rg_main_group.getTouchables(); 
 
 
  //設(shè)置單選框事件 
  vp_main_view.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
   @Override 
   public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
 
   } 
 
   @Override 
   public void onPageSelected(int position) { 
    //設(shè)置選中 
    RadioButton button= (RadioButton) views.get(position); 
    button.setChecked(true); 
   } 
 
   @Override 
   public void onPageScrollStateChanged(int state) { 
 
   } 
  }); 
 
 
 } 
 
 //定義屬于自己的適配器 
 class MyAdapter extends FragmentPagerAdapter{ 
 
  public MyAdapter(FragmentManager fm) { 
   super(fm); 
  } 
 
  //獲得碎片的所有 
  @Override 
  public Fragment getItem(int position) { 
   return fragments.get(position); 
  } 
 
  //返回碎片的長度 
  @Override 
  public int getCount() { 
   return fragments.size(); 
  } 
 } 
 
} 

四個碎片對應(yīng)的Fragment

WeiXinFragment

package com.example.g160628_android10_viewpagerfragment_zuoye; 
 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
/** 
 * Created by Administrator on 2017/6/15. 
 */ 
 
public class WeiXinFragment extends Fragment { 
 @Nullable 
 @Override 
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
  //返回對應(yīng)的fragment_weixin 
  return inflater.inflate(R.layout.fragment_weixin,null); 
 } 
} 

ContactFragment

package com.example.g160628_android10_viewpagerfragment_zuoye; 
 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
/** 
 * Created by Administrator on 2017/6/15. 
 */ 
 
public class ContactFragment extends Fragment { 
 @Nullable 
 @Override 
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
  //返回對應(yīng)的fragment_contact 
  return inflater.inflate(R.layout.fragment_contact,null); 
 } 
} 

FindFragment

package com.example.g160628_android10_viewpagerfragment_zuoye; 
 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
/** 
 * Created by Administrator on 2017/6/15. 
 */ 
 
public class FindFragment extends Fragment { 
 @Nullable 
 @Override 
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
  //返回對應(yīng)的fragment_find 
  return inflater.inflate(R.layout.fragment_find,null); 
 } 
} 

MineFragment

package com.example.g160628_android10_viewpagerfragment_zuoye; 
 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
/** 
 * Created by Administrator on 2017/6/15. 
 */ 
 
public class MineFragment extends Fragment { 
 @Nullable 
 @Override 
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
  //返回對應(yīng)的fragment_mine 
  return inflater.inflate(R.layout.fragment_mine,null); 
 } 
} 

需要的圖片

small_weixin   small_contact   small_find    small_mine

剩下的自己去截了,就不一一展示了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android自定義控件ImageView實現(xiàn)圓形圖片

    android自定義控件ImageView實現(xiàn)圓形圖片

    這篇文章主要為大家詳細介紹了android自定義控件ImageView實現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android登錄代碼MVP架構(gòu)詳解

    Android登錄代碼MVP架構(gòu)詳解

    這篇文章主要為大家詳細介紹了Android登錄代碼MVP架構(gòu)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android使用Intent傳遞組件大數(shù)據(jù)

    Android使用Intent傳遞組件大數(shù)據(jù)

    這篇文章主要介紹了Android使用Intent傳遞組件大數(shù)據(jù),文章圍繞主題展開詳細的內(nèi)容詳情,感興趣的朋友可以參考一下
    2022-07-07
  • Android14原生PackageInstaller安裝某些apk報錯問題

    Android14原生PackageInstaller安裝某些apk報錯問題

    本文主要介紹了在Android 14上安裝大型應(yīng)用時遇到的java.lang.RuntimeException: Could not copy bitmap to parcel blob錯誤,下面就一起來介紹一下解決方法,感興趣的可以了解一下
    2025-03-03
  • Android實現(xiàn)微信分享帶有縮略圖的網(wǎng)頁

    Android實現(xiàn)微信分享帶有縮略圖的網(wǎng)頁

    最近做了一個web app的項目,要求分享web頁還要帶有圖片功能,怎么實現(xiàn)呢?今天小編給大家分享android實現(xiàn)微信分享帶有縮略圖的網(wǎng)頁功能,需要的朋友參考下
    2017-02-02
  • Android使用ViewFlipper實現(xiàn)圖片切換功能

    Android使用ViewFlipper實現(xiàn)圖片切換功能

    這篇文章主要為大家詳細介紹了Android使用ViewFlipper實現(xiàn)圖片切換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • android自定義Camera拍照并查看圖片

    android自定義Camera拍照并查看圖片

    這篇文章主要為大家詳細介紹了android自定義Camera拍照并查看圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android控件ViewPager實現(xiàn)帶有動畫的引導頁

    Android控件ViewPager實現(xiàn)帶有動畫的引導頁

    這篇文章主要為大家詳細介紹了Android控件ViewPager實現(xiàn)帶有動畫的引導頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 詳解Android之圖片加載框架Fresco基本使用(二)

    詳解Android之圖片加載框架Fresco基本使用(二)

    本篇文章主要介紹了Android之圖片加載框架Fresco基本使用,可以實現(xiàn)進度條和圖片縮放等內(nèi)容,有興趣的可以了解一下。
    2016-12-12
  • Android Jetpack庫剖析之Lifecycle組件篇

    Android Jetpack庫剖析之Lifecycle組件篇

    本章也是帶來了Jetpack中我認為最重要的架構(gòu)組件Lifecycle的原理探索,至于為什么覺得它是最重要是因為像ViewModel,LiveData這些組件也依賴于Lifecycle來感知宿主的生命周期,那么本章我們帶著幾個問題來探索一下這個組件
    2022-07-07

最新評論

屏山县| 丰都县| 津市市| 东台市| 齐齐哈尔市| 任丘市| 资源县| 寿光市| 卢湾区| 湾仔区| 平远县| 雷山县| 乐业县| 延川县| 道孚县| 兴仁县| 镇江市| 大荔县| 靖安县| 呼伦贝尔市| 甘南县| 湟源县| 古蔺县| 河东区| 蒙山县| 甘德县| 广宁县| 鄂托克前旗| 靖边县| 江津市| 麟游县| 信丰县| 班戈县| 清苑县| 江永县| 涿州市| 龙门县| 宜宾县| 米泉市| 盐城市| 宜黄县|