14

I am newbie in using android fragments .I got a scenario Let me explain it i got

  1. Main Activity that extends FragmentActivity .My main activity consists of two Fragments
  2. Option List That extends Fragment
  3. Details List that extends Fragment
  4. Details description that extends Fragment

When I launch my main activity . It will consist of Option List in Fragment 1 & Details List in Fragment 2 . Then i select an item from Details List . Then its Description must be loaded into Fragment 2 of the main activity

See this image

enter image description here

I am not getting an idea how to achieve this . I mean how can i tell Details List fragment to load Details description fragment inside main activity . Also when i press back button i must return to the initial stage ie

enter image description here

EDIT :

What i did was creating a interfaces(Listener) inside fragments and implement it on my parent activity. But if there is 10 different fragments i need implement all interfaces in my parent activity . So is there any other approach to achieve this??

4

3 回答 3

4

开发片段时要始终记住的是,它需要一个 UI 才能显示。您需要在布局中放置片段的位置。有两种方法可以做到这一点:

  • 创建一个 Fragment 类并在您的布局中声明它们,如下所示。

    这里我们假设我们已经创建了一个 ArticleListFragment 和 ArticleReaderFragment 片段。

    <fragment
        android:id="@+id/list"
        android:name="com.example.news.ArticleListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    
    <fragment
        android:id="@+id/viewer"
        android:name="com.example.news.ArticleReaderFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />
    

    这样做的缺点是您不能在运行时更改它,这意味着当您的应用程序正在执行时,您不能用另一个片段替换片段。例如,如果您必须显示两个片段,则必须在布局中声明两个片段并隐藏其中一个。幸运的是还有另一种方法。

  • 在运行时以编程方式添加您的片段。在这种方法中,您必须首先声明一个布局,并确保添加一个容器(LinearLayout、RelativeLayout 等)来放置片段。例如:

    <ListView
        android:id="@id/options_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>
    
    <LinearLayout
        android:id="@id/detail_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >
    </LinearLayout>
    

在这里,我为您的选项定义了一个列表 options_list 和一个布局 detail_layout,您需要在其中放置详细信息。现在在运行时,当单击一个选项时,您会在 detail_layout 上显示详细信息片段,例如:

ExampleFragment fragment = new ExampleFragment();
getFragmentManager().beginTransaction().add(R.id.detail_layout, fragment).commit();

用另一个片段替换那个片段:

Fragment newFragment = new AnotherExampleFragment();
getFragmentManager().beginTransaction().replace(R.id.detail_layout, newFragment).addToBackStack(null).commit();

注意对 addToBackStack 的调用。这是必需的,以便当用户按下返回时,将显示上一个。

我想你可以从这里弄清楚。:)

于 2013-05-24T07:50:41.250 回答
0

根据http://developer.android.com/guide/components/fragments.html#Adding

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

您可能还需要:

public class MainActivity extends FragmentActivity {
    ...
    public void onArticleSelected(Uri articleuri) {
        //use data to add the new fragment
    }
    ...
} 

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;

    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(noteUri);
    }
    ...
}
于 2013-05-24T06:04:24.830 回答
0

在您的片段活动中,您可以使用 2 个容器视图(例如 2 个线性布局) - 用于左侧导航(选项列表)和右侧内容。然后你需要一个公共函数 public void switchFragmentInContainer(Fragment f, int position) ,比如你将一个新片段加载到你的容器中......

在您的 OptionList 中,您将此函数称为 ((FragmentActivity) getActivity).switchFragmentInContainer(NEWFRAGMENT, FragmentActivity.RIGHT);.

我希望这是您搜索的内容。

于 2013-05-24T05:58:36.963 回答