在Android中获取片段上下文

时间:2015-08-23 21:09:36

标签: android fragment

我的活动有5个片段。我希望达到我的片段上下文之一,但当我使用getActivity()方法进行覆盖上下文时,我的代码适用于每个片段。我如何专门化每个片段上下文? (我的意思是只到达那个片段上下文)?

public class PageFragment extends Fragment {

public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
ListView list1;
View view;

public static PageFragment newInstance(int page) {
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, page);
    PageFragment fragment = new PageFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPage = getArguments().getInt(ARG_PAGE);
}

// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.tab1, container, false);
    adapterkur();
    return view;
}

private void adapterkur() {
    Insan[] insan_data = new Insan[]
            {
                    new Insan(R.mipmap.apoprof, "Apo"),
                    new Insan(R.mipmap.baranprof, "Showers"),
                    new Insan(R.mipmap.kursatprof, "Snow"),
                    new Insan(R.mipmap.ozerprof , "Ben Delay Remix"),
                    new Insan(R.mipmap.taylanprof , "Sis Atma Och"),
                    new Insan(R.mipmap.aliprof , "BigFoot"),
                    new Insan(R.mipmap.hasanprof , "Marlboro Light"),
                    new Insan(R.mipmap.bengisuprof , "Operation"),
                    new Insan(R.mipmap.beyzaprof, "Bana yok mu"),
                    new Insan(R.mipmap.seloprof , "mega")
            };

    InsanAdapter adapter = new InsanAdapter(getActivity().getApplicationContext(), R.layout.listview_item, insan_data);
    list1 = (ListView) view.findViewById(R.id.listView);
    View header = getActivity().getLayoutInflater().inflate(R.layout.listview_header, null);
    list1.addHeaderView(header);
    list1.setAdapter(adapter);
}
  }

 public class SampleFragmentPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.IconTabProvider {
final int PAGE_COUNT = 5;
private int tabIcons[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher
        ,R.mipmap.ic_launcher};

public SampleFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
}

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

@Override
public Fragment getItem(int position) {
    return PageFragment.newInstance(position + 1);
}

@Override
public int getPageIconResId(int position) {
    return tabIcons[position];
}
 }

2 个答案:

答案 0 :(得分:1)

如果您查看适配器类SampleFragmentPagerAdapter,可以看到您要返回新的PageFragment五次:

@Override
public Fragment getItem(int position) {
    return PageFragment.newInstance(position + 1);
}

如果您查看FragmentPagerAdapter getItem()的文档,请说明:

  

返回与指定位置关联的片段。

因此,您创建了五个列表。

如果您想为每个页面创建单独的片段,可以使用switch语句:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return PageFragment.newInstance(position + 1);
            break;
        case 1:
            return PageFragment1.newInstance(position + 1);
            break;
        case 2:
            return PageFragment2.newInstance(position + 1);
            break;
        //etc...
    }
}

答案 1 :(得分:1)

好吧,如果我得到了你想要的东西,你想要访问每个片段,以便能够修改它的内容等等。对吗?

首先,在你的Activity (其中一个包含片段分页器......)中,你必须实现页面的设置,以便能够访问它们,如下所示: / p>

FirstFragment mFirstFragment;
SecondFragment mSecondFragment;
.
.
.

因此,在onCreate()的{​​{1}}方法中,请执行以下操作:

Activity

请注意,mFirstFragment = (FirstFragment) adapter.instantiateItem(pager,0); mSecondFragment = (SecondFragment) adapter.instantiateItem(pager, 1); pagerViewPageradapter

为什么有用?

因为现在你可以做一些事情,比如在每个片段中单独更改视图,例如:

FragmentPagerAdapter

mSecondFragment.mButton.setVisibility(view.VISIBLE);

假设第一个片段中有mFirstFragment.mTextView1.setText("hello, its my second fragment's textview"); ,第二个片段中有Button mButton,例如。