2013-12-17 84 views
1

我有一個擴展片段的類。我想要按下活動中的按鈕以打開此對話框中其佈局的片段,並且片段通常在其位置打開。這可能嗎?在對話框中顯示我的片段android

這是我的片段(TipsFragment.java)

public class TipsFragment extends Fragment{ 

ListView list; 
TipsAdapter tipsAdapter; 
AlertDialog PageDialog; 
DAO db; 
Cursor c; 

ArrayList<HashMap<String, String>> tipsList; 
HashMap<String, String> map; 

ArrayList<HashMap<String, String>> pages; 
Integer tipType; 

ImageButton page; 
ImageButton add; 
ImageButton search; 

EditText inputAdd; 
EditText inputSearch; 

ImageView addImage; 

RelativeLayout layoutAdd; 
RelativeLayout layoutSearch; 

int itemSelected; 


@Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     db = new DAO(activity); 
     db.open(); 
    } 

public TipsFragment(){} 

public TipsFragment(int tipType, int itemSelected){ 

    this.tipType = tipType; 
    this.itemSelected = itemSelected; 
} 



@Override 
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
     final Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.tips_fragement, container, false); 

    System.out.println("tipType: "+tipType); 
    System.out.println("itemSelected: "+itemSelected); 

    page = (ImageButton) rootView.findViewById(R.id.pager); 
    add = (ImageButton) rootView.findViewById(R.id.add); 
    search = (ImageButton) rootView.findViewById(R.id.search); 

    inputSearch = (EditText) rootView.findViewById(R.id.inputSearch); 
    inputAdd = (EditText) rootView.findViewById(R.id.inputAdd); 

    addImage = (ImageView) rootView.findViewById(R.id.imageAdd); 

    layoutAdd = (RelativeLayout) rootView.findViewById(R.id.layoutAdd); 
    layoutSearch = (RelativeLayout) rootView.findViewById(R.id.layoutSearch); 

    if (tipType != 0) { 
     switch (tipType) { 
     case 1: 
      System.out.println("All tips"); 
      if (getActivity().getIntent().getStringExtra("startFrom") == null) { 
       c = db.getTips("0"); 
      } else { 
       c = db.getTips(getActivity().getIntent().getStringExtra("startFrom")); 
      } 

      break; 
     case 2: 
      System.out.println("favorite tips"); 
      c = db.getFavoriteTips(); 
      page.setVisibility(View.GONE); 
      System.out.println("in favorite, count_records: "+c.getCount()); 
      break; 

     } 
    } 

    System.out.println("count_records: "+c.getCount()); 

    if (c.getCount() != 0) { 

     if (getActivity().getIntent().getStringExtra("startLabel") != null) { 
      page = (ImageButton) rootView.findViewById(R.id.pager); 
     } 
     tipsList = new ArrayList<HashMap<String, String>>(); 

     list = (ListView) rootView.findViewById(R.id.tipList); 

     do { 
      map = new HashMap<String, String>(); 

      // adding each child node to HashMap key =&gt; value 
      map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0)))); 
      map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1)))); 

      // adding HashList to ArrayList 
      tipsList.add(map); 

     } while (c.moveToNext()); 

     // Getting adapter by passing xml data ArrayList 
     tipsAdapter = new TipsAdapter(getActivity(), tipsList); 
     list.setAdapter(tipsAdapter); 

     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       map = tipsList.get(position); 

       System.out.println("in list select item, tipType: "+tipType); 

       if (!MainActivity.tSlidingLayer.isOpened()) { 
        MainActivity.tSlidingLayer.openLayer(true); 
       } 


      } 
     }); 

     page.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Strings to Show In Dialog with Radio Buttons 


      } 
     }); 

     add.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) {     


      } 
     }); 

     search.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 


      } 
     }); 

     inputSearch.addTextChangedListener(new TextWatcher() { 

      public void afterTextChanged(Editable s) { 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 
     }); 

    } 


    addImage.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getActivity(), "addImage pressed", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return rootView; 
} 

}

,這是佈局tips_fragement.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="5dp" 

android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/titleBar" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:gravity="center" 
    android:orientation="horizontal" > 

     <ImageButton 
      android:id="@+id/add" 
      android:layout_width="45dp" 
      android:layout_height="35dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/add" /> 

     <ImageButton 
      android:id="@+id/search" 
      android:layout_width="45dp" 
      android:layout_height="35dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/search" /> 

     <ImageButton 
      android:id="@+id/pager" 
      android:layout_width="45dp" 
      android:layout_height="35dp" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:scaleType="fitXY" 
      android:src="@drawable/pager" /> 

</LinearLayout> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="5dip" 
    android:background="@drawable/shadow" > 
</View> 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layoutAdd" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:visibility="gone" > 

<EditText 
    android:id="@+id/inputAdd" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Add" /> 

<ImageView 
    android:id="@+id/imageAdd" 
    android:padding="5dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/inputAdd" 
    android:layout_alignBottom="@+id/inputAdd" 
    android:layout_alignRight="@+id/inputAdd" 
    android:src="@android:drawable/ic_menu_add" /> 

</RelativeLayout> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/layoutSearch" 
android:visibility="gone" > 

<EditText 
    android:id="@+id/inputSearch" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Search"/> 

<ImageView 
    android:id="@+id/imageSearch" 
    android:padding="5dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/inputSearch" 
    android:layout_alignBottom="@+id/inputSearch" 
    android:layout_alignRight="@+id/inputSearch" 
    android:src="@android:drawable/ic_menu_search" /> 

</RelativeLayout> 

<ListView 
    android:id="@+id/tipList" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/buttons" 
    android:divider="#351802" 
    android:dividerHeight="2dp" 
    android:listSelector="@drawable/list_selector" /> 

</LinearLayout> 

回答

1

由於Android支持nestingFragments(在支持庫可用)您可以將您的TipsFragment顯示爲另一個DialogFragment的內容。儘可能避免使用此解決方案。

更好的解決方案是使TipsFragment擴展DialogFragment,這樣你就可以在內嵌或對話框中顯示Fragment

1 - 讓TipsFragment延伸DialogFragment

public class TipsFragment extends DialogFragment {...} 

2 - 按一下按鈕使用FragmentTransaction.addFragmentTransaction.replace展現DialogFragment內嵌或包含在你的佈局,並改變其知名度。

3 - 顯示TipsFragment作爲一個對話框

TipsFragment fr = new TipsFragment(); 
fr.show(getSupportFragmentManager(), "dialog_fragment_tag"); 


指南:Performing Fragment Transactions。在ApiDemos應用程序中有更多樣本SDK samples

+0

請您告訴我在按下按鈕時如何在對話框中顯示TipsFragment。我是新來的。 – Eman87

+0

對不起,請給我一個使用FragmentTransaction.add或FragmentTransaction.replace的例子。並感謝你對我的努力。 – Eman87

+0

更新了答案,以包含指向FragmentTransaction指南的鏈接 – dxjankov