Created
August 18, 2016 12:19
-
-
Save umanda/66af46da27a563532725c75925ab36cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Main Activity | |
public class MainActivity extends AppCompatActivity implements MaterialTabListener { | |
private MaterialTabHost tabHost; | |
private ViewPager viewPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_product); | |
setUpMaterialTab(); | |
} | |
private void setUpMaterialTab(){ | |
tabHost = (MaterialTabHost) findViewById(R.id.material_tabs); | |
viewPager = (ViewPager) findViewById(R.id.material_tab_view_pager); | |
MaterialTabPagerAdapter adapter = new MaterialTabPagerAdapter(getSupportFragmentManager()); | |
viewPager.setAdapter(adapter); | |
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { | |
@Override | |
public void onPageSelected(int position) { | |
// when user do a swipe the selected tab change | |
tabHost.setSelectedNavigationItem(position); | |
if(position == Constants.FAVORITES) { | |
//adapter.notifyDataSetChanged(); | |
} | |
} | |
}); | |
for (int i = 0; i < adapter.getCount(); i++) { | |
tabHost.addTab( | |
tabHost.newTab() | |
.setText(adapter.getPageTitle(i)) | |
.setTabListener(this) | |
); | |
} | |
} | |
@Override | |
public void onTabSelected(MaterialTab tab) { | |
// when the tab is clicked the pager swipe content to the tab position | |
viewPager.setCurrentItem(tab.getPosition()); | |
//Toast.makeText(getApplicationContext(),"onTabSelected" + tab ,Toast.LENGTH_SHORT).show(); | |
if(tab.getPosition() == Constants.FAVORITES){ | |
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); | |
Intent intent = new Intent(Constants.FAVORITES_TAB_SELECTED); | |
localBroadcastManager.sendBroadcast(intent); | |
} | |
} | |
@Override | |
public void onTabReselected(MaterialTab tab) { | |
viewPager.setCurrentItem(tab.getPosition()); | |
} | |
@Override | |
public void onTabUnselected(MaterialTab tab) { | |
if(tab.getPosition() == Constants.FAVORITES){ | |
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); | |
Intent intent = new Intent(Constants.FAVORITES_TAB_UN_SELECTED); | |
localBroadcastManager.sendBroadcast(intent); | |
} | |
} | |
/**************** | |
* Inner class decelerations | |
****************/ | |
public class MaterialTabPagerAdapter extends FragmentStatePagerAdapter { | |
String[] materialTabTitles; | |
public MaterialTabPagerAdapter(FragmentManager fm) { | |
super(fm); | |
materialTabTitles = getResources().getStringArray(R.array.material_tab_titles); | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return materialTabTitles[position]; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
Fragment fragment = null; | |
switch (position) { | |
case TAB_A: | |
fragment = TAB_A_Fragment.getInstance(position); | |
break; | |
case TAB_B: | |
fragment = TAB_B_Fragment.getInstance(position); | |
break; | |
case TAB_C: | |
fragment = TAB_C_Fragment.getInstance(position); | |
break; | |
} | |
return fragment; | |
} | |
@Override | |
public int getCount() { | |
return 3; | |
} | |
} | |
} | |
TAB_A_Fragment | |
public class TAB_A_Fragment extends Fragment { | |
ProductRecyclerAdapter adapter; | |
TextView volleyError; | |
ProductReceiver ProductReceiver; | |
private RecyclerView productRecyclerView; | |
private ArrayList<Product> productArrayList; | |
public static TAB_A_Fragment getInstance(int position) { | |
TAB_A_Fragment TAB_A_fragment = new TAB_A_Fragment(); | |
Bundle bundleArguments = new Bundle(); | |
bundleArguments.putInt("position", position); | |
productListFragment.setArguments(bundleArguments); | |
return productListFragment; | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putParcelableArrayList("products", productArrayList); | |
} | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
private void sendJSONRequest() { | |
// get Data from server | |
} | |
public void refresh() { | |
if (productArrayList != null){ | |
adapter = new ProductRecyclerAdapter(getActivity()); | |
productRecyclerView.setAdapter(adapter); | |
adapter.setData(productArrayList); | |
} | |
} | |
public void onPause() { | |
super.onPause(); | |
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(productReceiver); | |
} | |
public void onResume() { | |
super.onResume(); | |
productReceiver = new pProductReceiver(); | |
LocalBroadcastManager.getInstance(getContext()).registerReceiver(productReceiver,new IntentFilter(Constants.FAVORITES_TAB_UN_SELECTED)); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View layout = inflater.inflate(R.layout.fragment_product_list, container, false); | |
productRecyclerView = (RecyclerView) layout.findViewById(R.id.product_recycler_list); | |
// set the layout manager before trying to display data | |
productRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); | |
adapter = new ProductRecyclerAdapter(getActivity()); | |
productRecyclerView.setAdapter(adapter); | |
if (savedInstanceState != null) { | |
productArrayList = savedInstanceState.getParcelableArrayList("products"); | |
adapter.setData(productArrayList); | |
} else { | |
sendJSONRequest(); | |
} | |
return layout; | |
} | |
private class ProductReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
ProductListFragment.this.refresh(); | |
} | |
} | |
} | |
ProductListFragment | |
public class ProductRecyclerAdapter extends RecyclerView.Adapter<ProductRecyclerAdapter.pPoductViewHolder> { | |
List<pProduct> productList = new ArrayList<>(); | |
FavoritepProductSharedPreference favoriteProductSharedPreference; | |
private LayoutInflater layoutInflater; | |
private Context context; | |
public ProductRecyclerAdapter(Context context) { | |
layoutInflater = LayoutInflater.from(context); | |
this.context = context; | |
favoriteProductSharedPreference = new FavoriteProductSharedPreference(); | |
} | |
@Override | |
public PoductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = layoutInflater.inflate(R.layout.product_recycler_list_item, parent, false); | |
pProductViewHolder productViewHolder = new ProductViewHolder(view); | |
return productViewHolder; | |
} | |
@Override | |
public void onBindViewHolder(final pProductViewHolder holder, final int position) { | |
final Product productData = productList.get(position); | |
holder.name.setText(productData.getNickname()); | |
// When Recycle list load set favorite flags and update model | |
// If a product exists or not in shared preferences then set favorite on or off drawable | |
if (favoriteProductSharedPreference.checkFavoriteItem(productList.get(position).getProductId(), context)) { | |
productData.setFavorite(Constants.FAVORITE_ICON_ON); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_on); | |
} else { | |
productData.setFavorite(Constants.FAVORITE_ICON_OFF); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_off); | |
} | |
//Set Other data | |
holder.favoriteProductImg.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (productData.getFavorite() == Constants.FAVORITE_ICON_OFF) { | |
favoriteProductSharedPreference.addFavoriteProducts(context, productList.get(position), productList.get(position).getProductId()); | |
productData.setFavorite(Constants.FAVORITE_ICON_ON); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_on); | |
} else { | |
favoriteProductSharedPreference.removeFavoriteProducts(context, productList.get(position), productList.get(position).getProductId()); | |
productData.setFavorite(Constants.FAVORITE_ICON_OFF); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_off); | |
} | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return productList.size(); | |
} | |
public void setData(ArrayList<Product> productList) { | |
this.productList = productList; | |
notifyItemRangeChanged(0, productList.size()); | |
} | |
public class ProductViewHolder extends RecyclerView.ViewHolder { | |
TextView name; | |
public ProductViewHolder(View itemView) { | |
super(itemView); | |
name = (TextView) itemView.findViewById(R.id.name); | |
// other data | |
favoriteProductImg = (ImageView) itemView.findViewById(R.id.favorite_product); | |
} | |
} | |
} | |
Tab_B_Fragment | |
public class Tab_B_FragmentFragment extends Fragment { | |
FavoriteProductRecyclerAdapter adapter; | |
FavoriteProductSharedPreference favoriteProductSharedPreference; | |
FavoriteProductReceiver favoriteProductReceiver; | |
private RecyclerView favoriteProductRecyclerView; | |
private ArrayList<Product> favoriteProduct; | |
private TextView emptyView; | |
public static FavoriteProductListFragment getInstance(int position) { | |
FavoriteProductListFragment favoritesFragment = new FavoriteProductListFragment(); | |
Bundle bundleArguments = new Bundle(); | |
bundleArguments.putInt("position", position); | |
favoritesFragment.setArguments(bundleArguments); | |
return favoritesFragment; | |
} | |
public void refresh() { | |
if (favoriteProduct != null){ | |
favoriteProduct = favoriteProductSharedPreference.getAllFavoriteProducts(getActivity()); | |
adapter = new FavoriteProductRecyclerAdapter(getActivity()); | |
favoriteProductRecyclerView.setAdapter(adapter); | |
adapter.setData(favoriteProduct); | |
} | |
} | |
public void onPause() { | |
super.onPause(); | |
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(favoriteProductReceiver); | |
} | |
public void onResume() { | |
super.onResume(); | |
favoriteProductReceiver = new FavoriteProductReceiver(); | |
LocalBroadcastManager.getInstance(getContext()).registerReceiver(favoriteProductReceiver,new IntentFilter(Constants.FAVORITES_TAB_SELECTED)); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
favoriteProductReceiver = new FavoriteProductReceiver(); | |
LocalBroadcastManager.getInstance(getContext()).registerReceiver(favoriteProductReceiver,new IntentFilter(Constants.FAVORITES_TAB_SELECTED)); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
favoriteProductSharedPreference = new FavoriteProductSharedPreference(); | |
favoriteProduct = favoriteProductSharedPreference.getAllFavoriteProducts(getActivity()); | |
View layout = inflater.inflate(R.layout.fragment_favorites_list, container, false); | |
favoriteProductRecyclerView = (RecyclerView) layout.findViewById(R.id.product_favorite_recycler_list); | |
emptyView = (TextView) layout.findViewById(R.id.empty_view); | |
if (favoriteProduct == null) { | |
Toast.makeText(getContext(),"No Favorites",Toast.LENGTH_SHORT).show(); | |
favoriteProductRecyclerView.setVisibility(View.GONE); | |
emptyView.setVisibility(View.VISIBLE); | |
} | |
else { | |
favoriteProductRecyclerView.setVisibility(View.VISIBLE); | |
emptyView.setVisibility(View.GONE); | |
Toast.makeText(getContext(),"Have Favorites begin",Toast.LENGTH_SHORT).show(); | |
favoriteProductRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); | |
adapter = new FavoriteProductRecyclerAdapter(getActivity()); | |
favoriteProductRecyclerView.setAdapter(adapter); | |
adapter.setData(favoriteProduct); | |
} | |
return layout; | |
} | |
private class FavoriteProductReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
FavoriteProductistFragment.this.refresh(); | |
} | |
} | |
} | |
FavoriteProductRecyclerAdapter | |
public class FavoriteProductRecyclerAdapter extends RecyclerView.Adapter<FavoriteProductRecyclerAdapter.ProductViewHolder> { | |
List<Product> productList = new ArrayList<>(); | |
FavoriteProductSharedPreference favoriteProductSharedPreference; | |
private LayoutInflater layoutInflater; | |
private Context context; | |
public FavoriteProductRecyclerAdapter(Context context) { | |
layoutInflater = LayoutInflater.from(context); | |
this.context = context; | |
favoriteProductSharedPreference = new FavoriteProductSharedPreference(); | |
} | |
@Override | |
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = layoutInflater.inflate(R.layout.product_recycler_list_item, parent, false); | |
ProductViewHolder productViewHolder = new ProductViewHolder(view); | |
return ProductViewHolder; | |
} | |
@Override | |
public void onBindViewHolder(final ProductViewHolder holder, final int position) { | |
inal Product productData = productList.get(position); | |
holder.name.setText(productData.getNickname()); | |
// When Recycle list load set favorite flags and update model | |
// If a product exists or not in shared preferences then set favorite on or off drawable | |
if (favoriteProductSharedPreference.checkFavoriteItem(productList.get(position).getProductId(), context)) { | |
productData.setFavorite(Constants.FAVORITE_ICON_ON); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_on); | |
} else { | |
productData.setFavorite(Constants.FAVORITE_ICON_OFF); | |
holder.favoriteProductImg.setImageResource(R.drawable.ic_favorite_off); | |
} | |
//Set Other data | |
holder.favoriteProductImg.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (productData.getFavorite().equals(Constants.FAVORITE_ICON_ON)) { | |
Product conData; | |
if(productList.size() != 0 ){ | |
conData = productList.get(position); | |
favoriteProductSharedPreference.removeFavoriteProduct(context, productList.get(position), productList.get(position).getProductId()); | |
Toast.makeText(context, R.string.removed_from_favorite_list, Toast.LENGTH_SHORT).show(); | |
productData.setFavorite(Constants.FAVORITE_ICON_OFF); | |
removeItem(position, conData); | |
notifyItemRemoved(position); | |
} | |
} | |
} | |
}); | |
} | |
private void removeItem(int position , Product conData) { | |
productList.remove(position); | |
} | |
@Override | |
public int getItemCount() { | |
if(productList == null){ | |
return 0; | |
} else { | |
return productList.size(); | |
} | |
} | |
public void setData(ArrayList<Product>productList) { | |
this.ProductList = productList; | |
} | |
private void loadImages(final ProductViewHolder productViewHolder, String avatarImageUrl) { | |
if (!avatarImageUrl.equals(Constants.STRING_NA)) { | |
imageLoader.get(avatarImageUrl, new ImageLoader.ImageListener() { | |
@Override | |
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { | |
productViewHolder.avatarImage.setImageBitmap(response.getBitmap()); | |
} | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
} | |
}); | |
} | |
} | |
public class ProductViewHolder extends RecyclerView.ViewHolder { | |
TextView name; | |
public ProductViewHolder(View itemView) { | |
super(itemView); | |
name = (TextView) itemView.findViewById(R.id.name); | |
// other data | |
favoriteProductImg = (ImageView) itemView.findViewById(R.id.favorite_product); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment