Reputation: 499
I use RecycleView in my application and I want to implement AsyncTask so it will work faster, because it takes a few seconds until fragment is loaded.
To be more specific, when clicking to enter the fragment which contains the RecyclerView, it hangs a few seconds and I understood that by using AsyncTask it will load only the items that shown on screen and only if I scroll down it will load the the next items. So it will fix the delaying...
How to do it please?
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import entities.Order;
import entities.User;
public class HomeFragment extends Fragment implements TransAdapter.ClickListener {
private RecyclerView rvTrans;
private TransAdapter adapter;
private MenuItem mSearchAction;
private SearchView searchView;
private ActionBar actionBar;
private boolean isSearchOpened = false;
private EditText etSearch;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
rvTrans = (RecyclerView) view.findViewById(R.id.rv_transactions);
rvTrans.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new TransAdapter(getActivity());
adapter.setClickListener(this);
rvTrans.setAdapter(adapter);
try {
adapter.setOrderList(HomeActivity.backEnd.booksForSale());
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_search:
handleMenuSearch();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mSearchAction = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(mSearchAction);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.clearData();
try {
adapter.setOrderList(HomeActivity.backEnd.bookGlobalSearch(query));
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
@Override
public void itemClicked(Order order) {
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtra("BOOK_PICTURE", order.getBookPicture());
intent.putExtra("BOOK_NAME", order.getBookName());
intent.putExtra("BOOK_AUTHOR", order.getAuthorName());
intent.putExtra("BOOK_GENRE", order.getGenre());
intent.putExtra("BOOK_PUBLISHING", order.getPublishingYear());
startActivity(intent);
}
private void handleMenuSearch() {
actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar(); //get the actionbar
if(isSearchOpened){ //test if the search is open
if(actionBar != null)
{
actionBar.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
actionBar.setDisplayShowTitleEnabled(true); //show the title in the action bar
}
// hides the keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
isSearchOpened = false;
} else { // open the search entry
if(actionBar != null)
{
actionBar.setDisplayShowCustomEnabled(true); //enable it to display a
// custom view in the action bar.
// action.setCustomView(R.layout.search_bar);//add the custom view
actionBar.setDisplayShowTitleEnabled(false); //hide the title
}
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
isSearchOpened = true;
}
}
}
Thanks!
Upvotes: 0
Views: 1481
Reputation: 387
May be you confuse using of async task.
Async task should be using in long operation to avoiding block UI thread (Ex. http request, pagination, etc.) But this no mean your businness logic will be run too fast (this will run in parallel). If in your recyclerView you load big image or image from url you need to manage directly your logic with cache pattern
you can start take a look how this logic work in project like picasso or Processing Bitmaps Off the UI Thread directly from google developers http://developer.android.com/training/displaying-bitmaps/process-bitmap.html#async-task
Hope this may help!!!
Upvotes: 1