2015-08-23 102 views
0

我的活動有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]; 
} 
} 
+0

很難理解你的意思。我以前從未遇到過這個問題,並且經常使用「碎片」。你有幾個片段同時附着在你的Activity上嗎? – PPartisan

+0

對不起,我的英語,是的,我使用fragmentpageradapter(標籤 - viewpager),我有一個活動5片段。當我爲第一個片段設置listview時,它應用了所有這些(因爲上下文中我使用getActivity方法)。我如何處理? – Bad0

+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

非常感謝PPartisan – Bad0

+0

@ Bad0沒問題。請接受/ upvote我的答案,如果它解決了您的問題:) – PPartisan

1

好吧,如果我得到你想要的東西,你想訪問的每個片段,以能夠修改其內容等。對嗎?

首先,在您的Activity(其中一個包含片段尋呼機..),你必須instatiate的頁面,才能夠訪問它們,就像這樣:

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,例如。

+0

謝謝傑拉爾多內託 – Bad0