问题描述
我想更新一个片段点击一个TextView上另一个片段的按钮。
其实我实现了回调函数的活动和它的工作,因为logcat的报告说,在TextView的文本已被更改。问题是,在第一个片段显示的TextView中不会更新到新的价值!这就像片段需要更新或东西...
这里的ActionBarTabsPager活动code:
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.app.FragmentManager;
进口android.app.FragmentManager.OnBackStackChangedListener;
进口android.content.Context;
进口android.os.Build;
进口android.os.Bundle;
进口android.support.v4.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.app.FragmentTransaction;
进口android.support.v4.app.ActionBar.Tab;
进口android.support.v4.app.SupportActivity;
进口android.support.v4.view.ViewPager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.TextView;
/ **
*演示相结合的操作栏带有ViewPager实现标签的UI
*该选项卡之间切换,并允许用户执行水平
*笔势选项卡之间移动。
* /
公共类ActionBarTabsPager扩展FragmentActivity实现SecondFragment.OnButtonClickedXListener {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
FragmentManager FM;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.actionbar_tabs_pager);
如果(savedInstanceState == NULL){
片段newFragment =新FirstFragment();
FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
。ft.add(R.id.abs__custom,newFragment,FirstFragment)提交();
片段newFragment2 =新FirstFragment();
FragmentTransaction平方英尺= getSupportFragmentManager()的BeginTransaction()。
。ft2.add(R.id.abs__custom,newFragment2,SecondFragment)提交();
}
。getSupportActionBar()setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab TAB1 = getSupportActionBar()newTab()的setText(片段1)。。;
ActionBar.Tab TAB2 = getSupportActionBar()newTab()的setText(片段2)。。;
//ActionBar.Tab TAB3 = getSupportActionBar()newTab()的setText(片段1)。。;
//ActionBar.Tab TAB4 = getSupportActionBar()newTab()的setText(片段2)。。;
mViewPager =(ViewPager)findViewById(R.id.pager);
mTabsAdapter =新TabsAdapter(这一点,getSupportActionBar(),mViewPager);
mTabsAdapter.addTab(TAB1,FirstFragment.class);
mTabsAdapter.addTab(TAB2,SecondFragment.class); // LoaderCursorSupport.CursorLoaderListFragment.class);
//mTabsAdapter.addTab(tab3,FirstFragment.class); // LoaderCustomSupport.AppListFragment.class);
//mTabsAdapter.addTab(tab4,SecondFragment.class); // LoaderThrottleSupport.ThrottledLoaderListFragment.class);
如果(savedInstanceState!= NULL){
。getSupportActionBar()setSelectedNavigationItem(savedInstanceState.getInt(指数));
}
}
@覆盖
保护无效的onSaveInstanceState(包outState){
super.onSaveInstanceState(outState);
outState.putInt(指数,getSupportActionBar()getSelectedNavigationIndex());
}
/ **
*这是实现标签和所有的管理辅助类
*连接ViewPager关联TabHost的详细信息。它依赖于
* 招。通常情况下一个选项卡主机有一个简单的API,用于提供视图或
*意图,每个标签将显示。这是不够的,用于切换
*页之间。因此,而不是我们做的标签主机的内容的一部分
* 0dp高(它未示出)和TabsAdapter提供它自己虚设
*以显示为选项卡的内容。它监听变化的标签,并采取
*照顾切换到正确的分页中ViewPager每当选择
*标签的变化。
* /
公共类TabsAdapter扩展FragmentPagerAdapter实现ViewPager.OnPageChangeListener,ActionBar.TabListener {
私人最终语境mContext;
私人最终动作条mActionBar;
私人最终ViewPager mViewPager;
私人最终的ArrayList<字符串> mTabs =新的ArrayList<字符串>();
公共TabsAdapter(FragmentActivity活动,动作条动作条,ViewPager寻呼机){
超级(activity.getSupportFragmentManager());
mContext =活动;
mActionBar =动作条;
mViewPager =寻呼机;
mViewPager.setAdapter(本);
mViewPager.setOnPageChangeListener(本);
}
公共无效addTab(ActionBar.Tab选项卡,类<> CLSS){
mTabs.add(clss.getName());
mActionBar.addTab(tab.setTabListener(本));
notifyDataSetChanged();
}
@覆盖
公众诠释getCount将(){
返回mTabs.size();
}
@覆盖
公共片段的getItem(INT位置){
返回Fragment.instantiate(mContext,mTabs.get(位置),NULL);
}
@覆盖
公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){
}
@覆盖
公共无效onPageSelected(INT位置){
mActionBar.setSelectedNavigationItem(位置);
}
@覆盖
公共无效onPageScrollStateChanged(INT状态){
}
@覆盖
公共无效onTabSelected(TAB键,FragmentTransaction英尺){
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabReselected(TAB键,FragmentTransaction英尺){
}
@覆盖
公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
}
}
@覆盖
公共无效OnButtonClickedX(视图v){
如果(ⅴ== findViewById(R.id.button1)){
Log.i(引发,引发);
FirstFragment FF =(FirstFragment)getSupportFragmentManager()findFragmentByTag(FirstFragment)。
查看根= ff.getView();
TextView的电视=(TextView中)root.findViewById(R.id.textView1);
Log.i(前编辑短信,+ tv.getText());
tv.setText(修改时间);
Log.i(后编辑短信,+ tv.getText());
}
// TODO自动生成方法存根
}
}
FirstFragment:
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类FirstFragment扩展片段{
INT MNUM;
/ **
*创建FirstFragment的新实例,提供NUM
*作为参数。
* /
静态FirstFragment的newInstance(INT NUM){
FirstFragment F =新FirstFragment();
//供应NUM输入作为参数。
捆绑的args =新包();
args.putInt(民,NUM);
f.setArguments(参数);
返回F;
}
/ **
*当创建,检索它的参数此实例的数量。
* /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
MNUM = getArguments()!= NULL? getArguments()调用getInt(民):1。
}
/ **
*片段的用户界面是只是显示它的一个简单的文本视图
*实例号。
* /
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.myfrag1,集装箱,假);
返回伏;
}
}
SecondFragment:
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.SupportActivity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
公共类SecondFragment扩展片段{
INT MNUM;
OnButtonClickedXListener mListener;
/ **
*创建CountingFragment的新实例,提供NUM
*作为参数。
* /
静态SecondFragment的newInstance(INT NUM){
SecondFragment F =新SecondFragment();
//供应NUM输入作为参数。
捆绑的args =新包();
args.putInt(民,NUM);
f.setArguments(参数);
返回F;
}
/ **
*当创建,检索它的参数此实例的数量。
* /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
MNUM = getArguments()!= NULL? getArguments()调用getInt(民):1。
}
/ **
*片段的用户界面是只是显示它的一个简单的文本视图
*实例号。
* /
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.myfrag2,集装箱,假);
查看按钮1 = v.findViewById(R.id.button1);
button1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mListener.OnButtonClickedX(五);
// TODO自动生成方法存根
}
});
返回伏;
}
公共接口OnButtonClickedXListener {
公共无效OnButtonClickedX(查看V);
}
@覆盖
公共无效onAttach(SupportActivity活动){
// TODO自动生成方法存根
super.onAttach(活动);
尝试 {
mListener =(OnButtonClickedXListener)的活动;
}赶上(ClassCastException异常E){
抛出新ClassCastException异常(activity.toString()+必须实现OnButtonClickedXListener);
}
}
}
myfrag1.xml:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=中心
机器人:文本=无字符串
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
< / LinearLayout中>
myfrag2.xml:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:重力=中心
机器人:文本=按钮/>
< / LinearLayout中>
编辑:
甚至变为0 ContainerViewId在ft.add()的最终渲染不会影响的。所以我想呈现由
管理 mTabsAdapter.addTab(TAB1,FirstFragment.class);
mTabsAdapter.addTab(TAB2,SecondFragment.class);
这个问题还是一样的,反正。
下面的 actionbar_tabs_pager.xml
< XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.view.ViewPager的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /寻呼机
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
/>
解决了!
重写 instantiateItem()在TabsAdapter并添加ViewPager作为ContainerViewID在FragmentTransaction了!
下面是功能全FragmentActivity!
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.app.FragmentManager;
进口android.app.FragmentManager.OnBackStackChangedListener;
进口android.content.Context;
进口android.os.Build;
进口android.os.Bundle;
进口android.support.v4.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.app.FragmentTransaction;
进口android.support.v4.app.ActionBar.Tab;
进口android.support.v4.app.SupportActivity;
进口android.support.v4.view.ViewPager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.TextView;
/ **
*演示相结合的操作栏带有ViewPager实现标签的UI
*该选项卡之间切换,并允许用户执行水平
*笔势选项卡之间移动。
* /
公共类ActionBarTabsPager扩展FragmentActivity实现SecondFragment.OnButtonClickedXListener {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
FragmentManager FM;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
Log.i(OnCreate中START,在OnCreate START);
的setContentView(R.layout.actionbar_tabs_pager);
如果(savedInstanceState == NULL){
片段newFragment =新FirstFragment();
片段newFragment2 =新SecondFragment();
FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
ft.add(R.id.pager,newFragment,FirstFragment);
ft.add(R.id.pager,newFragment2,SecondFragment);
ft.commit();
}
。getSupportActionBar()setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab TAB1 = getSupportActionBar()newTab()的setText(片段1)。。;
ActionBar.Tab TAB2 = getSupportActionBar()newTab()的setText(片段2)。。;
//ActionBar.Tab TAB3 = getSupportActionBar()newTab()的setText(片段1)。。;
//ActionBar.Tab TAB4 = getSupportActionBar()newTab()的setText(片段2)。。;
mViewPager =(ViewPager)findViewById(R.id.pager);
mTabsAdapter =新TabsAdapter(这一点,getSupportActionBar(),mViewPager);
mTabsAdapter.addTab(TAB1,FirstFragment.class);
mTabsAdapter.addTab(TAB2,SecondFragment.class); // LoaderCursorSupport.CursorLoaderListFragment.class);
//mTabsAdapter.addTab(tab3,FirstFragment.class); // LoaderCustomSupport.AppListFragment.class);
//mTabsAdapter.addTab(tab4,SecondFragment.class); // LoaderThrottleSupport.ThrottledLoaderListFragment.class);
如果(savedInstanceState!= NULL){
。getSupportActionBar()setSelectedNavigationItem(savedInstanceState.getInt(指数));
}
Log.i(OnCreate中END,在OnCreate END);
}
@覆盖
保护无效的onSaveInstanceState(包outState){
Log.i(的onSaveInstanceState START,的onSaveInstanceState START);
super.onSaveInstanceState(outState);
outState.putInt(指数,getSupportActionBar()getSelectedNavigationIndex());
Log.i(的onSaveInstanceState END,的onSaveInstanceState END);
}
/ **
*这是实现标签和所有的管理辅助类
*连接ViewPager关联TabHost的详细信息。它依赖于
* 招。通常情况下一个选项卡主机有一个简单的API,用于提供视图或
*意图,每个标签将显示。这是不够的,用于切换
*页之间。因此,而不是我们做的标签主机的内容的一部分
* 0dp高(它未示出)和TabsAdapter提供它自己虚设
*以显示为选项卡的内容。它监听变化的标签,并采取
*照顾切换到正确的分页中ViewPager每当选择
*标签的变化。
* /
公共类TabsAdapter扩展FragmentPagerAdapter实现ViewPager.OnPageChangeListener,ActionBar.TabListener {
私人最终语境mContext;
私人最终动作条mActionBar;
私人最终ViewPager mViewPager;
私人最终的ArrayList<字符串> mTabs =新的ArrayList<字符串>();
私人FragmentTransaction mCurTransaction = NULL;
公共TabsAdapter(FragmentActivity活动,动作条动作条,ViewPager寻呼机){
超级(activity.getSupportFragmentManager());
mContext =活动;
mActionBar =动作条;
mViewPager =寻呼机;
mViewPager.setAdapter(本);
mViewPager.setOnPageChangeListener(本);
}
公共无效addTab(ActionBar.Tab选项卡,类<> CLSS){
Log.i(addTab,addTab);
mTabs.add(clss.getName());
mActionBar.addTab(tab.setTabListener(本));
notifyDataSetChanged();
}
@覆盖
公众诠释getCount将(){
返回mTabs.size();
}
@覆盖
公共对象instantiateItem(View容器,INT位置){
如果(mCurTransaction == NULL){
mCurTransaction = getSupportFragmentManager()的BeginTransaction()。
}
// TODO自动生成方法存根
片段片段=的getItem(位置);
如果(片段!= NULL){
Log.i(碎片找到!,碎片找到!+ fragment.getTag());
mCurTransaction.attach(片段);
}
返回片段; // super.instantiateItem(集装箱,位置);
}
@覆盖
公共片段的getItem(INT位置){
Log.i(的getItem,的getItem);
如果(位置== 0)
{Log.i(位置= 0,位置= 0);
返回getSupportFragmentManager()findFragmentByTag(FirstFragment);}
否则,如果(位置== 1)
{Log.i(位置= 1,位置= 1);
返回getSupportFragmentManager()findFragmentByTag(SecondFragment);}
否则返回NULL; // Fragment.instantiate(mContext,mTabs.get(位置),NULL);
}
@覆盖
公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){
}
@覆盖
公共无效onPageSelected(INT位置){
mActionBar.setSelectedNavigationItem(位置);
}
@覆盖
公共无效onPageScrollStateChanged(INT状态){
}
@覆盖
公共无效onTabSelected(TAB键,FragmentTransaction英尺){
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabReselected(TAB键,FragmentTransaction英尺){
}
@覆盖
公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
}
}
@覆盖
公共无效OnButtonClickedX(视图v){
如果(ⅴ== findViewById(R.id.button1)){
Log.i(引发,引发);
FirstFragment FF =(FirstFragment)getSupportFragmentManager()findFragmentByTag(FirstFragment)。
查看根= ff.getView();
TextView的电视=(TextView中)root.findViewById(R.id.textView1);
Log.i(前编辑短信,+ tv.getText());
tv.setText(修改时间);
Log.i(后编辑短信,+ tv.getText());
}
// TODO自动生成方法存根
}
}
由于vbsteven!
I am trying to update a TextView in a Fragment clicking on a button on another Fragment.
Actually I implemented the callback function to the Activity and it does work, since the Logcat reports that the text in the TextView has been changed. The problem is the Textview shown in the first fragment is not updated to the new value! It's like the Fragment needs to be updated or something...
Here's the ActionBarTabsPager activity code:
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentManager.OnBackStackChangedListener;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.SupportActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;
/**
* Demonstrates combining the action bar with a ViewPager to implement a tab UI
* that switches between tabs and also allows the user to perform horizontal
* flicks to move between the tabs.
*/
public class ActionBarTabsPager extends FragmentActivity implements SecondFragment.OnButtonClickedXListener{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar_tabs_pager);
if (savedInstanceState == null) {
Fragment newFragment = new FirstFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.abs__custom, newFragment, "FirstFragment").commit();
Fragment newFragment2 = new FirstFragment();
FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.add(R.id.abs__custom, newFragment2, "SecondFragment").commit();
}
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");
//ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Fragment 1");
//ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Fragment 2");
mViewPager = (ViewPager)findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
mTabsAdapter.addTab(tab1, FirstFragment.class);
mTabsAdapter.addTab(tab2, SecondFragment.class);//LoaderCursorSupport.CursorLoaderListFragment.class);
//mTabsAdapter.addTab(tab3, FirstFragment.class);//LoaderCustomSupport.AppListFragment.class);
//mTabsAdapter.addTab(tab4, SecondFragment.class);//LoaderThrottleSupport.ThrottledLoaderListFragment.class);
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
}
/**
* This is a helper class that implements the management of tabs and all
* details of connecting a ViewPager with associated TabHost. It relies on a
* trick. Normally a tab host has a simple API for supplying a View or
* Intent that each tab will show. This is not sufficient for switching
* between pages. So instead we make the content part of the tab host
* 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
* view to show as the tab content. It listens to changes in tabs, and takes
* care of switch to the correct paged in the ViewPager whenever the selected
* tab changes.
*/
public class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<String> mTabs = new ArrayList<String>();
public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = actionBar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss) {
mTabs.add(clss.getName());
mActionBar.addTab(tab.setTabListener(this));
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return Fragment.instantiate(mContext, mTabs.get(position), null);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
@Override
public void OnButtonClickedX(View v) {
if (v==findViewById(R.id.button1)){
Log.i("TRIGGERED","TRIGGERED");
FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
View root = ff.getView();
TextView tv = (TextView) root.findViewById(R.id.textView1);
Log.i("Text before Edit",""+tv.getText());
tv.setText("MODIFIED");
Log.i("Text after Edit",""+tv.getText());
}
// TODO Auto-generated method stub
}
}
FirstFragment:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
int mNum;
/**
* Create a new instance of FirstFragment, providing "num"
* as an argument.
*/
static FirstFragment newInstance(int num) {
FirstFragment f = new FirstFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfrag1, container, false);
return v;
}
}
SecondFragment:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.SupportActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
int mNum;
OnButtonClickedXListener mListener;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static SecondFragment newInstance(int num) {
SecondFragment f = new SecondFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfrag2, container, false);
View button1 = v.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.OnButtonClickedX(v);
// TODO Auto-generated method stub
}
});
return v;
}
public interface OnButtonClickedXListener{
public void OnButtonClickedX(View v);
}
@Override
public void onAttach(SupportActivity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
mListener = (OnButtonClickedXListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnButtonClickedXListener");
}
}
}
myfrag1.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:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="No String"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
myfrag2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Button" />
</LinearLayout>
EDIT:
Even changing to 0 the ContainerViewId in ft.add() doesn't impact at all in final rendering. SO I guess the rendering is managed by
mTabsAdapter.addTab(tab1, FirstFragment.class);
mTabsAdapter.addTab(tab2, SecondFragment.class);
The issue is still the same, anyway.
Here's actionbar_tabs_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
SOLVED!
Overriding instantiateItem() in TabsAdapter and adding the ViewPager as ContainerViewID in the FragmentTransaction made it!
Here's the functioning whole FragmentActivity!
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentManager.OnBackStackChangedListener;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.SupportActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;
/**
* Demonstrates combining the action bar with a ViewPager to implement a tab UI
* that switches between tabs and also allows the user to perform horizontal
* flicks to move between the tabs.
*/
public class ActionBarTabsPager extends FragmentActivity implements SecondFragment.OnButtonClickedXListener{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("ONCREATE START","ONCREATE START");
setContentView(R.layout.actionbar_tabs_pager);
if (savedInstanceState == null) {
Fragment newFragment = new FirstFragment();
Fragment newFragment2 = new SecondFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.pager, newFragment, "FirstFragment");
ft.add(R.id.pager, newFragment2, "SecondFragment");
ft.commit();
}
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");
//ActionBar.Tab tab3 = getSupportActionBar().newTab().setText("Fragment 1");
//ActionBar.Tab tab4 = getSupportActionBar().newTab().setText("Fragment 2");
mViewPager = (ViewPager)findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);
mTabsAdapter.addTab(tab1, FirstFragment.class);
mTabsAdapter.addTab(tab2, SecondFragment.class);//LoaderCursorSupport.CursorLoaderListFragment.class);
//mTabsAdapter.addTab(tab3, FirstFragment.class);//LoaderCustomSupport.AppListFragment.class);
//mTabsAdapter.addTab(tab4, SecondFragment.class);//LoaderThrottleSupport.ThrottledLoaderListFragment.class);
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
}
Log.i("ONCREATE END","ONCREATE END");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i("onSaveInstanceState START","onSaveInstanceState START");
super.onSaveInstanceState(outState);
outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
Log.i("onSaveInstanceState END","onSaveInstanceState END");
}
/**
* This is a helper class that implements the management of tabs and all
* details of connecting a ViewPager with associated TabHost. It relies on a
* trick. Normally a tab host has a simple API for supplying a View or
* Intent that each tab will show. This is not sufficient for switching
* between pages. So instead we make the content part of the tab host
* 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
* view to show as the tab content. It listens to changes in tabs, and takes
* care of switch to the correct paged in the ViewPager whenever the selected
* tab changes.
*/
public class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<String> mTabs = new ArrayList<String>();
private FragmentTransaction mCurTransaction = null;
public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = actionBar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss) {
Log.i("addTab","addTab");
mTabs.add(clss.getName());
mActionBar.addTab(tab.setTabListener(this));
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Object instantiateItem(View container, int position) {
if (mCurTransaction == null) {
mCurTransaction = getSupportFragmentManager().beginTransaction();
}
// TODO Auto-generated method stub
Fragment fragment = getItem(position);
if (fragment!=null){
Log.i("Fragment Found!","Fragment Found! "+fragment.getTag());
mCurTransaction.attach(fragment);
}
return fragment;//super.instantiateItem(container, position);
}
@Override
public Fragment getItem(int position) {
Log.i("getItem","getItem");
if (position==0)
{Log.i("position=0","position=0");
return getSupportFragmentManager().findFragmentByTag("FirstFragment");}
else if (position==1)
{Log.i("position=1","position=1");
return getSupportFragmentManager().findFragmentByTag("SecondFragment");}
else return null;//Fragment.instantiate(mContext, mTabs.get(position), null);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
@Override
public void OnButtonClickedX(View v) {
if (v==findViewById(R.id.button1)){
Log.i("TRIGGERED","TRIGGERED");
FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
View root = ff.getView();
TextView tv = (TextView) root.findViewById(R.id.textView1);
Log.i("Text before Edit",""+tv.getText());
tv.setText("MODIFIED");
Log.i("Text after Edit",""+tv.getText());
}
// TODO Auto-generated method stub
}
}
Thanks to vbsteven!
这篇关于从另一个片段更新的ViewPager一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!