【发布时间】:2018-06-20 12:20:55
【问题描述】:
有一个底部工具栏有四个选项卡的活动。 每次点击以下选项卡时,都会将片段替换为容器框架布局。 在所有片段中都有一个 API,数据从该 API 加载到片段中。但问题是如何避免数据已经加载后重新加载,这会影响用户体验。
public class HomeSellarActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout llMessage, llMe, llHome, llAppointment;
private ImageView ivHome, ivMessage, ivAppointment, ivMe;
private TextView tvHome, tvMessage, tvAppointment, tvMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sellar_home);
init();
HomePageFragment homePage = new HomePageFragment();
this.getFragmentManager().beginTransaction().replace(R.id.frameLayout, homePage).commit();
llMessage.setOnClickListener(this);
llMe.setOnClickListener(this);
llHome.setOnClickListener(this);
llAppointment.setOnClickListener(this);
}
private void init() {
ivHome = (ImageView) findViewById(R.id.ivHome);
ivMessage = (ImageView) findViewById(R.id.ivMessage);
ivAppointment = (ImageView) findViewById(R.id.ivAppointment);
ivMe = (ImageView) findViewById(R.id.ivMe);
llMessage = (LinearLayout) findViewById(R.id.llMessage);
llMe = (LinearLayout) findViewById(R.id.llMe);
llHome = (LinearLayout) findViewById(R.id.llHome);
llAppointment = (LinearLayout) findViewById(R.id.llAppointment);
tvHome = (TextView) findViewById(R.id.tvHome);
tvAppointment = (TextView) findViewById(R.id.tvAppointment);
tvMe = (TextView) findViewById(R.id.tvMe);
tvMessage = (TextView) findViewById(R.id.tvMessage);
}
@Override
public void onClick(View view) {
if (view == llMessage) {
tvMessage.setTextColor(Color.WHITE);
tvHome.setTextColor(getResources().getColor(R.color.blue_dark));
tvAppointment.setTextColor(getResources().getColor(R.color.blue_dark));
tvMe.setTextColor(getResources().getColor(R.color.blue_dark));
ivHome.setImageResource(R.drawable.home_icon_blue);
ivMessage.setImageResource(R.drawable.chat_icon_on_click);
ivAppointment.setImageResource(R.drawable.play_button_icon);
ivMe.setImageResource(R.drawable.user_icon);
MessagesFragment messages = new MessagesFragment();
this.getFragmentManager().beginTransaction().replace(R.id.frameLayout, messages).commit();
}
if (view == llMe) {
tvMessage.setTextColor(getResources().getColor(R.color.blue_dark));
tvHome.setTextColor(getResources().getColor(R.color.blue_dark));
tvAppointment.setTextColor(getResources().getColor(R.color.blue_dark));
tvMe.setTextColor(Color.WHITE);
ivHome.setImageResource(R.drawable.home_icon_blue);
ivMessage.setImageResource(R.drawable.chat_icon);
ivAppointment.setImageResource(R.drawable.play_button_icon);
ivMe.setImageResource(R.drawable.user_icon_selected_copy);
AccountHomeFragment accountHome = new AccountHomeFragment();
this.getFragmentManager().beginTransaction().replace(R.id.frameLayout, accountHome).commit();
}
if (view == llHome) {
tvMessage.setTextColor(getResources().getColor(R.color.blue_dark));
tvHome.setTextColor(Color.WHITE);
tvAppointment.setTextColor(getResources().getColor(R.color.blue_dark));
tvMe.setTextColor(getResources().getColor(R.color.blue_dark));
ivHome.setImageResource(R.drawable.home_icon);
ivMessage.setImageResource(R.drawable.chat_icon);
ivAppointment.setImageResource(R.drawable.play_button_icon);
ivMe.setImageResource(R.drawable.user_icon);
HomePageFragment homePageFragment = new HomePageFragment();
this.getFragmentManager().beginTransaction().replace(R.id.frameLayout, homePageFragment).commit();
}
if (view == llAppointment) {
tvMessage.setTextColor(getResources().getColor(R.color.blue_dark));
tvHome.setTextColor(getResources().getColor(R.color.blue_dark));
tvAppointment.setTextColor(Color.WHITE);
tvMe.setTextColor(getResources().getColor(R.color.blue_dark));
ivHome.setImageResource(R.drawable.home_icon_blue);
ivMessage.setImageResource(R.drawable.chat_icon);
ivAppointment.setImageResource(R.drawable.music_icon);
ivMe.setImageResource(R.drawable.user_icon);
Services services = new Services();
this.getFragmentManager().beginTransaction().replace(R.id.frameLayout, services).commit();
}
}
片段代码之一:
public class HomePageFragment extends Fragment implements View.OnClickListener {
private RecyclerView rvServices;
private RecentServicesAdapter recentServicesAdapter;
private ArrayList<String> list;
private ProgressDialog progressBar;
private ImageView ivMailBox, ivProfileImage;
private TextView tvStatus, tvMasterName, tvTitleName, tvTransactionYesterday, tvTransactionWeek, tvTransactionMonth, tvTransactionAmtYesterday, tvTransactionAmtWeek, tvTransactionAmtMonth;
private SwitchButton switch_button;
private Button btnIncomeRecord;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_homepage, container, false);
rvServices = (RecyclerView) view.findViewById(R.id.rvServices);
btnIncomeRecord = (Button) view.findViewById(R.id.btnIncomeRecord);
ivMailBox = (ImageView) view.findViewById(R.id.ivMailBox);
tvStatus = (TextView) view.findViewById(R.id.tvStatus);
switch_button = (SwitchButton) view.findViewById(R.id.switch_button);
tvMasterName = (TextView) view.findViewById(R.id.tvMasterName);
tvTitleName = (TextView) view.findViewById(R.id.tvTitleName);
tvTransactionMonth = (TextView) view.findViewById(R.id.tvTransactionMonth);
tvTransactionWeek = (TextView) view.findViewById(R.id.tvTransactionWeek);
tvTransactionYesterday = (TextView) view.findViewById(R.id.tvTransactionYesterday);
tvTransactionAmtMonth = (TextView) view.findViewById(R.id.tvTransactionAmtMonth);
tvTransactionAmtWeek = (TextView) view.findViewById(R.id.tvTransactionAmtWeek);
tvTransactionAmtYesterday = (TextView) view.findViewById(R.id.tvTransactionAmtYesterday);
ivProfileImage = (ImageView) view.findViewById(R.id.ivProfileImage);
progressBar = new ProgressDialog(getActivity());
callApi();
ivMailBox.setOnClickListener(this);
btnIncomeRecord.setOnClickListener(this);
return view;
}
private void callApi() {
progressBar.show();
progressBar.setMessage("Getting HomePage Data");
progressBar.setCancelable(false);
if (NetworkHelper.isNetworkAvailable(getActivity())) {
ApiSellarConnection.getHomePageData(AppSharedPref.getCustomerId(getActivity()), AppSharedPref.getStoreId(getActivity())).enqueue(new Callback<HomePageResponse>() {
@SuppressLint("SetTextI18n")
@Override
public void onResponse(Call<HomePageResponse> call, Response<HomePageResponse> response) {
if (response.code() == 200) {
if (response.body().getSellerData().getActiveSellerStatus() == 0) {
tvStatus.setText("Offline");
switch_button.setChecked(false);
}
if (response.body().getSellerData().getActiveSellerStatus() == 1) {
tvStatus.setText("Online");
switch_button.setChecked(true);
}
Glide.with(getActivity()).load(response.body().getSellerData().getSellerLogo()).apply(new RequestOptions().placeholder(R.drawable.pro)).into(ivProfileImage);
tvMasterName.setText(response.body().getSellerData().getUserName());
tvTitleName.setText(response.body().getSellerData().getTitle());
tvTransactionMonth.setText("" + response.body().getMonthlyStatitics().getNoOfTransactions());
tvTransactionAmtMonth.setText(response.body().getMonthlyStatitics().getTotalSale());
tvTransactionWeek.setText("" + response.body().getWeeklyStatitics().getNoOfTransactions());
tvTransactionAmtWeek.setText(response.body().getWeeklyStatitics().getTotalSale());
tvTransactionYesterday.setText("" + response.body().getYesterdayStatitics().getNoOfTransactions());
tvTransactionAmtYesterday.setText(response.body().getYesterdayStatitics().getTotalSale());
recentServicesAdapter = new RecentServicesAdapter(response.body().getRecentOrderList(), getActivity());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
rvServices.setLayoutManager(mLayoutManager);
rvServices.setItemAnimator(new DefaultItemAnimator());
rvServices.setAdapter(recentServicesAdapter);
rvServices.setFocusable(false);
rvServices.setNestedScrollingEnabled(false);
} else {
Toast.makeText(getActivity(), "Response Code " + response.code(), Toast.LENGTH_LONG).show();
}
progressBar.cancel();
}
@Override
public void onFailure(Call<HomePageResponse> call, Throwable t) {
progressBar.cancel();
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
progressBar.cancel();
Toast.makeText(getActivity(), "Poor Internet Connection", Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View view) {
if (view == ivMailBox) {
Intent intent = new Intent(getActivity(), MailBoxActivity.class);
startActivity(intent);
}
if (view == btnIncomeRecord) {
Intent intent = new Intent(getActivity(), MyIncomeActivity.class);
intent.putExtra("transactYesterday", tvTransactionYesterday.getText());
intent.putExtra("transactWeek", tvTransactionWeek.getText());
intent.putExtra("transactMonth", tvTransactionMonth.getText());
intent.putExtra("transactAmtYesterday", tvTransactionAmtYesterday.getText());
intent.putExtra("transactAmtWeek", tvTransactionAmtWeek.getText());
intent.putExtra("transactAmtMonth", tvTransactionAmtMonth.getText());
startActivity(intent);
}
}
}
【问题讨论】:
-
您需要发布一些代码,因为不清楚您正在使用哪些元素。通常您可以像在
ViewPager中一样设置offscreenPageLimit并防止您描述的延迟加载...您也可以使用Fragment的相同实例而不是创建一个新实例在每次标签点击时。 -
@HedShafran 不,我没有使用视图寻呼机,每次点击都有自定义底部栏,加载了一个片段。在这种情况下如何避免重新加载数据。
-
确保每个
Fragment实例只创建一次,并重复使用现有实例。但是没有看到代码,我无法告诉你要更改什么... -
这个问题可以按照fragment的生命周期来管理吗?可以查看活动代码
-
您可以使用单独的对象获取数据(例如作为单个实例的管理器类)。这样你的数据与片段的生命周期无关。然后每个片段可以在需要时请求相关数据(和/或在数据准备好/刷新时监听回调).. 例如 ==>
MyManager.getInstance().getData()
标签: android api android-fragments fragment bottomnavigationview