有一个活动,该活动的底部工具栏包含四个选项卡。 在以下选项卡的每次点击上,都用容器框架布局替换一个片段。 在所有片段中,都有一个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);
}
}
}
答案 0 :(得分:0)
您有多种选择,例如使用新的体系结构组件库的ViewModel通过单例对象访问数据。但是如果我是我,我会使用带有实时数据选项的ViewModel
如果要使用单例解决方案,则可以访问
之类的数据。 DataProvider.getInstance().loadHomeData(new RequestListener<Home>(){
void onDataLoad(Home home){
//...
}
})
如果您想使用ViewModel,可以在Google官方网站上找到示例page
答案 1 :(得分:0)
按照以下更新的代码进行操作:
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;
private AccountHomeFragment accountHomeFragment = new AccountHomeFragment();
private MessagesFragment messagesFragment = new MessagesFragment();
private Services ser = new Services();
private HomePageFragment homePageFragment = new HomePageFragment();
private Fragment active = accountHomeFragment;
private final FragmentManager fm = getFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sellar_home);
init();
fm.beginTransaction().add(R.id.frameLayout, accountHomeFragment, "4").hide(accountHomeFragment).commit();
fm.beginTransaction().add(R.id.frameLayout, ser, "3").hide(ser).commit();
fm.beginTransaction().add(R.id.frameLayout, messagesFragment, "2").hide(messagesFragment).commit();
fm.beginTransaction().add(R.id.frameLayout, homePageFragment, "1").commit();
active = homePageFragment;
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);
fm.beginTransaction().hide(active).show(messagesFragment).commit();
active = messagesFragment;
}
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);
fm.beginTransaction().hide(active).show(accountHomeFragment).commit();
active = accountHomeFragment;
}
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);
fm.beginTransaction().hide(active).show(homePageFragment).commit();
active = homePageFragment;
}
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);
fm.beginTransaction().hide(active).show(ser).commit();
active = ser;
}
}
}