2011-05-10 119 views
39

我如你所期望它创建其内部onCreateView视图中的片段。但是我想定期更改视图。如何更改片段内的视图?

我的使用情况是,该片段显示了Web服务的一些数据。当用户从列表(另一个片段)中选择一个选项时,该片段应切换到进度条,然后加载后切换回数据视图。

我可以让两个片段 - 装载片段,以及显示片段,但似乎因为这是一个封装的事件,我宁愿做这一切的一个片段里。

基本上,我问,是什么,是的setContentView的片段内equivilent。

回答

31

您可以使用Fragment.getView()。这将返回包含Fragment的容器的视图。根据该观点,您可以致电removeAllViews。然后建立新视图并将它们添加到从getView()获得的视图中。

http://developer.android.com/reference/android/app/Fragment.html#getView()

+2

这个答案在技术上是不正确的。 'getView()'返回'onCreateView()'中提供的'View'而不是容器'View' – 2014-10-03 08:55:37

+0

好点。尽管如此,它仍然是@monkjack问题的答案。 – bplayer 2014-10-03 09:36:03

+0

'getView()'返回视图,但没有'removeAllViews()'方法。该方法似乎在'ViewGroup'中。我错过了什么吗? – Tim 2015-02-04 15:14:45

4

你可以使用一个FrameLayout你的进度条和数据视图,例如之间切换

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:id="@+id/cover_image" 
     android:scaleType="fitCenter" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    <ProgressBar android:id="@+id/cover_progress" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center"/> 
</FrameLayout> 

onViewCreate()那么您需要储存这两种观点为实例字段

public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
    ... 
    image = (ImageView) container.findViewById(R.id.cover_image); 
    progress = container.findViewById(R.id.cover_progress); 
    ... 
} 

,只要你想在两者之间切换(记得要做到这一点在你的UI /主线程),只是做一些事情像

progress.setVisibility(View.INVISIBLE); 
image.setVisibility(View.VISIBLE); 
+1

'ViewSwitcher'在这种情况下也很方便 – 2011-12-08 19:19:47

+0

view.findViewById(R.id.cover_image); 什么是变量“视图”? – Montaro 2013-09-06 05:20:01

+1

@Montaro应该是'容器',在答案中修复。谢谢! – 2013-09-06 10:49:09

1

U可以使用事务删除和添加。我使用了可以加载任何视图的片段。 片段的构造函数都必须有一个整数,该整数是R.layout .... 我只是删除旧的片段,并把一些新的。 !!!它只适用于你使这个片段是动态的,而不是在xml布局中。如果由于某种原因,你想改变整个片段视图(例如,异步URL请求,这将改变对成功的看法) 就创造这样一个的LinearLayout R.id.fragment_layout

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.wundlist_fragmente); 
    Log.v("WoundListActivity", "WoundListActivity gestartet..."); 

    Log.v("WoundListActivity", "Liste..."); 
    //dynamisch hinzufügen... 
    wlf=new WoundListFragment();   
    fm.beginTransaction().add(R.id.fragment_layout,wlf).commit(); 

} 

//If Menubutton Clicked change 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if(item.getItemId()==R.id.itemaddwound){ 
     Log.v("List", "neue Wunde Activity"); 

     fm.beginTransaction().remove(wlf).commit(); 
     fm.beginTransaction().add(R.id.fragment_layout, new NewWoundFragment()).commit(); 

    } 

    return super.onOptionsItemSelected(item); 
} 
24

,你可以或者在父活动中使用FragmentTransaction并即时创建新的片段。

或者您可以保留片段并调用此片段的方法,该方法将自行刷新。 示例: 在父项活动中,我构建并存储了片段列表List<RefreshFragment> mFragmentList

这里是RefreshFragment类(和我所有的片段延长这个在我的例子):

public class RefreshFragment extends Fragment { 
    protected Data data; // here your asynchronously loaded data 

    public void setData(Data data) { 
     this.data = data; 
     // The reload fragment code here ! 
     if (! this.isDetached()) { 
      getFragmentManager().beginTransaction() 
       .detach(this) 
       .attach(this) 
       .commit(); 
     } 
    } 
} 

然后在我的活动异步回调,我可以打电话:

for (RefreshFragment f : mFragmentList) f.setData(data); 

所以每次片段将用正确的数据更新,当前附加的片段将立即更新。你必须在片段中提供你自己的onCreateView。

重要的是片段可以用getFragmentManager()重新加载自己。

+0

这对我很有用。谢谢。 – Jacek 2012-11-22 19:37:48

+0

问题是,当你加载你的数据或当用户点击一个按钮时,碎片永远不会被分离... – 2013-12-25 12:55:10

+0

@Waza_Be我认为这是一个类型,应该看起来像这样:'if(!this.isDetached()){'' 。 – sulai 2015-11-04 13:22:48

-1

这是什么工作对我来说:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_recipe, container, false); 
    Button recipeBuyButton = (Button) v.findViewById(
      R.id.recipe_buy_button); 
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GE_SS_Two_Medium.otf"); 
    recipeBuyButton.setTypeface(tf); 
    return v; 
+1

为什么人们对正确答案进行反对投票?这个答案没有什么是错的。 +1从我身边。 – 2015-07-08 04:10:59

+0

因为这个答案与当前问题无关。 – Sierisimo 2015-10-26 19:15:58

1

使用Solostaran14s响应我现在这样做:

接口:

public interface FragmentReattachListener { 

    public void reAttach(); 

} 

片段:

public class MyFragment extends Fragment implements FragmentReattachListener { 
//... 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    //... 
    App.registerFragmentReattachListener(this); 
    //... 
} 

//...  

@Override 
public void reAttach() { 
    FragmentManager f = getFragmentManager(); 
    if (f != null) { 
     f.beginTransaction() 
        .detach(this) 
        .attach(this) 
        .commit(); 
     } 
    } 

} 

从应用类:

private static void reattachFragments() { 
     if (fragmentReattachListeners.size() > 0) { 
      for (FragmentReattachListener listener : fragmentReattachListeners) { 
       listener.reAttach(); 
      } 
     } 
    } 
+0

我赞同:更好的界面。 – Solostaran14 2015-11-23 14:03:24

-1

我有同样的问题,fragment.getView()始终返回null,虽然Fragment.onCreateView已经调用之前

我们不能在运行时改变片段的内容,因为我的片段添加到ViewPager所以我尝试用其他方法来获得视图:

View view = viewPager.getChildAt(0); 
TextView text = (TextView) (view.findViewById(R.id.textView)); 
text.setText("12345"); 

这个问题现在是resoloved,希望可以帮到你

2

创建一个“虚拟”的默认视图

<?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" > 

</LinearLayout> 

充气片段中的onCreateView法“虚拟”默认视图,并把它作为一个占位符来添加视图需要

private LayoutInflater mInflater; 
private ViewGroup mContainer; 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){ 
    mInflater = inflater; 
    mContainer = container; 

    View v =inflater.inflate(R.layout.home_view,container,false); 
    placeholder = (ViewGroup) v; 

    return placeholder; 
} 

要更改视图到一个新的观点,你首先删除所有以前的观点则抬高新的视图,并添加

View newView = mInflater.inflate(R.layout.custom_dash, mContainer, false); 
placeholder.removeAllViews(); 
placeholder.addView(newView); 
1

这里有一个简单的解决方案:为您节省在onCreateView收到充气和容器,并在以后使用它们。例如:

private LayoutInflater mInflater; 
private ViewGroup mRootView; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    mInflater = inflater; 
    mRootView = container; 
    return null; 
} 

public void someCallBackLater() { 
    // mRootView.removeAllViews(); // in case you call this callback again... 
    mInflater.inflate(R.layout.my_layout, mRootView); 
}