viewPager = (ViewPager) findViewById(R.id.tabanim_viewpager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabanim_tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
public void setupViewPager(ViewPager upViewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new ViewTodayFragment(),"Daily");
adapter.addFrag(new ViewWeekFragment(),"Weekly");
adapter.addFrag(new ViewMonthFragment(),"Monthly");
adapter.addFrag(new ViewYearFragment(),"Yearly");
viewPager.setAdapter(adapter);
}
我想在每次更改選項卡時刷新片段。在選項卡視圖中,我想刷新片段每次更改
內片段,這是我的代碼
public class ViewTodayFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
RecyclerView recyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
// TODO: Rename and change types of parameters
View v;
ListView listview;
private ArrayList<DailyModel> mItems;
public static String datesel="a";
public ViewTodayFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_view_today, container, false);
recyclerView =(RecyclerView)v.findViewById(R.id.view_today_lv_today);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
loadData();
return v;
}
@Override
public void onResume() {
super.onResume();
loadData();
}
public void loadData(){
mItems=new ArrayList<DailyModel>();
List<Income> ll = null;
Log.d("ALLL",datesel);
if(datesel.equals("a")){
Calendar cw = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
String currentDay=format1.format(cw.getTime());
ll=new IncomeHandler(getActivity()).getIncomeByThisDay(currentDay);
}else{
ll=new IncomeHandler(getActivity()).getIncomeByThisDay(datesel);
}
for (int i = 0; i < ll.size(); i++) {
DailyModel a=new DailyModel();
a.setCategory(ll.get(i).getWay());
a.setDescription(ll.get(i).getDes());
a.setType(ll.get(i).getType());
a.setBank("Bank");
a.setAmount(ll.get(i).getAmount());
mItems.add(a);
}
mAdapter = new DailyAdapter(mItems);
recyclerView.setAdapter(mAdapter);
}
}
所以像這樣的有4個選項卡,所以我想刷新每次改變tab.I嘗試做時間內的onResume(),但它也不能加工。
分享你的代碼,你在標籤佈局中添加片段.. –
好吧,我分享了它。 – Lakshan
查看我的回答下面 –