在 Android 中获取片段上下文

Get Fragment context in Android

我的activity有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];
}
 }

如果您查看适配器 class、SampleFragmentPagerAdapter,您会发现您返回了一个新的 PageFragment 五次:

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

如果您查看 FragmentPagerAdapter getItem() 的文档,它指出:

Return the Fragment associated with a specified position.

因此,您正在创建五个列表。

如果您想为每个页面创建单独的片段,您可以使用 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...
    }
}

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

首先,在您的 Activity (其中包含片段寻呼机..),您必须实例化页面,以便能够访问它们,像这样:

FirstFragment mFirstFragment;
SecondFragment mSecondFragment;
.
.
.

因此,在 ActivityonCreate() 方法中,执行以下操作:

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,在第二个片段中有一个 TextView mTextView1,例如。