简体   繁体   中英

How to pass the data from the fragment to bottom sheet in 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. 在此处输入图片说明

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;
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM