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

时间:2017-02-17 11:11:06

标签: java android

我想从片段列表视图中获取数据并在底部工作表布局中查看它。我想查看上面附图。请帮助我。enter image description here

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

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

2 个答案:

答案 0 :(得分:2)

无论您想要从主列表视图传递到底部列表的数据是什么,只需将这些数据传递到底部页面适配器中即可。
在这里行 CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds);
做这个改变
CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet);


其中 myDataArrayListToDisplayInBottomSheet
是您必须在bottomSheet中显示的数据的 ArrayList&lt;&gt;

在您 CustomAdapter 中,使用此数据进行相应显示。

答案 1 :(得分:1)

您可以将整个联系人对象传递给底部片段,以序列化和反序列化您的联系人对象。

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

} 

您可以随后启动Bottomsheet片段,例如:

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

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

你可以在这里看到工作示例

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)方法设置和获取联系人对象。