[英]How to pass the data from the fragment to bottom sheet in 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;
}
您可以通过序列化和反序列化您的联系人对象将整个联系人对象传递到底部工作表片段。
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
}
之后,您可以启动底部片断,执行以下操作:
FragmentTransaction transaction = ((FragmentActivity) context)
.getSupportFragmentManager()
.beginTransaction();
DetailFragment.newInstance(billGatesContactObject).show(transaction, "dialog_playback");
您可以在此处查看工作示例
另一个肮脏的解决方案是将联系人对象保留在宿主 Activity 类中,并使用((HostActivity) getActivity).getContact()
和((HostActivity) getActivity).setContact(billGates)
方法设置和获取联系人对象。
无论您想要从主列表视图传递到底部表列表的数据是什么,只需将这些数据传递到底部表适配器中即可。
在此行
CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds);
做这个改变
CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet);
其中myDataArrayListToDisplayInBottomSheet
是您必须显示在 bottomSheet 中的数据的ArrayList<>
。
在您的CustomAdapter
,使用此数据进行相应的显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.