Android Fragments 碎片扩展问题

lyr7nygr  于 12个月前  发布在  Android
关注(0)|答案(2)|浏览(139)

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

    }
}

u1ehiz5o

u1ehiz5o1#

你有多种选择,例如通过单例对象访问数据,使用新架构组件库的ViewModel。
如果你想使用单例解决方案,你可以访问数据,如

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

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

9cbw7uwe

9cbw7uwe2#

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

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;

    }
}

字符串
}

相关问题