单击对话框时替换另一个片段上方的片段(viewpager)

时间:2013-10-18 16:05:48

标签: java android android-fragments actionbarsherlock android-viewpager

当我在另一个片段中单击一个对话框时,我正在尝试打开一个片段。我正在使用ActionBarSherlock和Tab。我的片段附加在视图寻呼机中。我几乎完成了这项工作。但我不能替换视图寻呼机内的新片段。我收到了一个错误。我读了帖子here。解决方案尚不清楚。

错误:

  

10-18 21:34:40.379:E / AndroidRuntime(19618):致命异常:主要   10-18 21:34:40.379:E / AndroidRuntime(19618):   java.lang.IllegalArgumentException:找不到id 0x7f040032的视图   片段的(com.example.actionbartestwithsherlock:id / pager)   AllContactsFragment {41fd4ba0#0 id = 0x7f040032} 10-18 21:34:40.379:   E / AndroidRuntime(19618):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)

我有三个片段关联,其中寻呼机名为FragmentTab1,FragmentTab2& FragmentTab3。

我的 MainActivity & FragmentAdapter 如下所示:

  public class MainActivity extends SherlockFragmentActivity {

    ActionBar.Tab Tab1, Tab2, Tab3, Tab4;
    private Context context = this;
    // view pager
    // Declare Variables
    ActionBar actionBar;
    ViewPager mPager;
    Tab tab;
    FragmentAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // set application in portrait mode
        ActivityHelper.initialize(this);

        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Locate ViewPager in activity_main.xml
        mPager = (ViewPager) findViewById(R.id.pager);
        // add an adapter to pager
        mPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),
                mPager, actionBar));

        addActionBarTabs();
    }

    private void addActionBarTabs() {

        String[] tabs = { "Tab 1", "Tab 2", "Tab 3" };
        for (String tabTitle : tabs) {
            ActionBar.Tab tab = actionBar.newTab().setText(tabTitle)
                    .setTabListener(tabListener);
            actionBar.addTab(tab);
        }
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }

    private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }
    };

class FragmentAdapter extends FragmentPagerAdapter implements
            ViewPager.OnPageChangeListener {

        private ViewPager mViewPager;
        final int TOTAL_PAGES = 3;

        public FragmentAdapter(FragmentManager fm, ViewPager pager,
                ActionBar actionBar) {
            super(fm);
            this.mViewPager = pager;
            this.mViewPager.setOnPageChangeListener(this);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return FragmentTab1.newInstance();
            case 1:
                return FragmentTab2.newInstance();
            case 2:
                return FragmentTab3.newInstance();
            default:
                throw new IllegalArgumentException(
                        "The item position should be less or equal to:"
                                + TOTAL_PAGES);
            }
        }

        @Override
        public int getCount() {
            return TOTAL_PAGES;
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    }
}

现在,在我的第一个标签 FragmentTab1 中,当按下按钮时,我会打开一个自定义对话框。我想在选择对话框选项时替换FragmentTab1中的新片段 AllContactsFragment

FragmentTab1片段类:

public class FragmentTab1 extends SherlockFragment implements OnClickListener {

Button btnTest;
ViewPager pager;
LinearLayout layoutBlockNumbers;
LinearLayout layoutContact, layoutCallLog, layoutSMSLog, layoutManually;
Context context;
CustomizedDialog dialog;
private static final int CONTACT_PICKER_RESULT = 1001;
private static final String DEBUG_TAG = "Contact List";
private static final double RESULT_OK = -1;

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

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

    layoutBlockNumbers = (LinearLayout) rootView
            .findViewById(R.id.layoutAddBlockNumbers);
    layoutBlockNumbers.setOnClickListener(this);
    return rootView;

}

@Override
public void onClick(View v) {

    if (v == layoutCallLog) {

        dialog.dismiss();
        // want to replace new fragment at position 0 in pager
        // problem is here ??? how to open new fragmnet
        Fragment allContactsFragment = AllContactsFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.pager, allContactsFragment).commit();

    }

    if (v == layoutBlockNumbers) {
        // open a dialog
        showDialog();
    } else if (v == layoutContact) {
        openContactList();
        dialog.dismiss();
    } else if (v == layoutSMSLog) {
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
}

// open a dialog
private void showDialog() {
    dialog = new CustomizedDialog(getActivity());
    dialog.setContentView(R.layout.dialog_add_number_type);
    dialog.setTitle("Add Black List Number");

    //initialize all linear layouts in dialog
    layoutCallLog = (LinearLayout) dialog.findViewById(R.id.layoutCallLog);
    layoutContact = (LinearLayout) dialog.findViewById(R.id.layoutContact);
    layoutSMSLog = (LinearLayout) dialog.findViewById(R.id.layoutSMSLog);
    layoutManually = (LinearLayout) dialog
            .findViewById(R.id.layoutManually);

    // add listener to several linear layout
    layoutContact.setOnClickListener(this);
    layoutCallLog.setOnClickListener(this);
    layoutSMSLog.setOnClickListener(this);
    layoutManually.setOnClickListener(this);

    dialog.show();
}

public static Fragment newInstance() {
    Fragment f = new FragmentTab1();
    return f;
} 
}

activity_main.xml如下所示:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

任何人都可以帮我解决这个问题吗?对于大量代码感到抱歉。

2 个答案:

答案 0 :(得分:1)

我不确定你能按照自己想要的方式做事。 ViewPager的设置方式与普通容器/片段设置的设置方式不同。在ViewPager中,您没有使用片段事务来添加片段,而是使用从后备列表中加载片段实例的适配器。

更换片段的工作原理如下:

(1)创建要添加的片段的实例 (2)将该片段添加到支持PagerAdapter的列表中 (3)显示新片段 (4)删除旧的

在当前项目中实现此问题的方法是设置适配器。目前,您使用的交换机语句只能返回固定数量的片段。您的适配器应该设置这样的东西。

class MyPageAdapter extends FragmentPagerAdapter{
    private List<Fragment> fragments  
    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { 
        super(fm); 
        this.fragments = fragments; 
    } 

    @Override  
    public Fragment getItem(int position) { 
        return this.fragments.get(position);   
    }   

    @Override   
    public int getCount() {     
        return this.fragments.size(); 
    } 
}

然后,您只需向适配器类添加一个方法即可添加或删除新片段。如果您知道要替换的片段的索引,那么完成此操作应该非常简单。您所要做的就是创建联系人片段的新实例,将其添加到您的阵列或列表中。 This Post解释了ViewPager如何处理新内容的添加/删除以及如何确保显示新片段。

答案 1 :(得分:0)

在我阅读this帖后,我解决了答案。

我只是添加一个ID android:id =“@ + id / fragmentTabLayout1到我的fragmenttab1.xml的顶部布局。然后调用 像往常一样的新片段:

    Fragment allContactsFragment = AllContactsFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();
        transaction.addToBackStack(null);
        // use this id to replace new fragment
        transaction.replace(R.id.fragmentTabLayout1, allContactsFragment).commit();