返回从详细信息片段返回到列表片段

时间:2015-07-30 14:55:21

标签: android android-fragments fragment

我有一个ListView,它从RSS获取并显示一些数据。我在ListView上显示,并使用onClickListener我显示list元素的详细信息。在此之前没有问题。但是我的ListView类和详细类都是Fragment.When我想从详细回到List片段,它不返回。如何从详细到List片段?这是一些代码;

public class BlogPostsFragment extends Fragment{

...

}

public class BlogDetailFragment extends Fragment{

...

}

以下是Activity中的片段事务;

 private void displayView(int position) {
    fragment = null;
    switch (position) {
    case 0:
        fragment = new BlogPostsFragment();
        break;
    default:
        break;

    }

    if (fragment != null) {
        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(drawerList.get(position));// Burada Fragment'ın titleı
                                            // veriliyor.
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        Log.e("BlogMainPageActivity",
                "Fragment oluşturulurken hata meydana geldi!");
    }
}

这是代码片段事务;

vi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            BlogDetailFragment bdf = new BlogDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", currentItem.getTitle());
            bundle.putString("description", currentItem.getDesc());
            bundle.putString("pubDate", currentItem.getPubDate());
            bundle.putString("link", currentItem.getLink());

            bdf.setArguments(bundle);

            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, bdf).commit();

        }
    });

我在一个activity中开始事务。所以我从Activity调用了BlogPostsFragment。

2 个答案:

答案 0 :(得分:1)

使用详细信息片段替换列表片段时,可以将列表片段添加到后端堆栈。这会将列表片段放在后堆栈的顶部,当用户按下时弹出它。修改您的代码:

vi.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        BlogDetailFragment bdf = new BlogDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", currentItem.getTitle());
        bundle.putString("description", currentItem.getDesc());
        bundle.putString("pubDate", currentItem.getPubDate());
        bundle.putString("link", currentItem.getLink());

        bdf.setArguments(bundle);

        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, bdf).addToBackStack(null).commit();

    }
});

答案 1 :(得分:1)

以下是你应该点击的内容:

String fragmentTag = getFragmentTag();
FragmentTransaction fragmentTransaction = FragmentTransaction  fragmentTransaction = getFragmentManager().beginTransaction();
BlogDetailFragment bdf = (BlogDetailFragment) getFragmentManager().findFragmentByTag(fragmentTag);

if(BlogDetailFragment == null) {
    bdf = new BlogDetailFragment();
    //ADD BUNDLE HERE
}
fragmentTransaction.replace(R.id.frame_container, bdf, fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
fragmentTransaction = null;