android 选项卡式 Activity 列表片段

android tabbed Activity list fragment

我是 android 的新手,我有我的 TabActivity(操作栏选项卡)和一些片段。在第一个选项卡中,我有一个列表,当我切换到第二个选项卡时,我想在第二个选项卡中显示所选列表的内容。在我的代码中,当我在第一个选项卡中选择列表项时,它切换到第二个选项卡但内容没有更新但是当我转到 tab4 和 tab5 然后 return 到 tab2 时,我得到了内容更新在第二个标签中?不知道Tab怎么用?所以给我一个标签列表的例子 Fragment

My first Fragment (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;

}

My second fragment (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();
    }
}

在你的第二个片段中覆盖 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);
   }
}
//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);

        }
    }