【问题标题】:Fragment Reloading Issue片段重新加载问题
【发布时间】: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


【解决方案1】:

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

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

 DataProvider.getInstance().loadHomeData(new RequestListener<Home>(){
   void onDataLoad(Home home){
   //...
 }
})

如果你想使用 ViewModel,你可以在 Google 的官方page找到一个例子

【讨论】:

    【解决方案2】:

    按照以下更新的代码:它正在工作

    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;
    
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-15
      • 2012-11-25
      • 2022-10-12
      • 2014-01-09
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多