替换旧片段以在viewpager中添加新片段

时间:2014-05-27 12:02:35

标签: java android android-fragments

我有这个代码。在设置片段时,我点击了那个按钮,我想查看新的片段。

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);

}

@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
Log.i("TAB", "Home");
return new HomePage();
case 1:
// Games fragment activity
Log.i("TAB", "Contact");
return new GetContacts();
case 2:
// Movies fragment activity
Log.i("TAB", "Setting");
return new Settings();
}
return null;
}

我扩展了FragmentPageAapter。

3 个答案:

答案 0 :(得分:0)

好的,这里是如何在片段A中添加按钮来切换片段c: 1-在片段a中添加按钮,在xml文件中,id为foo,如下所示:

<Button
    android:id="@+id/foo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="go to fragemnt c" />

去片段一个名为onCreateView的类覆盖方法,并通过创建名为buttonClick的接口设置onclickListner创建并在旁边片段创建var,然后覆盖方法onAttch初始化接口var onAttch方法:

public class FragmentA extends Fragment {

buttonClick click;
Button foo;

interface buttonClick {
    void buttonClicked(View v);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    click = (buttonClick) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container,false);

    foo = (Button) view.findViewById(R.id.foo);
    foo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            click.buttonClicked(v);
        }
    });

    return view;
}

}

然后回到你的MainActivity实现buttonClick接口并覆盖方法中的方法buttonClicked(View v); setCurrentItem用于视图寻呼机,如下所示:

calss MainActivity implements FragmentA.buttonClick {

   // your code here ...

   public void buttonClicked(View v){
      //get your viewPager var
      viewPager.setCurrentItem(3);
    }

}

我希望这有帮助

答案 1 :(得分:0)

你有多种方法可以实现你的#34;我想查看新片段&#34;。

通过&#34;新片段&#34;你可能意味着&#34;新内容和ui&#34;如果是这样,你可以&#34;作弊&#34;您的出路并为具有重叠视图的布局充气,您设置为VISIBLEGONE

E.g。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

   <LinearLayout
       android:id="@+id/lyt_stuff_a"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >

       <TextView
           android:id="@+id/txt_stuff_a"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="This stuff is for content a" />
   </LinearLayout>

   <LinearLayout
       android:id="@+id/lyt_stuff_b"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >

       <TextView
           android:id="@+id/txt_stuff_b"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="This stuff is for content b"
           android:visibility="gone" />
   </LinearLayout>

</RelativeLayout>

因此,在代码中,您只需按

切换内容即可
getView().findViewById(R.id.lyt_stuff_a).setVisibility(View.GONE);
getView().findViewById(R.id.lyt_stuff_b).setVisibility(View.VISIBLE);

但是如果布局非常复杂,这种方法可能会使View树膨胀。并且适当的片段可能会做太多的事情,你想在多个片段中分开。

如果是这种情况,您必须了解配置更改或重新启动应用程序并处理这些更改(即保存您的状态并重新初始化正确的片段)。如果您对此感到满意,可以在运行时更改ViewPager中的Fragments:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    protected Fragment[] fragments;

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
        initFragPageCount();
    }

    private void initFragPageCount() {
        fragments = new Fragment[3];
    }

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

    @Override
    public Fragment getItem(int position) {
        Fragment f = fragments[position];
        if (f == null) {
            f = initFragment(position);
        }
        return f;
    }

    private Fragment initFragment(int position) {
        Fragment f = fragments[position];
        if (f == null) {
            switch (position) {
            case 0:
                f = new HomePage();
                fragments[position] = f;
                break;
            case 1:
                f = new GetContacts();
                fragments[position] = f;
                break;
            case 2:
                f = new Settings();
                fragments[position] = f;
                break;
            }
        }
        return f;
    }

    public void replaceFrag(Fragment f, int index) {
        if (index > 0 && index < fragments.length) {
            fragments[i] = f;
        }
        notifyDataSetChanged();
    }
}

你可以像这样替换它(例如在你的按钮OnClickListener中):

adaptPager.replaceFrag(new FragReplacement(), 2); // replaces the third Fragment in the Pager

答案 2 :(得分:0)

  

动态添加或删除viewpager中的片段   加载不同的片段调用setupViewPagerCustom(viewPager)。   例如按钮单击调用:setupViewPagerCustom(viewPager);

    private void setupViewPager(ViewPager viewPager)
{
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new fragmnet1(), "HOME");
    adapter.addFrag(new fragmnet2(), "SERVICES");

    viewPager.setAdapter(adapter);
}

private void setupViewPagerCustom(ViewPager viewPager)
{

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFrag(new fragmnet3(), "Contact us");
    adapter.addFrag(new fragmnet4(), "ABOUT US");


    viewPager.setAdapter(adapter);
}

//Viewpageradapter, handles the views

static class ViewPagerAdapter extends FragmentStatePagerAdapter
{
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager){
        super(manager);
    }

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

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

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }

    public void addFrag(Fragment fragment, String title){
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position){
        return mFragmentTitleList.get(position);
    }
}