Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the data from the fragment to bottom sheet in android?

Tags:

java

android

I want to get data from the fragment listview and view it in a bottom sheet layout. I want to view like above attached image. Please help me.enter image description here

I done code for viewing the bottom sheet ,when user clicks the listview items the bottom sheet will be opened.In the bottom sheet the listview item details in the fragment should be viewed in a bottom sheet.The code for my project is attached below.

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_call_log, container, false);
    callLogList = (ListView) v.findViewById(R.id.callLogList);
    contactPhoto = (ImageView) v.findViewById(R.id.missedImage);

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getActivity());
    View bottomSheetView = inflater.inflate(R.layout.bottom_sheet,null);
    bottomSheetDialog.setContentView(bottomSheetView);


   String[] textString = new String[]{"Play", "Share", "Call", "add Notes","Add To Block","Delete"};
  int[]  drawableIds = new int[]{R.drawable.play_icon, R.drawable.share_icon, R.drawable.call_icon, R.drawable.add_notes_icon,
            R.drawable.block_icon,R.drawable.delete_icon};

    final ListView listbottom = (ListView)bottomSheetDialog.findViewById(R.id.listBottomSheets);
    CustomAdapter adapter = new CustomAdapter(this,  textString, drawableIds);
    listbottom.setAdapter(adapter);




    listbottom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {



            switch (position)
            {
                case 0:
                    Toast.makeText(getActivity(),"playing the song",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getActivity(),"sharing the song",Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getActivity(),"deleting the song",Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(getActivity(),"blocking the song",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(getActivity(),"nothing selected ",Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });


    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetView.getParent());
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    bottomSheetBehavior.setPeekHeight(320);

    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            Toast.makeText(getActivity(),"Hidden",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {

        }
    });

    callLogList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            bottomSheetDialog.show();
        }
    });

    setRetainInstance(true);

    return v;
}
like image 452
Prabha Karan Avatar asked Feb 17 '17 11:02

Prabha Karan


People also ask

How pass data from fragment to bottom sheet in Android?

You can add BottomSheetFragment in your nav_graph and pass data as an argument of action . And finally, get the result as mentioned in the document.

How do you pass data class between fragments?

To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.


2 Answers

You can pass entire contact object to bottom sheet fragment by to serializing and deserialize your contact object.

public class DetailFragment extends BottomSheetDialogFragment{ 
  private static final String DESCRIBABLE_KEY = "describable_key";
  private ContactModel contactToShow ;

  public static DetailFragment newInstance(ContactModel modelToPass) {
    DetailFragment bottomSheetFragment = new DetailFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable(DESCRIBABLE_KEY, modelToPass);
    bottomSheetFragment .setArguments(bundle);

    return bottomSheetFragment ;
  } 

  @Override 
  public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

    //Deserilize contact object
    contactToShow = (ContactModel) getArguments().getSerializable(
        DESCRIBABLE_KEY);

    // The rest of your code to display detail of bill Gates

} 

You can afterward start bottomsheet fragment doing something like:

  FragmentTransaction transaction = ((FragmentActivity) context)
                            .getSupportFragmentManager()
                            .beginTransaction();

DetailFragment.newInstance(billGatesContactObject).show(transaction, "dialog_playback");

You can see working example here

https://github.com/dkim0419/SoundRecorder/blob/master/app/src/main/java/com/danielkim/soundrecorder/fragments/PlaybackFragment.java

Another dirty solution is to keep contact object in host Activity class and set and fetch contact object using ((HostActivity) getActivity).getContact() and ((HostActivity) getActivity).setContact(billGates) method.

like image 180
Hitesh Sahu Avatar answered Nov 14 '22 22:11

Hitesh Sahu


Whatever the data you have that you want to pass from your main listview to bottomsheet list, just pass those data in the bottomsheet adapter .
here in the line

CustomAdapter adapter = new CustomAdapter(this,  textString, drawableIds); 

make this change

CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet); 

where myDataArrayListToDisplayInBottomSheet is the ArrayList<> of the data that you have to display in the bottomSheet.

And in you CustomAdapter, use use this data to display accordingly.

like image 20
sohaib karim Avatar answered Nov 14 '22 23:11

sohaib karim