android选项卡活动列表片段

时间:2016-12-26 04:36:53

标签: android android-fragments android-tabactivity

我是android的新手,我有TabActivity(操作栏标签)和一些片段。在第一个选项卡中,我有一个列表,当我切换第二个选项卡时,我想在第二个选项卡中显示所选列表的内容。在我的代码中,当我在第一个选项卡中选择列表项时,它切换到第二个选项卡,但内容没有更新,但是当我转到tab4和tab5然后返回到tab2时,我在第二个选项卡中获得了内容更新?我不知道Tab是如何工作的?那么给我一个示例选项卡列表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;

}
  

我的第二个片段(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();
    }
}

2 个答案:

答案 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);
   }
}

答案 1 :(得分: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);

        }
    }