Android Selectable Adapter

package com.resturant.vivaatech.resturantapp.Adapters;

import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.widget.Filter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by arifh_000 on 6/14/2016.
 */
public abstract class SelectableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    @SuppressWarnings("unused")
    private static final String TAG = SelectableAdapter.class.getSimpleName();

    private SparseBooleanArray selectedItems;



    public SelectableAdapter () {
        selectedItems = new SparseBooleanArray ();
    }

    /**
     * Indicates if the item at position position is selected
     * @param position Position of the item to check
     * @return true if the item is selected, false otherwise
     */
    public boolean isSelected(int position) {
        return getSelectedItems().contains(position);
    }

    /**
     * Toggle the selection status of the item at a given position
     * @param position Position of the item to toggle the selection status for
     */
    public void toggleSelection(int position) {
        if (selectedItems.get(position, false)) {
            selectedItems.delete(position);
        } else {
            selectedItems.put(position, true);
        }
        notifyItemChanged(position);

    }

    /**
     * Clear the selection status for all items
     */
    public void clearSelection() {
        List<Integer> selection = getSelectedItems();
        selectedItems.clear();
        for (Integer i : selection) {
            notifyItemChanged(i);
        }
    }

    /**
     * Count the selected items
     * @return Selected items count
     */
    public int getSelectedItemCount() {
        return selectedItems.size();
    }

    /**
     * Indicates the list of selected items
     * @return List of selected items ids
     */
    public List<Integer> getSelectedItems() {
        List<Integer> items = new ArrayList<>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); ++i) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }
}

How to prevent fragments from multiple instance

private void replaceFragment (Fragment fragment){
  String backStateName = fragment.getClass().getName();

  FragmentManager manager = getSupportFragmentManager();
  boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

  if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(backStateName);
    ft.commit();
  }
}


For my case:
I have declared it in  a class 

public static void replaceFragment (FragmentManager manager,Fragment fragment){

    String backStateName =  fragment.getClass().getName();
    String fragmentTag = backStateName;
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.fragmentContainer, fragment, fragmentTag);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();

    }
}

and used it as :

Functions.replaceFragment(getFragmentManager(),new OrderFoodTypeFragment());

Reference :

http://stackoverflow.com/questions/18305945/how-to-resume-fragment-from-backstack-if-exists/18306258#18306258

How to Add customize Android DialogFragment

public class ColorDialogFragment extends SherlockDialogFragment {

    public ColorDialogFragment() {
    //You need to provide a default constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
                     ViewGroup container,
                     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_color_picker, container);
    // R.layout.dialog_color_picker is the custom layout of my dialog
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.gravity = Gravity.LEFT;
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle);
    // this setStyle is VERY important.
    // STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
    // so for example the size of the default dialog will not get in my way
    // the style extends the default one. see bellow.        
    }

  }

<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style
i Have done it as :


<style name="colorPickerStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:cacheColorHint">@android:color/white</item>
    <item name="android:windowBackground">@android:color/white</item>
</style>
http://stackoverflow.com/questions/14946887/setting-the-size-of-a-dialogfragment

How to convert ArrayList to HashSet for eliminating duplicates

 

List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);

http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist

How to Send data from one Fragment to another fragment in android

http://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Bundle has put methods for lots of data types. See this

Then in your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);

How to Disable Navigation Drawer for specific fragments

1 . Create an interface first

public interface DrawerLocker {
    public void setDrawerEnabled(boolean enabled);
}


2. Implement interface on Activity that’s has navigation drawer / Container Activity

public class MainActivity extends Activity implements DrawerLocker {

    public void setDrawerEnabled(boolean enabled) {
        int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :
                                 DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
        drawer.setDrawerLockMode(lockMode);
        toggle.setDrawerIndicatorEnabled(enabled);
    }

    ...
}

3. Cast that interface on you specific fragments

((DrawerLocker) getActivity()).setDrawerEnabled(false);

 

RecyclerView OnTouch Listener

1. implements RecyclerView.OnItemTouchListener
2. final GestureDetector mGestureDetector = new GestureDetector(getActivity().getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {

@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}

});

3. mRecyclerView.addOnItemTouchListener(
new OrderFoodTypeFragment(getActivity().getApplicationContext(), new OrderFoodTypeFragment.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {

clickPosition =position;

}
})
);

 

http://sapandiwakar.in/recycler-view-item-click-handler/