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!
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!!!
Related
I am attaching a fragment from a fragment that is attached to an activity and then inside the fragment i have set my custom toolbar with set setDisplayHomeUpEnabled to true. when am attaching the fragment i have set the Main activity toolbar to be hidden, the only problem is that it is not being unhidden when am returning to the parent fragment from the child fragment
package manu.apps.cartv6.Activities;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import manu.apps.cartv6.Classes.CartCounterConverter;
import manu.apps.cartv6.Classes.Config;
import manu.apps.cartv6.Classes.Product;
import manu.apps.cartv6.Classes.UserSessionManager;
import manu.apps.cartv6.Fragments.UserProfileFragment;
import manu.apps.cartv6.Fragments.ViewProductsFragment;
import manu.apps.cartv6.Interfaces.AddRemoveCallbacks;
import manu.apps.cartv6.R;
import manu.apps.cartv6.Tests.CartFragment;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener, AddRemoveCallbacks {
/** Lists*/
public static List<Product> cartList = new ArrayList<>();
/** Variables*/
// Counter for Cart
public static int cart_count = 0;
// Get Id for users
String getId;
// Time Interval for back button press
private static final int TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
// How long the back button has been pressed
private long backPressed;
//Getting Class Info
private static final String TAG = MainActivity.class.getSimpleName();
/** Views*/
// Toolbars
Toolbar toolbar;
// TextViews
TextView navHeaderUsername;
// ImageViews
ImageView navHeaderProfileImage;
/** Session Managing*/
UserSessionManager userSessionManager;
/** Navigation Drawer*/
//Declaring ActionBarDrawerToggle
private ActionBarDrawerToggle actionBarDrawerToggle;
//Declaring Navigation View
NavigationView navigationView;
//Declaring Drawer Layout
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Session Manager for the Users
userSessionManager = new UserSessionManager(this);
userSessionManager.checkUserLogin();
//Finding Drawer Layout
drawerLayout = findViewById(R.id.drawer_layout);
//Finding Navigation View
navigationView = findViewById(R.id.navigation_view);
//Finding Toolbar
toolbar = findViewById(R.id.main_tool_bar);
//Toolbar Workings
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Setting View Products Fragment
Fragment fragment = new ViewProductsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,fragment).commit();
//Setting ActionBarDrawerToggle to Open and Close
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
// Setting Navigation Item Selected Listener for Items in Navigation View
navigationView.setNavigationItemSelectedListener(this);
/**
* app:headerLayout="#layout/layout_nav_header"
*
* Remove the above line in NavigationView because we are inflating programmatically
*
* */
View navView = navigationView.inflateHeaderView(R.layout.layout_nav_header);
//Finding Views in Navigation Header which is inside Navigation View
navHeaderUsername = navView.findViewById(R.id.nav_header_username);
navHeaderProfileImage = navView.findViewById(R.id.nav_header_profile_image);
// Parsing Username and Email
HashMap<String, String> user = userSessionManager.getUserDetails();
getId = user.get(userSessionManager.ID);
}
// Fetch User Details
private void fetchUserDetails(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
//progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.FETCH_USER_DETAILS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.i(TAG, response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("fetch");
if (success.equals("1")) {
for (int i = 0; i < jsonArray.length();i++) {
JSONObject object = jsonArray.getJSONObject(i);
String username = object.getString("username").trim();
String email = object.getString("email").trim();
// Set Details for the Navigation Header
navHeaderUsername.setText(username);
//etProfileUsername.setText(username);
//etProfileEmail.setText(email);
}
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "JSON Error fetching Details", Toast.LENGTH_SHORT).show();
Snackbar.make(getWindow().getDecorView().getRootView(),e.toString(),Snackbar.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Volley Error fetching Details", Toast.LENGTH_SHORT).show();
Snackbar.make(getWindow().getDecorView().getRootView(),error.toString(),Snackbar.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String > params = new HashMap<>();
params.put("id", getId);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public static String numberFormatter(double d) {
DecimalFormat decimalFormat = new DecimalFormat("#,###.##");
return decimalFormat.format(d);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.user_toolbar_menu_items, menu);
MenuItem cartItem = menu.findItem(R.id.cart_action);
cartItem.setIcon(CartCounterConverter.convertLayoutToImage(MainActivity.this,cart_count,R.drawable.view_cart_icon));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Check if Action Bar Drawer Toggle has been selected
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()) {
case R.id.cart_action:
//startActivity(new Intent(MainActivity.this,CartActivity.class));
Fragment fragment = new CartFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,fragment).commit();
getSupportActionBar().hide();
break;
case R.id.logout_action:
userSessionManager.logOutUser();
finish();
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
/*switch (item.getItemId()) {
case R.id.action_category_one:
Toast.makeText(this, "Under Development", Toast.LENGTH_SHORT).show();
break;
case R.id.action_profile:
fragment = new UserProfileFragment();
break;
}*/
int id = item.getItemId();
if (id == R.id.action_view_products) {
fragment = new ViewProductsFragment();
}
// else if (id == R.id.action_receipts) {
// }
else if (id == R.id.action_profile) {
//startActivity(new Intent(MainActivity.this,UserProfileActivity.class));
fragment = new UserProfileFragment();
}
if (fragment !=null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
//R.id." " will be found in content_main.xml from the Frame Layout
ft.replace(R.id.frame_layout,fragment);
ft.commit();
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onClick(View v) {
}
// When back button is pressed method
#Override
public void onBackPressed() {
//Check if navigation drawer is opened or closed
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
if (backPressed + TIME_INTERVAL > System.currentTimeMillis()) {
super.onBackPressed();
return;
} else {
Toast.makeText(getBaseContext(), "Press back button again to exit", Toast.LENGTH_SHORT).show();
}
backPressed = System.currentTimeMillis();
}
}
#Override
protected void onResume() {
super.onResume();
fetchUserDetails();
}
// Remember to implement AddRemoveCallback Interface
#Override
public void onAddProduct() {
cart_count++;
invalidateOptionsMenu();
}
// Remember to implement AddRemoveCallback Interface
#Override
public void onRemoveProduct() {
cart_count--;
invalidateOptionsMenu();
}
}
My child fragment
package manu.apps.cartv6.Tests;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import manu.apps.cartv6.Activities.MainActivity;
import manu.apps.cartv6.Fragments.ViewProductsFragment;
import manu.apps.cartv6.R;
/**
* A simple {#link Fragment} subclass.
*/
public class CartFragment extends Fragment {
Toolbar fragmentCartToolbar;
public CartFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//((MainActivity) getActivity()).getSupportActionBar().hide();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_cart, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fragmentCartToolbar = view.findViewById(R.id.fragment_cart_tool_bar);
((MainActivity) getActivity()).setSupportActionBar(fragmentCartToolbar);
((MainActivity) getActivity()).getSupportActionBar().setTitle("Blank Fragment");
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
final Toolbar bar = (Toolbar) getActivity().findViewById(R.id.main_tool_bar);
fragmentCartToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity) getActivity()).setSupportActionBar(bar);
//((MainActivity) getActivity()).getSupportActionBar().show();
Fragment fragment = new ViewProductsFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,fragment).commit();
}
});
}
}
The problem is that the moment i add setNavigationOnClickListener the Main Activity toolbar doesn't show again when i remove it, it shows perfectly
#Override
public void onResume() {
super.onResume();
((MainActivity)getActivity()).getSupportActionBar().hide();
((MainActivity) getActivity()).setSupportActionBar(fragmentCartToolbar);
((MainActivity) getActivity()).getSupportActionBar().setTitle("Blank Fragment");
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
fragmentCartToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new ViewProductsFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,fragment).commit();
}
});
}
Please try to add below in your parent fragment's onResume(), I guess it's ViewProductsFragment from the provided code.
This will allow you to hide the action bar from the fragment itself rather than the activity.
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
I have made a View Pager along with some fragments and in those fragments i have used recycler view to show the Dynamic data fetched from the internet .I have used a Pull to Refresh also for my fragment .Sometimes due to slow internet connection when my fragment page gets loaded then it shows an awkard behaviour with my recycler view data..The data gets loaded in recycler view but it is scrollable behind my phone screen and a Recycler View "still" image is shown at the front without the scroll effect .Hence i am not able to function my Recycler view properly .When i try to scroll the page which is formed at the back is scrolled and the front page is "Still".
What might be the issue???
Fragment File
package com.global.market;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
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.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Asia extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
// ProgressDialog pDialog;
private RecyclerView mRecyclerView;
//private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private CountryDataAdapter adapter;
private ArrayList<CountriesData> data;
private SwipeRefreshLayout swipe;
public Asia(){
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// setContentView(R.layout.countrydata);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.countrydata, container, false);
// pDialog = new ProgressDialog(this.getContext(), ProgressDialog.THEME_HOLO_LIGHT);
//mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view1);
mRecyclerView = (RecyclerView)v.findViewById(R.id.my_recycler_view1);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(adapter);
// loadJSON();
swipe=(SwipeRefreshLayout)v.findViewById(R.id.swipe_refresh_layout);
swipe.setOnRefreshListener(this);
swipe.setColorSchemeColors(R.color.b, R.color.p, R.color.g, R.color.o);
swipe.post(new Runnable() {
#Override
public void run() {
swipe.setRefreshing(true);
loadRetro();
}
}
);
return v;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
boolean x=isNetworkAvailable();
if(x)
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
// pDialog.show();
// swipe.setRefreshing(true);
{
loadRetro();
return true;
}
else Toast.makeText(getActivity(), "No Internet Connection!",
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onRefresh(){
// swipe.setRefreshing(true);
loadRetro();
//pDialog.dismiss();
}
public void loadRetro(){
swipe.setRefreshing(true);
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
// pDialog.show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.appuonline.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonrequest request = retrofit.create(jsonrequest.class);
Call<JsonResponse> call = request.getJSON();
call.enqueue(new Callback<JsonResponse>() {
#Override
public void onResponse(Call<JsonResponse> call, Response<JsonResponse> response) {
try{
JsonResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getData()));
adapter = new CountryDataAdapter(data);
mRecyclerView.setAdapter(adapter);
// if (pDialog.isShowing())
// pDialog.dismiss();
swipe.setRefreshing(false);
}catch (Exception e){}
}
#Override
public void onFailure(Call<JsonResponse> call, Throwable t) {
try{
Log.d("Error1", "no connection");
swipe.setRefreshing(false);}
catch(Exception e){}
}
});
}
}
Do tell me if some other code is required for further clarification
This is my Screenshot .
The content which you can see in the above screenshot sometimes becomes "still" and the whole content as it is becomes active at the back of this page and the front page content does not works .
You set new Adapter in onRefresh method again which is not recommended. try to add new Items in your adapter with method like this:
In your onCreateView initilize your Adapter with something like this:
adapter = new MyAdapter(getActivity());
Also put this method inside your adapter:
public void addItems(List<Object> newList){
//put this method in your adapter
adapterList = newList;
notifyDatasetChanged();
//it's better to call notifyItemRangeInserted()
}
And in your pull to refresh when new data comes add it your adapter. something like this:
adapter.addItems(your_new_fetch_list_data);
Also use this design pattern for retrofit may help you your code become cleaner.
I have to add more code in following class, but I need to separate handleMenuSearch() in another class to organize my code. I can't call getSupportActionBar() in the separated class. How do I get support ActionBar in another class?
NewsakHome.java
package com.newsak;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Bassem on 8/26/2015.
*/
public class NewsakHome extends AppCompatActivity {
private Toolbar toolBar;
private MenuItem searchItem;
private EditText searchText;
private boolean isSearchOpened = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsak_welcome);
toolBar = (Toolbar) findViewById(R.id.tool_bar_newsak);
// toolBar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolBar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.search_bar_id:
handleMenuSearch();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
searchItem = menu.findItem(R.id.search_bar_id);
return super.onPrepareOptionsMenu(menu);
}
protected void handleMenuSearch(){
ActionBar actionBar = getSupportActionBar();
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(isSearchOpened) // is cloases
{
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
// hide the Keyboard
inputManager.hideSoftInputFromWindow(searchText.getWindowToken() ,0);
//set the closed Icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_open));
} else
{
searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_open));
}
isSearchOpened = false;
}
else
{
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.search_bar);
actionBar.setDisplayShowCustomEnabled(true); // set our search layout
searchText = (EditText) actionBar.getCustomView().findViewById(R.id.search_text_id);
searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
doSearch(view.getText().toString());
return true;
}
return false;
}
});
searchText.requestFocus();
//open Keyboard
inputManager.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
//set close icon
//set the closed Icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_closed));
} else
{
searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_closed));
}
isSearchOpened = true;
}
}
//You’d add this method too, to close the search entry with the backbutton:
public void onBackPressed() {
if(isSearchOpened)
{
handleMenuSearch();
return;
}
super.onBackPressed();
}
private void doSearch(String value) {
Toast.makeText(this, "your value is :"+value, Toast.LENGTH_LONG).show();
}
}
You could pass a reference of NewsakHome into your menu function, something like this:
// MenuSearchClass.java
public class MenuSearchClass() {
public static void handleMenuSearch(NewsakHome context) {
// use 'context' whenever you want to access one of NewsakHome's methods/members
ActionBar actionBar = context.getSupportActionBar();
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
...
}
}
Then, pass this into handleMenuSearch() whenever you call it:
// NewsakHome.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.search_bar_id:
// 'this' will become 'context' in handleMenuSearch()
// handleMenuSearch() is static to make the example easier to read
MenuSearchClass.handleMenuSearch(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I do agree with #Peter Brittain though. You shouldn't be splitting a class just to make it smaller or more readable. Activity classes usually handle a lot of functionality, so it makes sense that they'd be some of the larger, more complex classes in your project.
I'm working on a note recognition software, and, I use a Thread.sleep to ensure a pitch is consistent enough over a period to qualify as a note rather than noise. This seems to be making my app skip frames though, and pretty much breaks the program. How can I avoid this?
package com.example.miguel.guitartab;
import be.tarsos.dsp.AudioDispatcher;
import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.io.android.AudioDispatcherFactory;
import be.tarsos.dsp.pitch.PitchDetectionHandler;
import be.tarsos.dsp.pitch.PitchDetectionResult;
import be.tarsos.dsp.pitch.PitchProcessor;
import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Song song = new Song();
private boolean finished = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tarsos_dsp);
AudioDispatcher dispatcher = AudioDispatcherFactory
.fromDefaultMicrophone(22050,1024,0);
dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, new PitchDetectionHandler() {
#Override
public void handlePitch(final PitchDetectionResult pitchDetectionResult,
AudioEvent audioEvent) {
runOnUiThread(new Runnable() {
#Override
public void run() {
final float pitch = pitchDetectionResult.getPitch();
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("" + pitch);
if (pitch>700) {
song = new Song();
finished = false;
} else if (finished){
text.setText("Song finished");
} else {
try{
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
float pitch2 = pitchDetectionResult.getPitch();
if (pitch * 0.99 < pitch2 && pitch * 1.01 > pitch2) {
if (pitch + 2 > song.notes.get(0) && pitch - 2 < song.notes.get(0)) {
System.out.println("Found pitch: " +song.notes.get(0));
text.setTextColor(0xFF00FF00);
song.notes.remove(0);
if (song.notes.size()==0){
finished = true;
text.setTextColor(0xFFFF0000);
}
} else {
if (finished = true){
text.setTextColor(0xFFFF0000);
} else {
text.setTextColor(0xFF00FF00);
}
}
}
}
}
});
}
}));
new Thread(dispatcher,"Audio Dispatcher").start();
}
#Override
public void onBackPressed()
{
moveTaskToBack(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tarsos_ds, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tarsos_ds,
container, false);
return rootView;
}
}
}
Before I start, yes I have read countless related questions. I still can't seem to track down the issue.
I have a SherlockFragmentActivity:
package com.kicklighterdesignstudio.floridaday;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public class FragmentActivity extends BaseActivity {
public static final String TAG = "FragmentActivity";
public static final int SCHEDULE_FRAGMENT = 0;
public static final int MAP_FRAGMENT = 1;
public static final int FOOD_FRAGMENT = 2;
public static final int TWITTER_FRAGMENT = 3;
public static final int HASHTAG_FRAGMENT = 4;
private Fragment mContent;
public FragmentActivity() {
super(R.string.main_activity_title);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame);
switchContent(SCHEDULE_FRAGMENT);
setBehindContentView(R.layout.menu_frame);
}
public void switchContent(int id) {
getFragment(id);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent)
.commit();
getSlidingMenu().showContent();
}
private void getFragment(int id) {
switch (id) {
case 0:
mContent = new ScheduleFragment();
break;
case 1:
mContent = new FoodFragment();
break;
case 2:
mContent = new MapFragment();
break;
case 3:
mContent = new TwitterFragment();
break;
case 4:
mContent = new HashtagFragment();
break;
}
}
}
It extends BaseActivity:
package com.kicklighterdesignstudio.floridaday;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
public class BaseActivity extends SlidingFragmentActivity {
// private int mTitleRes;
protected ListFragment mFrag;
public BaseActivity(int titleRes) {
// mTitleRes = titleRes;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setTitle(mTitleRes);
setTitle("");
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new MenuFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(
R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
sm.setBehindScrollScale(0.0f);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Right now, I'm only concerned with ScheduleFragment:
package com.kicklighterdesignstudio.floridaday;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class ScheduleFragment extends SherlockFragment implements OnItemClickListener {
public static final String TAG = "ScheduleFragment";
private ArrayList<ScheduleItem> schedule;
private FloridaDayApplication app;
public ScheduleFragment() {
// setRetainInstance(true);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
app = (FloridaDayApplication) activity.getApplication();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.schedule_fragment, container, false);
}
#Override
public void onStart() {
super.onStart();
getSherlockActivity().getSupportActionBar().setTitle(R.string.schedule_fragment);
schedule = app.getSchedule();
ListView scheduleListView = (ListView) getActivity().findViewById(R.id.schedule_list);
ScheduleItemAdapter adapter = new ScheduleItemAdapter(getActivity(), schedule);
scheduleListView.setAdapter(adapter);
scheduleListView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> a, View v, int id, long position) {
Fragment newFragment = new ScheduleItemFragment(id);
FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, newFragment).addToBackStack(null).commit();
}
}
ScheduleFragment displays a list of schedule items. When clicked, a new fragment is displayed (ScheduleItemFragment) to show details of the item and a map to its location:
package com.kicklighterdesignstudio.floridaday;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.MarkerOptions;
#SuppressLint("ValidFragment")
public class ScheduleItemFragment extends SherlockFragment {
public static final String TAG = "ScheduleItemFragment";
private int scheduleItemId;
private FloridaDayApplication app;
private ScheduleItem scheduleItem;
private MapView mapView;
private GoogleMap map;
public ScheduleItemFragment(int id) {
scheduleItemId = id;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
app = (FloridaDayApplication) activity.getApplication();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.schedule_item_fragment, container, false);
setHasOptionsMenu(true);
// Initialize MapView
mapView = (MapView) v.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
return v;
}
#Override
public void onStart() {
super.onStart();
scheduleItem = app.getSchedule().get(scheduleItemId);
getSherlockActivity().getSupportActionBar().setTitle(scheduleItem.getTitle());
// Map Stuff
map = mapView.getMap();
if (map != null) {
map.getUiSettings();
map.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Add marker to the map
MarkerOptions options = new MarkerOptions().position(scheduleItem.getLocation().getPosition()).title(
scheduleItem.getLocation().getTitle());
map.addMarker(options);
// Adjust Camera Programmatically
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
scheduleItem.getLocation().getPosition(), 14);
map.animateCamera(cameraUpdate);
} else {
Log.i(TAG, "Map is null");
}
// Other Views
TextView title = (TextView) getSherlockActivity().findViewById(R.id.title);
TextView time = (TextView) getSherlockActivity().findViewById(R.id.time);
TextView description = (TextView) getSherlockActivity().findViewById(R.id.description);
title.setText(scheduleItem.getTitle());
time.setText(scheduleItem.getDateTimeString());
description.setText(scheduleItem.getDescription());
}
// TODO: Make this work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getFragmentManager().popBackStack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
#Override
public void onLowMemory() {
mapView.onLowMemory();
super.onLowMemory();
}
#Override
public void onPause() {
mapView.onPause();
super.onPause();
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onSaveInstanceState(Bundle outState) {
mapView.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
}
Thanks to BaseActivity, the icon in my ActionBar is clickable. It toggles the Sliding Menu. When viewing a ScheduleItemFragment, I would like the icon to return to the previous item in the backstack (which you can see I'm trying to do.) No matter what I try, the icon always toggles the Sliding Menu. Any thoughts on how to guarantee my Fragment gains control of the ActionBar menu clicks?
you need to call setHasOptionsMenu(true); in onCreate, not onCreateView
also I'm interested what happens when you debug, does onOptionsItemSelected still get called?
edit: add the following to your fragment
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
edit1:
there may be a better way to do this but I'm not exactly sure how your fragments are setup out, create a public field in baseActivity public bool isItemFragmentOnTop
now onOptionsItemSelected in the case of the home button getting press do this
if (isItemFragmentOnTop){
getFragmentManager().popBackStack();
isItemFragmentOnTop = false;
} else {
toggle();
}
return true;
then in your fragment you can call ((BaseActivity)getActivity).isItemFragmentOnTop = true; to make the home button pop the back stack, you would want to do this when you display your fragment onItemClick in your list view.