2016-12-26 75 views
1

我是新來的android,我有我的TabActivity(動作欄選項卡)與一些片段。在第一個選項卡中,我有一個列表,當我切換第二個選項卡時,我想在第二個選項卡中顯示所選列表的內容。在我的代碼中,當我在第一個選項卡中選擇列表項目時,它切換到第二個選項卡,但內容不更新,但是當我移到tab4和tab5然後返回到tab2時,我在第二個選項卡中獲取內容更新?我不知道Tab是如何工作的?所以給我一個例子標籤列表Fragmentandroid tabbed活動列表片段

我的第一個片段(TAB1)

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view; 
    Toast.makeText(getActivity(),"frag1",Toast.LENGTH_SHORT).show(); 
    view = inflater.inflate(R.layout.fragment_company, container, false); 
    TextView textView = (TextView)view.findViewById(R.id.section_label); 
    textView.setText("Company"); 

    ListView listView = (ListView)view.findViewById(R.id.companyList) ; 
    final List<Company> companies = Company.fetchCompanies(); 
    final List<String> compamnyNames = new ArrayList<String>(); 

    for (Company company : companies) { 
     compamnyNames.add(company.getName().getName()); 
    } 

    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,compamnyNames); 
    listView.setAdapter(listAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Company.setSelectedCompany(companies.get(i)); 

      TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs); 
      tabLayout.getTabAt(1).select(); 

     } 
    }); 

    return view; 

} 

我的第二個片段(TAB2)

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    return loadTableData(); 
} 


public View loadTableData() 
{ 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    ViewGroup container = (ViewGroup) getView(); 

    View view; 

    view = inflater.inflate(R.layout.fragment_outstanding, container, false); 
    OutstandingAdapterGenerator adapterGenerator = new OutstandingAdapterGenerator(); 
    adapterGenerator.setContext(getActivity()); 

    ListView listView = (ListView)view.findViewById(R.id.outstandingPurchaseList) ; 


    adapterGenerator.setListViewResource(R.layout.list_item); 
    adapterGenerator.setPurchase(true); 
    adapterGenerator.setCompany(Company.getSelectedCompany()); 
    adapterGenerator.setViewIds(new int[]{R.id.textViewPartyName,R.id.textViewTotal}); 
    SimpleAdapter listAdapter = adapterGenerator.generateAdapter(); 
    listView.setAdapter(listAdapter); 
    return view; 
} 



@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 



    if(isVisibleToUser && isResumed()) 
    { 

     loadTableData(); 
    } 
} 
+0

請添加您的更新代碼中的第二個片段 –

+0

請上傳您爲此做了代碼。 –

回答

1

覆蓋setUserVisibleHint方法你的第二個片段,並把你的更新代碼我ñ它,我只是寫我從你的代碼獲得,這是你的更新部分

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 
    if (isVisibleToUser && isResumed()) { 
     adapterGenerator.setCompany(Company.getSelectedCompany()); 
     adapterGenerator.setViewIds(new int[] {R.id.textViewPartyName,R.id.textViewTotal}); 
     SimpleAdapter listAdapter = adapterGenerator.generateAdapter(); 
     listView.setAdapter(listAdapter); 
    } 
} 
+0

我做到了,但我面臨同樣的問題。我在第二個選項卡中獲取內容,但是我的問題是當我單擊第一個選項卡中的列表項時,它不會立即顯示在第二個選項卡上,當我滑動到第4個和第5個選項卡時,然後回到我的第2個選項卡,內容已更新,我再次選擇列表,內容沒有改變,就像.. – Narendhran

+0

_I再次更新我的代碼(上)_ – Narendhran

+0

感謝您的善良重播** masoud vali **,我犯了一個小錯誤,我把我的'setUserVisibleHint'這意味着我在'setUserVisibleHint'中調用的方法是'loadTableData'終於Iam在viewPager中使用'setOffScreenPageLimit'方法來解決我的問題 – Narendhran

0
//my view pager 


mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     mViewPager.setOffscreenPageLimit(6); 

//onCreateView 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view; 
     view = inflater.inflate(R.layout.fragment_outstanding, container, false); 
     return view; 
    } 

//UserVisibleHint 

    public void setUserVisibleHint(boolean isVisibleToUser) { 
     super.setUserVisibleHint(isVisibleToUser); 
     if(isVisibleToUser) 
     { 
      OutstandingAdapterGenerator adapterGenerator_purchase = new OutstandingAdapterGenerator(); 
     adapterGenerator_purchase.setContext(getActivity()); 

     ListView listView = (ListView)getView().findViewById(R.id.outstandingPurchaseList) ; 


     adapterGenerator_purchase.setListViewResource(R.layout.list_item); 
     adapterGenerator_purchase.setPurchase(true); 
     adapterGenerator_purchase.setCompany(Company.getSelectedCompany()); 
     adapterGenerator_purchase.setViewIds(new int[]{R.id.textViewPartyName,R.id.textViewTotal}); 
     SimpleAdapter listAdapter = adapterGenerator_purchase.generateAdapter(); 
     listView.setAdapter(listAdapter); 

     } 
    } 
+0

**這解決了我的問題** – Narendhran