




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】如何在Android項(xiàng)目中使用ViewPager對(duì)radiogroup進(jìn)行關(guān)聯(lián)
如何在Android項(xiàng)目中使用ViewPager對(duì)radiogroup進(jìn)行關(guān)聯(lián)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。AndroidViewPager與radiogroup實(shí)現(xiàn)關(guān)聯(lián)步驟1.實(shí)例化ViewPager2.通過(guò)LayoutInflater加載布局,返回View結(jié)果3.把生成的每一個(gè)View對(duì)象添加到List集合中4.實(shí)例化適配器,傳遞View集合5.在適配器中繼承自PagerAdapter,實(shí)現(xiàn)內(nèi)部的四個(gè)方法getCount();返回視圖的數(shù)量isViewFromObject();是否通過(guò)對(duì)象加載視圖View==objectinstantiateltem();加載當(dāng)前頁(yè)面(通過(guò)container.addView();添加視圖)返回個(gè)給用戶(hù)destroyItem();銷(xiāo)毀滑出的視圖(通過(guò)container.removerView();銷(xiāo)毀視圖)6.實(shí)例化每個(gè)RadioButton7.點(diǎn)擊每個(gè)RaidoButton時(shí),切換不同的頁(yè)面(viewPager.setCurrentltem(下標(biāo)))8.當(dāng)頁(yè)面切換后,還要把當(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)聽(tīng)需要addOnPagerChangeListener11.在onPagerSelected方法中,根據(jù)position頁(yè)面的下標(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="/apk/res/android"
xmlns:tools="/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:陳簫陽(yáng)(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();
}
//是否通過(guò)對(duì)象加載視圖
@Override
public
boolean
isViewFromObject(View
view,
Object
object)
{
return
view
==
object;
}
//加載當(dāng)前頁(yè)面
@Override
public
Object
instantiateItem(ViewGroup
container,
int
position)
{
container.addView(mList.get(position));
return
mList.get(position);//View
}
//銷(xiāo)毀滑出視圖
@Override
public
void
destroyItem(ViewGroup
container,
int
position,
Object
object)
{
container.removeView(mList.get(position));
}
}3.新建一個(gè)fragment包,在包中新建OneFragment類(lèi)用于滑動(dòng)展示,新建布局文件fragmentone.xml并添加TextView用于添加不同頁(yè)面的內(nèi)容,共有四個(gè)這里只寫(xiě)一個(gè)OneFragment類(lèi)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:陳簫陽(yáng)(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="/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="這是微信頁(yè)面"/>
</LinearLayout>4.編寫(xiě)主類(lèi)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)聽(tīng)
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)聽(tīng)事件
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)過(guò)程中的動(dòng)作
@Override
public
void
onPageScrolled(int
position,
float
positionOffset,
int
positionOffsetPixels)
{
}
//選擇某個(gè)頁(yè)面松手后會(huì)被調(diào)用
@Override
public
void
onPageSelected(int
position)
{
//清理所有導(dǎo)航欄的狀態(tài)
clearState();
switch
(position)
{
//使用Switch拿到下
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030冰柜行業(yè)市場(chǎng)占有率及投資前景評(píng)估規(guī)劃報(bào)告
- 2025至2030苯基聚苯產(chǎn)業(yè)市場(chǎng)深度調(diào)研及發(fā)展趨勢(shì)與發(fā)展趨勢(shì)分析與未來(lái)投資戰(zhàn)略咨詢(xún)研究報(bào)告
- 杞縣美食活動(dòng)策劃方案
- 普法平安宣傳活動(dòng)方案
- 柏拉圖婚禮活動(dòng)方案
- 暑期牙科優(yōu)惠活動(dòng)方案
- 最強(qiáng)班組活動(dòng)方案
- 機(jī)關(guān)零碳活動(dòng)方案
- 杭州水上活動(dòng)方案
- 機(jī)構(gòu)招生策劃活動(dòng)方案
- 2025年 北京門(mén)頭溝大峪街道社區(qū)儲(chǔ)備人才招募考試試題附答案
- 規(guī)范申報(bào)專(zhuān)題培訓(xùn)-課件
- 亞馬遜品牌授權(quán)書(shū)(英文模板)
- 《現(xiàn)代漢語(yǔ)修辭》PPT課件(完整版)
- DB52∕T 046-2018 貴州省建筑巖土工程技術(shù)規(guī)范
- AZ91D鎂合金半固態(tài)成型
- 100以?xún)?nèi)不進(jìn)位不退位加減法練習(xí)習(xí)題(直接打印)
- 巨光Y型空氣消毒器
- 基于MATLAB的直流電機(jī)雙閉環(huán)調(diào)速系統(tǒng)的設(shè)計(jì)與仿真
- MPACC案例《作業(yè)成本法案例分析》
- 大健康生活館運(yùn)營(yíng)手冊(cè)
評(píng)論
0/150
提交評(píng)論