使用Bundle参数填充片段

时间:2013-12-26 21:44:38

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

我有一个listFragment来显示一个列表, 当我们点击列表时,我想显示一个片段,其中包含被点击元素的详细信息。我用动态片段做这个。 我的问题是关于显示我的detailsFragment。 (cf 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 detailFragment

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

1 个答案:

答案 0 :(得分:0)

我认为问题在于,您的所有TextView的高度属性都为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>

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