Android ViewPager與radiogroup實(shí)現(xiàn)關(guān)聯(lián)示例
Android ViewPager與radiogroup實(shí)現(xiàn)關(guān)聯(lián)
效果圖展示
Android ViewPager與radiogroup實(shí)現(xiàn)關(guān)聯(lián)步驟
1.實(shí)例化ViewPager
2.通過LayoutInflater加載布局,返回View結(jié)果
3.把生成的每一個(gè)View對(duì)象添加到List集合中
4.實(shí)例化適配器,傳遞View集合
5.在適配器中繼承自PagerAdapter,實(shí)現(xiàn)內(nèi)部的四個(gè)方法
- getCount(); 返回視圖的數(shù)量
- isViewFromObject(); 是否通過對(duì)象加載視圖 View==object
- instantiateltem(); 加載當(dāng)前頁面(通過container.addView();添加視圖)返回個(gè)給用戶
- destroyItem(); 銷毀滑出的視圖(通過container.removerView();銷毀視圖)
6.實(shí)例化每個(gè)RadioButton
7.點(diǎn)擊每個(gè)RaidoButton時(shí),切換不同的頁面(viewPager.setCurrentltem(下標(biāo)))
8.當(dāng)頁面切換后,還要把當(dāng)前的導(dǎo)航欄變?yōu)榫G色
- 設(shè)置文本顏色的setTextColor(getResources().getColor(R.color.tvGreen));
- 設(shè)置文本的上方的圖片的,四個(gè)參數(shù)分別為,左、上、右、下setCompoundDrawablesWithIntrinsicBounds (null,getResources().getDrawable)(R.drawable.call_t),null,null);
9.當(dāng)你每次點(diǎn)擊之前的時(shí)候,添加一個(gè)方法,清除方法,(清理之 前的所有導(dǎo)航欄的狀態(tài),置為灰色)
10.實(shí)現(xiàn)滑動(dòng)監(jiān)聽需要addOnPagerChangeListener
11.在onPagerSelected方法中,根據(jù)position頁面的下標(biāo)判斷分別設(shè)置對(duì)應(yīng)的底部導(dǎo)航欄狀態(tài)
代碼演示
1.在主布局文件中引入android-support-v4.jar包并添加RadioGroup并在RadioGroup中添加RadioButton用于顯示導(dǎo)航欄
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cxy.viewpager.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"> </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#F4F3F3" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/mess_t" android:gravity="center" android:text="微信" android:textColor="#11CD6E"/> <RadioButton android:id="@+id/radioButton2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/call_f" android:gravity="center" android:text="通訊錄" android:textColor="@android:color/darker_gray"/> <RadioButton android:id="@+id/radioButton3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/show_f" android:gravity="center" android:text="發(fā)現(xiàn)" android:textColor="@android:color/darker_gray"/> <RadioButton android:id="@+id/radioButton4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/my" android:gravity="center" android:text="我" android:textColor="@android:color/darker_gray"/> </RadioGroup> </LinearLayout>
2.ViewPager需要適配器繼承于PagerAdapter
package com.example.cxy.viewpager.adapter; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * date:2017/3/7 * Created:陳簫陽(admin) */ public class MyViewPagerAdpter extends PagerAdapter { private List<View> mList; public MyViewPagerAdpter(List<View> list) { mList = list; } //返回視圖數(shù)量 @Override public int getCount() { return mList.size(); } //是否通過對(duì)象加載視圖 @Override public boolean isViewFromObject(View view, Object object) { return view == object; } //加載當(dāng)前頁面 @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(mList.get(position)); return mList.get(position);//View } //銷毀滑出視圖 @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mList.get(position)); } }
3.新建一個(gè)fragment包,在包中新建OneFragment類用于滑動(dòng)展示,新建布局文件fragmentone.xml并添加TextView用于添加不同頁面的內(nèi)容,共有四個(gè)這里只寫一個(gè)
OneFragment類
package com.example.cxy.viewpager.fragment; 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; import com.example.cxy.viewpager.R; /** * date:2017/3/7 * Created:陳簫陽(admin) */ public class OneFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmentone, null); return view; } }
fragmentone.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:textSize="30sp" android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:text="這是微信頁面"/> </LinearLayout>
4.編寫主類
package com.example.cxy.viewpager; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import com.example.cxy.viewpager.adapter.MyViewPagerAdpter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener { private ViewPager mViewPager; private List<View> mList; private RadioGroup mRadioGroup; private RadioButton weChatBtn, msgBtn, showBtn, myBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化所有控件 initView(); } private void initView() { //實(shí)例化ViewPager mViewPager = (ViewPager) findViewById(R.id.viewPager); //實(shí)例化Radiogroup mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup); //給RadioGroup添加監(jiān)聽 mRadioGroup.setOnCheckedChangeListener(this); //實(shí)例化RadioButton weChatBtn = (RadioButton) findViewById(R.id.radioButton1); msgBtn = (RadioButton) findViewById(R.id.radioButton2); showBtn = (RadioButton) findViewById(R.id.radioButton3); myBtn = (RadioButton) findViewById(R.id.radioButton4); //實(shí)例化List數(shù)組 mList = new ArrayList<>(); View view1 = LayoutInflater.from(this).inflate(R.layout.fragmentone, null); View view2 = LayoutInflater.from(this).inflate(R.layout.fragmenttwo, null); View view3 = LayoutInflater.from(this).inflate(R.layout.fragmentthree, null); View view4 = LayoutInflater.from(this).inflate(R.layout.fragmentfour, null); //把生成的每一個(gè)View對(duì)象添加到集合中 mList.add(view1); mList.add(view2); mList.add(view3); mList.add(view4); //實(shí)例化適配器 MyViewPagerAdpter adapter = new MyViewPagerAdpter(mList); //給ViewPager添加適配器 mViewPager.setAdapter(adapter); //給ViewPager添加監(jiān)聽事件 mViewPager.addOnPageChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //清理所有導(dǎo)航欄的狀態(tài) clearState(); switch (checkedId) { case R.id.radioButton1: //給ViewPager設(shè)置當(dāng)前布局 mViewPager.setCurrentItem(0); //給RadioButton設(shè)置文本顏色 weChatBtn.setTextColor(getResources().getColor(R.color.tvGreen)); //給RadioButton設(shè)置文本上方的圖片 weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.mess_t), null, null); break; case R.id.radioButton2: mViewPager.setCurrentItem(1); msgBtn.setTextColor(getResources().getColor(R.color.tvGreen)); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.call_t), null, null); break; case R.id.radioButton3: mViewPager.setCurrentItem(2); showBtn.setTextColor(getResources().getColor(R.color.tvGreen)); showBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.show_t), null, null); break; case R.id.radioButton4: mViewPager.setCurrentItem(3); myBtn.setTextColor(getResources().getColor(R.color.tvGreen)); myBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.my_t), null, null); break; } } //初始化底部導(dǎo)航欄 private void clearState() { weChatBtn.setTextColor(getResources().getColor(android.R.color.darker_gray)); msgBtn.setTextColor(getResources().getColor(android.R.color.darker_gray)); showBtn.setTextColor(getResources().getColor(android.R.color.darker_gray)); myBtn.setTextColor(getResources().getColor(android.R.color.darker_gray)); weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.mess_f), null, null); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.call_f), null, null); showBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.show_f), null, null); myBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.my), null, null); } //滑動(dòng)過程中的動(dòng)作 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } //選擇某個(gè)頁面松手后會(huì)被調(diào)用 @Override public void onPageSelected(int position) { //清理所有導(dǎo)航欄的狀態(tài) clearState(); switch (position) { //使用Switch拿到下標(biāo)定義當(dāng)滑動(dòng)到相應(yīng)位置小點(diǎn)顯示顏色 case 0: //當(dāng)頁面切換后,還要把當(dāng)前的導(dǎo)航欄變?yōu)榫G色 weChatBtn.setTextColor(getResources().getColor(R.color.tvGreen)); //設(shè)置文本的上方的圖片的,四個(gè)參數(shù)分別為,左、上、右、下 weChatBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.mess_t), null, null); break; case 1: msgBtn.setTextColor(getResources().getColor(R.color.tvGreen)); msgBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.call_t), null, null); break; case 2: showBtn.setTextColor(getResources().getColor(R.color.tvGreen)); showBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.show_t), null, null); break; case 3: myBtn.setTextColor(getResources().getColor(R.color.tvGreen)); myBtn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.my_t), null, null); break; } } //手指放上去,松開,拖動(dòng)都會(huì)被調(diào)用 @Override public void onPageScrollStateChanged(int state) { } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android控件系列之RadioButton與RadioGroup使用方法
- android RadioGroup的使用方法
- android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法
- Android編程開發(fā)之RadioGroup用法實(shí)例
- 讓Android中RadioGroup不顯示在輸入法上面的辦法
- Android編程單選項(xiàng)框RadioGroup綜合應(yīng)用示例
- Android開發(fā)之RadioGroup的簡(jiǎn)單使用與監(jiān)聽示例
相關(guān)文章
Android高級(jí)xml布局之輸入框EditText設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android高級(jí)xml布局之輸入框EditText設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關(guān)介紹
本篇文章小編為大家介紹,基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關(guān)介紹。需要的朋友參考下2013-04-04Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)檢測(cè)當(dāng)前電源狀態(tài)的方法,涉及Android針對(duì)當(dāng)前電源的電量、容量、伏數(shù)、溫度等的檢測(cè)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-11-11MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯
這篇文章主要為大家介紹了MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android使用SlidingPaneLayout 實(shí)現(xiàn)仿微信的滑動(dòng)返回
這篇文章主要介紹了Android使用SlidingPaneLayout 實(shí)現(xiàn)仿微信的滑動(dòng)返回,需要的朋友可以參考下2018-04-04Android編程中FileOutputStream與openFileOutput()的區(qū)別分析
這篇文章主要介紹了Android編程中FileOutputStream與openFileOutput()的區(qū)別,結(jié)合實(shí)例形式分析了FileOutputStream與openFileOutput()的功能,使用技巧與用法區(qū)別,需要的朋友可以參考下2016-02-02Android App中用Handler實(shí)現(xiàn)ViewPager頁面的自動(dòng)切換
這篇文章主要介紹了Android App中用Handler實(shí)現(xiàn)ViewPager頁面的自動(dòng)切換的方法,類似于相冊(cè)自動(dòng)播放,主要是切換后要提示當(dāng)前頁面所在的位置,需要的朋友可以參考下2016-05-05