gpt4 book ai didi

android 选项卡式 Activity 列表 fragment

转载 作者:行者123 更新时间:2023-11-30 00:49:16 24 4
gpt4 key购买 nike

我是 android 的新手,我有我的 TabActivity(操作栏选项卡)和一些 fragment 。在第一个选项卡中,我有一个列表,当我切换到第二个选项卡时,我想在第二个选项卡中显示所选列表的内容。在我的代码中,当我在第一个选项卡中选择列表项时,它切换到第二个选项卡但内容没有更新但是当我转到 tab4 和 tab5 然后返回到 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();
}
}

最佳答案

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

关于android 选项卡式 Activity 列表 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41326482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com