片段重新加载问题

问题描述 投票:0回答:2

有一个活动的底部工具栏有四个选项卡。 每次点击以下选项卡时,都会将片段替换为容器框架布局。 在所有片段中都有一个 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);
        }

    }
}
android android-fragments bottomnavigationview
2个回答
0
投票

您有多种选择,例如使用新架构组件库的 ViewModel 通过单例对象访问数据。但如果我是你,我会使用带有实时数据选项的 ViewModel

如果你想使用单例解决方案,你可以访问像

这样的数据
 DataProvider.getInstance().loadHomeData(new RequestListener<Home>(){
   void onDataLoad(Home home){
   //...
 }
})

如果你想使用 ViewModel 你可以在 Google 官方页面

找到一个例子

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;

    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.