【问题标题】:Populate fragment with Bundle arguments使用 Bundle 参数填充片段
【发布时间】:2013-12-26 21:44:37
【问题描述】:

我有一个 listFragment 来显示一个列表, 当我们单击列表时,我想显示一个带有单击元素详细信息的片段。我正在使用动态片段进行此操作。 我的问题是关于我的 detailsFragment 的显示。 (参见picture)。 我应该有“指定”和其他信息。 我只有我的 DetailsFragment XML 的一部分。 但是我用调试模式检查了捆绑包,通信正常。 那么为什么这个片段的显示会出现问题呢?

主要主持人活动

public void onListItemClick(int id) {

F_Outils_details detail_frag = (F_Outils_details)
getFragmentManager().findFragmentById(R.id.listFragment2);

if (detail_frag != null) {
// If article frag is available, we're in two-pane layout...

// Call a method in the ArticleFragment to update its content
detail_frag.setDetails((int)id);

} 
else {
// If the frag is not available, we're in the one-pane layout     and must swap frags...

F_Outils_details detail_frag_new = new F_Outils_details();

Bundle args = new Bundle();
args.putInt("outil",(int) id);
detail_frag_new.setArguments(args);
FragmentTransaction transaction =getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back

transaction.replace(R.id.fragment_container1, detail_frag_new);
transaction.addToBackStack(null);


transaction.commit();
}

DetailFragmentActivity

​​>
public class F_Outils_details extends Fragment {
ArrayList detail_outils=new ArrayList();
Outil outil;
TextView id_outil;
TextView designation;
TextView date_location;
View a;
ArrayList liste_outils;
int mCurrentPosition=-1;


public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.f_outils_details, container, false);

}



    @Override
    public void onStart() {
super.onStart();

// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
//si il y a pas d'arguments
if (args != null) {

setDetails(args.getInt("outil"));
} else if (mCurrentPosition != -1) {
System.out.println("mccurent posit");
// Set article based on saved instance state defined during onCreateView
setDetails(mCurrentPosition);
}
}

//methode qui va changer le texte !
public void setDetails(int id){
if(id==-1){
designation.setText("");
date_location.setText("");
}
else{
//on recupere l'outil
DB_Locoutils dbLocoutils=DB_Locoutils.getInst();
liste_outils=dbLocoutils.getOutils();
outil =(Outil) liste_outils.get((int) id);
System.out.println("**************"+outil.getDesignation());

//id_outil=(TextView) getView().findViewById(R.id.id_outil);
designation=(TextView) getActivity().findViewById(R.id.designation);
date_location=(TextView) getActivity().findViewById(R.id.date_location);
if(designation!=null){
System.out.println("DESIGNATION OK");
}
//id_outil.setText(outil.getIdOutil());
designation.setText(outil.getDesignation());
date_location.setText(outil.getDateLocation());
}
}

}

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" >
<TextView
 android:id="@+id/titre"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:textStyle="bold"
 android:textSize="20dp"
 android:text="Details" />

 <TextView
     android:id="@+id/id_outil"
     android:layout_width="fill_parent"
     android:layout_height="match_parent"
     android:text="id outil"
  />

<TextView
     android:id="@+id/designation"
     android:layout_width="fill_parent"
     android:layout_height="match_parent"
     android:text="designation"
     />

<TextView
     android:id="@+id/refClient"
     android:layout_width="fill_parent"
     android:layout_height="match_parent"
     android:text="ref Client"
      />

<TextView
android:id="@+id/date_location"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="date location"
/>

【问题讨论】:

  • 这一行显示什么:System.out.println("**************"+outil.getDesignation());?
  • 这显示了很好的信息!这意味着我成功转移了我的 Bundle,但这实际上是关于我的片段的显示。可以在 Method Onstart 中修改我的 textview 吗?
  • 不,这不相关,请参阅:stackoverflow.com/a/13795379/2668136 - 在这里,他说您可以将 textviews 修改为 onStart 方法。这是另一回事。
  • 也许是布局的问题!对于我的主要活动,我使用 &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container1" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; 替换为线性布局。也许 FrameLayout 和 Linear 不适合?
  • 或者也许我应该在我的事务提交后更改我的 TextView?

标签: android android-layout android-fragments android-view android-xml


【解决方案1】:

我认为问题在于您的所有TextViews 的高度属性都为match_parent。这表明您的第一个项目占据了所有父级高度,而其他项目则留在后面。尝试使用此布局:

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

    <TextView
        android:id="@+id/titre"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="20dp"
        android:text="Details" />

    <!-- as the item above, set the height to wrap_content -->
    <TextView
        android:id="@+id/id_outil"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="id outil"
        />

    <TextView
        android:id="@+id/designation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="designation"
        />

    <TextView
        android:id="@+id/refClient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="ref Client"
        />

    <TextView
        android:id="@+id/date_location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="date location"
        />

</LinearLayout>

希望这会有所帮助,您将获得预期的结果。

【讨论】:

  • 这很奇怪,使用 getActivity() 或 getView(),在这两种情况下我都能获得我的 TextView 元素。但问题仍然存在。在您向我指出的不同情况下,他们在 OnCreateView 方法中获得了由于 NullJavaException 而我无法执行的视图。
  • no :/ 奇怪的是,当我将方向更改为横向时,我得到了正确的信息!我会检查是否没有两个DetailsFragments
  • 不客气!我四处寻找你的问题。确实,这很简单。很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 2011-03-28
相关资源
最近更新 更多