仅在片段上从服务加载数据一次

时间:2016-10-18 09:29:07

标签: android android-fragments

我有一个片段,每次点击它时都会加载数据,我希望它的行为是这样的,如果我点击它一次并移动到另一个片段,当我回到它时,它不会重新加载数据并且它不再显示“加载微调器”。我怎么做?这是我的片段代码:

public ConsultantFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);//Make sure you have this line of code.
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View consultantView = inflater.inflate(R.layout.fragment_consultant, container, false);
    //consultantList = new ArrayList<>();
    //consultantLisView = (ListView) consultantView.findViewById(R.id.consultant_listview);
    swipeRefreshLayout = (SwipeRefreshLayout) consultantView.findViewById(R.id.consultant_list_swipe_refresh);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(false);
            retrieveConsultantList();    
            consultantRecyclerView.setAdapter(consultantRecylerViewAdapter);
        }
    });

    consultantRecyclerView = (RecyclerView) consultantView.findViewById(R.id.consultant_recyclerview);
    setUpDialog();

    FloatingActionButton createConsultantFab = (FloatingActionButton) consultantView.findViewById(R.id.add_consultant);
    createConsultantFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent createConsultantIntent = new Intent(getContext(), CreateConsultantActivity.class);
            startActivity(createConsultantIntent);

        }

    });

    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(R.string.consultant_list_title);

    retrieveConsultantList();

    return consultantView;
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // inflater.inflate(R.menu.main, menu);
    super.onCreateOptionsMenu(menu, inflater);


    searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);
    /// searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {

    consultantRecylerViewAdapter.getFilter().filter(newText);
    consultantRecylerViewAdapter.notifyDataSetChanged();
    return false;
}

private void setUpConsultantRecyclerView(List<Consultant> consultantList) {


    consultantRecylerViewAdapter = new ConsultantRecylerViewAdapter(getContext(), consultantList);
    consultantRecyclerView.setAdapter(consultantRecylerViewAdapter);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    consultantRecyclerView.setLayoutManager(linearLayoutManager);

    consultantRecyclerView.setItemAnimator(new DefaultItemAnimator());
}

public void setUpDialog() {
    consultantListLoadingDialog = ProgressDialog.show(getContext(),
            "Loading consultant",
            "Please wait");
}

private void retrieveConsultantList() {
    final StringRequest consultantStringRequest = new StringRequest(Request.Method.GET,
            Constants.BASE_URL.concat(Constants.CONSULTANT_KEY_WORD),
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //eventList =  new ArrayList<>();
                    JsonDataParser parseJSON = new JsonDataParser();
                    setUpConsultantRecyclerView(parseJSON.jsonArrayToConsultantList(response));
                    consultantListLoadingDialog.dismiss();
                    //consultantListAdapter.addAll();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

               }
         });
    VolleyRequestQueue.getInstance(getContext()).addToRequestQueue(consultantStringRequest);
}

}

1 个答案:

答案 0 :(得分:0)

从onCreateView中删除从服务器获取结果的方法。替换片段时,onCreateView始终调用。而是一次在onCreate上工作。

@Override
public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
   retrieveConsultantList();
}