【安卓笔记】ViewPager+Fragment布局中的Fragment数据更新
<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=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
package com.example.viewpagerdemo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Tab3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab3,null); } }
<?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:background="#0000ff" android:orientation="vertical" > <TextView android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="我是第三个界面" /> </LinearLayout>
package com.example.viewpagerdemo2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { private ViewPager vPager = null; private static final int TAB_COUNT = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vPager = (ViewPager) findViewById(R.id.viewpager); vPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); } public class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new Tab1(); case 1: return new Tab2(); case 2: return new Tab3(); } return null; } @Override public int getCount() { return TAB_COUNT; } } }
getActivity().getSupportFragmentManager().findFragmentById(R.id.fg1);首先我们在Mainctivity中添加一个方法,用于获取Adapter:
/** * 获取适配器 * @return */ public MyPagerAdapter getAdapter() { return adapter; }
<?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:background="#ff0000" android:orientation="vertical" > <TextView android:id="@+id/tab1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第一个界面--->已更新" /> <TextView android:id="@+id/tab1_ss" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第一个界面--->已更新" /> </LinearLayout>然后在选项卡2中添加一个按钮,布局就不贴了:
package com.example.viewpagerdemo2; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; public class Tab2 extends Fragment { private static final String TAG = "Tab2"; private Button but = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab2,null); but = (Button) view.findViewById(R.id.but); Log.i(TAG,"TAB2 CREATED..."); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = Tab2.this.getActivity().getSharedPreferences(Tab1.CONTENT_VIEW,Context.MODE_PRIVATE); boolean state = sp.getBoolean(Tab1.IS_UPDATE, false); Toast.makeText(getActivity(),state+"",0).show(); Editor editor = sp.edit(); editor.putBoolean(Tab1.IS_UPDATE,!state); editor.commit(); MainActivity a = (MainActivity) getActivity(); a.getAdapter().notifyDataSetChanged(); } }); } }
package com.example.viewpagerdemo2; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Tab1 extends Fragment { public static final String CONTENT_VIEW = "content_view"; public static final String IS_UPDATE = "is_update"; private static final String TAG = "Tab1"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG,"TAB1 CREATED..."); SharedPreferences sp =getActivity().getSharedPreferences(CONTENT_VIEW,Context.MODE_PRIVATE); boolean is_update = sp.getBoolean(IS_UPDATE,false); View view = null; if(is_update) { view = inflater.inflate(R.layout.tab1_new, null); }else { view = inflater.inflate(R.layout.tab1,null); } return view; } }
/** * 页面内容集合 */ private List<Fragment> fgs = null;然后为其增加set方法:
/** * 重新设置页面内容 * @param items */ public void setPagerItems(List<Fragment> items) { if (items != null) { for (int i = 0; i < fgs.size(); i++) { mFragmentManager.beginTransaction().remove(fgs.get(i)).commit(); } fgs = items; } }
/** * @author Rowand jj *回调接口 */ public interface OnReloadListener { public void onReload(); }再添加一个设置回调接口的方法:
public void setOnReloadListener(OnReloadListener listener) { this.mListener = listener; }最后再提供一个reLoad方法:
/** *当页面数据发生改变时你可以调用此方法 * * 重新载入数据,具体载入信息由回调函数实现 */ public void reLoad() { if(mListener != null) { mListener.onReload(); } this.notifyDataSetChanged();//不可少,通知系统数据改变 }Activity在设置适配器时,先为适配器实现添加一个回调函数,在回调方法中调用setPagerItems方法重新设置数据。
adapter.setOnReloadListener(new OnReloadListener() { @Override public void onReload() { fgs = null; List<Fragment> list = new ArrayList<Fragment>(); list.add(new Tab1()); list.add(new Tab2()); list.add(new Tab3()); adapter.setPagerItems(list); } });再刚才的界面2的Fragment的按钮响应事件中调用:
MainActivity a = (MainActivity) getActivity(); a.getAdapter().reLoad();
package com.example.viewpagerdemo2; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.util.Log; /** * @author Rowand jj * 页面适配器 */ public class MyPagerAdapter extends FragmentPagerAdapter { private static final String TAG = "YiPageAdapter"; /** * 页面内容集合 */ private List<Fragment> fgs = null; private FragmentManager mFragmentManager; /** * 当数据发生改变时的回调接口 */ private OnReloadListener mListener; public MyPagerAdapter(FragmentManager fm, List<Fragment> fgs) { super(fm); this.fgs = fgs; mFragmentManager = fm; } @Override public Fragment getItem(int index) { Log.i(TAG,"ITEM CREATED..."); return fgs.get(index); } @Override public int getCount() { return fgs.size();// 返回选项卡总数 } @Override public int getItemPosition(Object object) { return POSITION_NONE; } /** * 重新设置页面内容 * @param items */ public void setPagerItems(List<Fragment> items) { if (items != null) { for (int i = 0; i < fgs.size(); i++) { mFragmentManager.beginTransaction().remove(fgs.get(i)).commit(); } fgs = items; } } /** *当页面数据发生改变时你可以调用此方法 * * 重新载入数据,具体载入信息由回调函数实现 */ public void reLoad() { if(mListener != null) { mListener.onReload(); } this.notifyDataSetChanged(); } public void setOnReloadListener(OnReloadListener listener) { this.mListener = listener; } /** * @author Rowand jj *回调接口 */ public interface OnReloadListener { public void onReload(); } }MainActivity:
package com.example.viewpagerdemo2; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import com.example.viewpagerdemo2.MyPagerAdapter.OnReloadListener; public class MainActivity extends FragmentActivity { private ViewPager vPager = null; private static final int TAB_COUNT = 3; private MyPagerAdapter adapter = null; private List<Fragment> fgs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vPager = (ViewPager) findViewById(R.id.viewpager); fgs = new ArrayList<Fragment>(); fgs.add(new Tab1()); fgs.add(new Tab2()); fgs.add(new Tab3()); adapter = new MyPagerAdapter(getSupportFragmentManager(), fgs); adapter.setOnReloadListener(new OnReloadListener() { @Override public void onReload() { fgs = null; List<Fragment> list = new ArrayList<Fragment>(); list.add(new Tab1()); list.add(new Tab2()); list.add(new Tab3()); adapter.setPagerItems(list); } }); vPager.setAdapter(adapter); } /** * 获取适配器 * @return */ public MyPagerAdapter getAdapter() { return adapter; } }仅仅是提供一种思路,如果大家有好的思路请留言。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。