我创建了带有五个片段的底部导航,在单击选项卡时不会一次又一次地重新创建。这样做是为了减少 API 调用的数量。在所有片段中都存在回收器视图,并且还实现了拉动刷新。当我拉动刷新,从服务器获取数据,但回收站视图没有更新时,问题就出现了。请阐明做错了什么。
底部导航类
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_std_bottom_bar);
studentSchoolDataBaseController = Application.getInstance().getStudentSchoolDataBaseController();
studentApiController = new StudentApiController(this);
studentAbsenceApiController = new StudentAbsenceApiController(this);
studentCoursesApiController = new StudentCoursesApiController(this);
studentBottomNavView = (BottomNavigationView) findViewById(R.id.navigation1);
CustomFont.cairo_font(getApplicationContext(), studentBottomNavView);
if (getIntent().getExtras() != null) {
notificationType = getIntent().getExtras().getString("type");
comesFromNotification = true;
msgFrom = getIntent().getExtras().getString(StaticConfig.CHAT_MSG_USER_TYPE);
}
fragmentManager.beginTransaction().add(R.id.main_container, studCurrCourseFragment, "1").hide(studCurrCourseFragment).commit();
fragmentManager.beginTransaction().add(R.id.main_container, stdPointsFragment, "2").hide(stdPointsFragment).commit();
fragmentManager.beginTransaction().add(R.id.main_container, stdHome, "3").commit();
fragmentManager.beginTransaction().add(R.id.main_container, stdTaskAndHw, "4").hide(stdTaskAndHw).commit();
fragmentManager.beginTransaction().add(R.id.main_container, stdRealtimeChat, "5").hide(stdRealtimeChat).commit();
showLoadingDialog();
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//hide Progressbar here or close your activity.
if(progressDialog!=null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
},10*1000);
BottomNavigationViewHelper.removeNavigationShiftMode(studentBottomNavView);
studentBottomNavView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
Fragment fragment = null;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_std_current_course:
fragmentManager.beginTransaction().hide(active).show(studCurrCourseFragment).commit();
active = studCurrCourseFragment;
isHome = false;
return true;
case R.id.navigation_std_points:
fragmentManager.beginTransaction().hide(active).show(stdPointsFragment).commit();
active = stdPointsFragment;
isHome = false;
return true;
case R.id.navigation_std_home:
fragmentManager.beginTransaction().hide(active).show(stdHome).commit();
active = stdHome;
isHome = true;
return true;
case R.id.navigation_std_task_hw:
fragmentManager.beginTransaction().hide(active).show(stdTaskAndHw).commit();
active = stdTaskAndHw;
isHome = false;
return true;
case R.id.navigation_chat
fragmentManager.beginTransaction().hide(active).show(stdRealtimeChat).commit();
active = stdRealtimeChat;
isHome = false;
return true;
}
return false;
}
});
studentBottomNavView.setOnNavigationItemReselectedListener(new
BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
//Toast.makeText(BottomStdNavigation.this, "Reselected",
Toast.LENGTH_SHORT).show();
}
});
setNavigationTypeface();
}
在下面添加我的片段类之一
标准点.java
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new ActionBarCustomization(getActivity(), "", R.layout.include_navbar_add_points);
initializeView();
}
@Override
public void onStart() {
super.onStart();
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_std_points, container, false);
findViews(rootView);
pointsReplaceBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), StudentPointsReplaceActivity.class);
startActivity(intent);
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
initializeView();
swipeRefreshLayout.setRefreshing(false);
}
});
return rootView;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
private void initializeView() {
// new ActionBarCustomization(getActivity(), "", R.layout.include_toolbar_back);
studentPointsApiController = new StudentPointsApiController(getActivity());
getPointsDetails();
getReplacePointsRequests();
}
private void findViews(View view) {
recyclerView = (RecyclerView)view. findViewById(R.id.student_points_details_recycler_view);
pointsReplacerecyclerView = (RecyclerView) view.findViewById(R.id.student_points_replace_sent_requests_recycler_view);
pointsReplacerecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, true));
swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.std_pts_refresh_id);
studentPointsDetailsTotal = (CustomTextView)view.findViewById(R.id.student_points_details_total);
studentPointsDetailsReplaced = (CustomTextView)view. findViewById(R.id.student_points_details_replaced);
studentPointsDetailsRemain = (CustomTextView)view. findViewById(R.id.student_points_details_remain);
pointsReplaceBtn = (CustomButton)view.findViewById(R.id.addStdPointBtnID);
}
private void initializeRecyclerAdapter() {
studentPointsDetailsRecyclerAdapter = new StudentPointsDetailsRecyclerAdapter(getActivity(), studentBalance.getBalanceDetails());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(studentPointsDetailsRecyclerAdapter);
//setData();
}
public void getPointsDetails() {
studentPointsApiController.getPointsDetails(new ObjectCallback() {
@Override
public void onSuccess(Object o) {
studentBalance = ((StudentBalance) o);
setData();
initializeRecyclerAdapter();
}
@Override
public void onFailed() {
}
});
}
private void getReplacePointsRequests() {
studentPointsApiController.getStudentPointsReplaceRequests(new ListCallback() {
@Override
public void onSuccess(List<Object> objects) {
replacedPoints.clear();
replacedPoints.addAll((List<ReplacedPoints>) (List<?>) objects);
pointsReplaceRequestsRecyclerAdapter = new
PointsReplaceRequestsRecyclerAdapter(getContext(),replacedPoints, StdPoints.this);
// RecyclerView.LayoutManager mLayoutManager1 = new
LinearLayoutManager(getContext());
// recyclerView.setLayoutManager(mLayoutManager1);
pointsReplacerecyclerView.setItemAnimator(new DefaultItemAnimator());
pointsReplacerecyclerView.setAdapter(pointsReplaceRequestsRecyclerAdapter);
}
@Override
public void onFail() {
}
});
}
适配器类
StudentPointsDetailsReclyerAdaper.java
private Context context;
private List<BalanceDetails> balanceDetails = new ArrayList<>();
public StudentPointsDetailsRecyclerAdapter(Context context, List<BalanceDetails>
balanceDetails) {
this.context = context;
this.balanceDetails = balanceDetails;
}
@NonNull
@Override
public StudentPointsDetailsHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_points_details, parent, false);
return new StudentPointsDetailsHolder(view, context);
}
@Override
public void onBindViewHolder(@NonNull StudentPointsDetailsHolder holder, int position) {
holder.setData(balanceDetails.get(position));
}
@Override
public int getItemCount() {
return balanceDetails.size();
}