我的活動有5個片段。我想達到我的片段的上下文中的一個,但是當我使用getActivity()
方法來達到上下文時,我的代碼適用於每個片段。我如何專門化每個片段上下文? (我的意思是達到只有片段上下文)?在Android中獲取片段上下文
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];
}
}
很難理解你的意思。我以前從未遇到過這個問題,並且經常使用「碎片」。你有幾個片段同時附着在你的Activity上嗎? – PPartisan
對不起,我的英語,是的,我使用fragmentpageradapter(標籤 - viewpager),我有一個活動5片段。當我爲第一個片段設置listview時,它應用了所有這些(因爲上下文中我使用getActivity方法)。我如何處理? – Bad0
碎片根本沒有自己的上下文,它們都使用活動上下文來處理它們駐留的活動。聽起來你需要使用不同的方法來處理你想要實現的任何方法。 –