如何将数据从片段传递到android中的底部工作表?

Pra*_*ran 6 java android

我想从片段列表视图中获取数据并在底部工作表布局中查看它。我想像上面附加的图像一样查看。请帮我。在此处输入图片说明

我完成了查看底部工作表的代码,当用户单击列表视图项目时,底部工作表将被打开。在底部工作表中,片段中的列表视图项目详细信息应在底部工作表中查看。我的项目的代码附在下面。

 @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;
}
Run Code Online (Sandbox Code Playgroud)

Hit*_*ahu 6

您可以通过序列化和反序列化您的联系人对象将整个联系人对象传递到底部工作表片段。

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

} 
Run Code Online (Sandbox Code Playgroud)

之后,您可以启动底部片断,执行以下操作:

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

DetailFragment.newInstance(billGatesContactObject).show(transaction, "dialog_playback");
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看工作示例

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

另一个肮脏的解决方案是将联系人对象保留在宿主 Activity 类中,并使用((HostActivity) getActivity).getContact()((HostActivity) getActivity).setContact(billGates)方法设置和获取联系人对象 。


soh*_*rim 5

无论您想要从主列表视图传递到底部表列表的数据是什么,只需将这些数据传递到底部表适配器中即可。
在此行

CustomAdapter adapter = new CustomAdapter(this,  textString, drawableIds); 
Run Code Online (Sandbox Code Playgroud)

做这个改变

CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet); 
Run Code Online (Sandbox Code Playgroud)

这里myDataArrayListToDisplayInBottomSheetArrayList<> 数据,你必须在bottomSheet显示。

而在你中CustomAdapter,使用这些数据进行相应的显示。