MVP框架登入展示資料
阿新 • • 發佈:2018-12-20
//清單檔案 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.moni2"> //這些屬於許可權 <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name=".App.App" (這個容易忘記 必須寫) android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SendActivity"></activity> </application> </manifest>
//登入的佈局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <TextView android:text="手機號:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="220dp" android:layout_marginLeft="30dp" /> <EditText android:id="@+id/name" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:layout_centerHorizontal="true" android:layout_marginTop="200dp" /> <TextView android:text="密 碼:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="270dp" android:layout_marginLeft="35dp" /> <EditText android:id="@+id/mima" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:layout_centerHorizontal="true" android:layout_marginTop="250dp" /> <CheckBox android:id="@+id/check_jz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="記住密碼" android:layout_alignLeft="@id/mima" android:layout_centerHorizontal="true" android:layout_marginTop="300dp" /> <CheckBox android:id="@+id/check_zd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自動登入" android:layout_alignRight="@id/mima" android:layout_centerHorizontal="true" android:layout_marginTop="300dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/rectangle" android:layout_centerHorizontal="true" android:text="登入" android:layout_marginBottom="40dp" /> </RelativeLayout>
//滑動佈局 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".SendActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" /> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:orientation="horizontal" > <TextView android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" android:layout_weight="1" /> <TextView android:id="@+id/t2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <RadioButton android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="我的資料" android:gravity="center" android:button="@null" /> <RadioButton android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="我的名片" android:gravity="center" android:button="@null" /> </RadioGroup> </LinearLayout> </android.support.constraint.ConstraintLayout>
//第一個fragment載入資料佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#44ff44"
tools:context=".fragment.Bfragment">
<!-- TODO: Update blank fragment layout -->
<com.example.xlist.me.maxwin.view.XListView
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.xlist.me.maxwin.view.XListView>
</FrameLayout>
//二維碼佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#00f"
tools:context=".fragment.Cfragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出登入"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
</FrameLayout>
<?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/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="的哇啊我發我午飯晚飯"
/>
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
android:layout_marginLeft="120dp"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
android:layout_marginLeft="240dp"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
/>
</RelativeLayout>
adapter介面卡
package com.example.moni2.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.moni2.ImageConfs;
import com.example.moni2.R;
import com.example.moni2.bean.Hzj;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends BaseAdapter {
List<Hzj.DataBean> list;
Context mContext;
public MyAdapter(Context context) {
mContext = context;
list = new ArrayList<>();
}
public void setDats(List<Hzj.DataBean> dateBean){
this.list.clear();
if(dateBean!=null){
list.addAll(dateBean);
notifyDataSetChanged();
}
}
public void addDats(List<Hzj.DataBean> dateBean){
if(dateBean!=null){
list.addAll(dateBean);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolde vh;
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.xlist_text,null);
vh = new ViewHolde(convertView);
}else{
vh = (ViewHolde) convertView.getTag();
}
Hzj.DataBean data = (Hzj.DataBean) getItem(position);
vh.BindDate(data);
return convertView;
}
class ViewHolde{
TextView t1;
ImageView image1;
ImageView image2;
ImageView image3;
public ViewHolde(View view) {
t1 = view.findViewById(R.id.t1);
image1 = view.findViewById(R.id.image1);
image2 = view.findViewById(R.id.image2);
image3 = view.findViewById(R.id.image3);
view.setTag(this);
}
public void BindDate(Hzj.DataBean data){
t1.setText(data.getTitle());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s(),image1,ImageConfs.getMan(mContext));
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s02(),image2,ImageConfs.getMan(mContext));
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s03(),image3,ImageConfs.getMan(mContext));
}
}
}
app註冊Imageloader
package com.example.moni2.App;
import android.app.Application;
import com.example.moni2.ImageConfs;
import com.nostra13.universalimageloader.core.ImageLoader;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(ImageConfs.getcon(this));
}
}
二維碼初始化註冊
package com.example.moni2.App;
import android.app.Application;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
public class ErApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ZXingLibrary.initDisplayOpinion(this);
}
}
//用到的請求資料
package com.example.moni2.bean;
import java.util.List;
public class Hzj {
private String msg;
private int code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* uniquekey : 1326bf3c4505783d4a964f81b6220e9d
* title : 美國被特朗普坑慘了!又有3個鐵桿盟友公開唱對臺戲!
* date : 2018-05-12 07:25
* category : 頭條
* author_name : 大國軍情
* url : http://mini.eastday.com/mobile/180512072544020.html
* thumbnail_pic_s : http://04.imgmini.eastday.com/mobile/20180512/20180512_fe1bf1b6ec00098f9455c84dc81e6763_cover_mwpm_03200403.jpg
* thumbnail_pic_s02 : http://04.imgmini.eastday.com/mobile/20180512/20180512_3f636fca330f2311583896aed3185196_cover_mwpm_03200403.jpg
* thumbnail_pic_s03 : http://04.imgmini.eastday.com/mobile/20180512/20180512_793fab03dadcd417bf2d8e5e1e48a0f5_cover_mwpm_03200403.jpg
*/
private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public void setThumbnail_pic_s(String thumbnail_pic_s) {
this.thumbnail_pic_s = thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
this.thumbnail_pic_s02 = thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
this.thumbnail_pic_s03 = thumbnail_pic_s03;
}
}
}
//請求資料的fragment
package com.example.moni2.fragment;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.moni2.R;
import com.example.moni2.adapter.MyAdapter;
import com.example.moni2.bean.Hzj;
import com.example.xlist.me.maxwin.view.XListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
public class Bfragment extends Fragment {
private XListView xlv;
MyAdapter ha;
private int page =1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bfragment, container, false);
xlv = view.findViewById(R.id.xlv);
ha = new MyAdapter(getActivity());
// Inflate the layout for this fragment
xlv.setAdapter(ha);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page =1;
Loadehzj(page);
}
@Override
public void onLoadMore() {
Loadehzj(page);
}
});
Loadehzj(page);
return view;
}
private void stopdf() {
page++;
xlv.stopRefresh();
xlv.stopLoadMore();
}
String path = "http://www.xieast.com/api/news/news.php?page=1";
@SuppressLint("StaticFieldLeak")
private void Loadehzj(int page) {
String Urlwith = path +page;
new AsyncTask<String, String,List<Hzj.DataBean>>(){
@Override
protected List<Hzj.DataBean> doInBackground(String... strings) {
Hzj dian = null;
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("GET");
int request = httpURLConnection.getResponseCode();
if(request == 200){
String str = String2(httpURLConnection.getInputStream());
dian = new Gson().fromJson(str,Hzj.class);
}
} catch (Exception e) {
e.printStackTrace();
}
return dian == null ? null : dian.getData();
}
@Override
protected void onPostExecute(List<Hzj.DataBean> dateBean) {
if(dateBean == null){
Toast.makeText(getActivity(),"下拉資料失敗",Toast.LENGTH_SHORT).show();
}
updateset(dateBean);
stopdf();
super.onPostExecute(dateBean);
}
}.execute(Urlwith);
}
private void updateset(List<Hzj.DataBean> dateBean) {
if(page == 1){
ha.setDats(dateBean);
}else{
ha.addDats(dateBean);
}
}
private String String2(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String text = null;
while((text=bufferedReader.readLine())!=null){
stringBuilder.append(text);
}
bufferedReader.close();
return stringBuilder.toString();
}
}
二維碼的fragment
package com.example.moni2.fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import com.example.moni2.MainActivity;
import com.example.moni2.R;
import com.uuzuche.lib_zxing.activity.CodeUtils;
public class Cfragment extends Fragment {
public SharedPreferences mSharedPreferences;
private ImageView image;
private Button btn;
SharedPreferences.Editor editor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cfragment, container, false);
// Inflate the layout for this fragment
image = view.findViewById(R.id.image);
btn = view.findViewById(R.id.btn);
mSharedPreferences = getActivity().getSharedPreferences("KWY",Context.MODE_PRIVATE);
String uname = mSharedPreferences.getString("uname", "");
Bitmap mBitmap = CodeUtils.createImage(uname, 200, 200,null);
image.setImageBitmap(mBitmap);
editor = mSharedPreferences.edit();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.clear();
editor.commit();
startActivity(new Intent(getActivity(),MainActivity.class));
getActivity().finish();
}
});
return view;
}
}
//建立P層 使用MVP框架搭建
package com.example.moni2.presenter;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.CheckBox;
import com.example.moni2.Person;
public class Presenter {
public boolean ispan(Person person, SharedPreferences mSharedPreferences,SharedPreferences.Editor editor,CheckBox check_jz,String uname,String umima,CheckBox check_zd){
if(TextUtils.isEmpty(person.name) || TextUtils.isEmpty(person.mima)){
mOnpresenter.shibai();
}else if(person.name.length() != 11){
mOnpresenter.geshi();
}else{
if(person.name.equals("13800138000") && person.mima.equals("123456")){
if(check_jz.isChecked()){
editor = mSharedPreferences.edit();
editor.putBoolean("ischeck",true);
editor.putString("uname",uname);
editor.putString("umima",umima);
editor.commit();
}else{
editor = mSharedPreferences.edit();
editor.putBoolean("ischeck",false);
editor.putString("uname",uname);
editor.putString("umima",umima);
editor.commit();
}
if(check_zd.isChecked()){
editor = mSharedPreferences.edit();
editor.putBoolean("isdeng",true);
editor.commit();
}
mOnpresenter.success();
}else{
mOnpresenter.zhmmbd();
}
}
return false;
}
public interface onpresenter{
void success();
void shibai();
void geshi();
void zhmmbd();
}
public onpresenter mOnpresenter;
public void setOnpresenter(onpresenter onpresenter){
this.mOnpresenter = onpresenter;
}
}
//
package com.example.moni2;
import android.content.Context;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
/**
* date:2018/11/10
* author:霍鉦傑(叮咯呤咚嗆)
* function:
*/
public class ImageConfs {
public static ImageLoaderConfiguration getcon(Context context){
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.memoryCacheSizePercentage(10)
.diskCacheSize(50*1024*1024)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.writeDebugLogs()
.build();
return configuration;
}
public static DisplayImageOptions getMan(Context context){
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher_background)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new CircleBitmapDisplayer())
.build();
return options;
}
}
//主函式
package com.example.moni2;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.moni2.presenter.Presenter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText name;
private EditText mima;
private CheckBox check_jz;
private CheckBox check_zd;
Presenter mPresenter;
private Button btn;
SharedPreferences.Editor editor;
SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
name = (EditText) findViewById(R.id.name);
mima = (EditText) findViewById(R.id.mima);
check_jz = (CheckBox) findViewById(R.id.check_jz);
check_zd = (CheckBox) findViewById(R.id.check_zd);
mPresenter = new Presenter();
btn = (Button) findViewById(R.id.btn);
mSharedPreferences = getSharedPreferences("KWY",MODE_PRIVATE);
mPresenter.setOnpresenter(new Presenter.onpresenter() {
@Override
public void success() {
Toast.makeText(MainActivity.this,"登陸成功",Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,SendActivity.class));
finish();
}
@Override
public void shibai() {
Toast.makeText(MainActivity.this,"賬號密碼不能為空",Toast.LENGTH_SHORT).show();
}
@Override
public void geshi() {
Toast.makeText(MainActivity.this,"手機號碼格式不對",Toast.LENGTH_SHORT).show();
}
@Override
public void zhmmbd() {
Toast.makeText(MainActivity.this,"賬號或密碼錯誤",Toast.LENGTH_SHORT).show();
}
});
deng();
btn.setOnClickListener(this);
}
private void deng() {
boolean ischeck = mSharedPreferences.getBoolean("ischeck",false);
String uname = mSharedPreferences.getString("uname" ,"");
String umima = mSharedPreferences.getString("umima" ,"");
if(ischeck){
name.setText(uname);
mima.setText(umima);
check_jz.setChecked(true);
}
//自動登入
boolean isdeng =mSharedPreferences.getBoolean("isdeng",false);
if(isdeng){
startActivity(new Intent(MainActivity.this,SendActivity.class));
finish();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
String uname = name.getText().toString().trim();
String umima = mima.getText().toString().trim();
final Person person = new Person();
person.name = uname;
person.mima = umima;
mPresenter.ispan(person,mSharedPreferences,editor,check_jz,uname,umima,check_zd);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mPresenter = null;
}
}
package com.example.moni2;
public class Person {
public String name;
public String mima;
}
//調轉到另一個activity裡面實現滑動
package com.example.moni2;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.moni2.fragment.Bfragment;
import com.example.moni2.fragment.Cfragment;
public class SendActivity extends AppCompatActivity {
private RadioGroup group;
private TextView t1,t2;
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
group = findViewById(R.id.group);
viewpager = findViewById(R.id.viewpager);
t1 = findViewById(R.id.t1);
t2 = findViewById(R.id.t2);
initview();
}
private void initview() {
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
Bfragment bFragment = new Bfragment();
return bFragment;
case 1:
Cfragment cFragment = new Cfragment();
return cFragment;
}
return null;
}
@Override
public int getCount() {
//總共3個fragment
return 2;
}
});
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.btn1:
viewpager.setCurrentItem(0);
break;
case R.id.btn2:
viewpager.setCurrentItem(1);
break;
}
}
});
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
switch (i){
case 0:
group.check(R.id.btn1);
//給每個條目設定顏色
t1.setBackgroundColor(Color.BLACK);
t2.setBackgroundColor(Color.WHITE);
break;
case 1:
group.check(R.id.btn2);
t2.setBackgroundColor(Color.BLACK);
t1.setBackgroundColor(Color.WHITE);
break;
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
}