2017-02-17 56 views
2

我想从片段列表视图中获取数据并在底部图纸布局中查看它。我想查看上面的附加图像。请帮帮我。 enter image description here如何将数据从片段传递给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; 
} 

回答

1

不管你有,你想从你的列表视图主要以bottomsheet列表传递数据,只是通过在bottomsheet适配器的数据。
在这里行
CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds);
进行此更改
CustomAdapter adapter = new CustomAdapter(this, textString, drawableIds, myDataArrayListToDisplayInBottomSheet);


其中myDataArrayListToDisplayInBottomSheet
ArrayList中的数据的<>,你必须在显示bottomSheet。

而在你CustomAdapter,使用此数据来相应地显示。

+0

“myDataArrayListToDisplayInBottomSheet”这意味着放置在片段中的列表视图的数组列表数据 –

+0

事实上,如果这是您想要在bottomSheet中显示的数据。 –

0

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

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

另一个肮脏的解决办法是保持接触对象在主机活动类并使用((HostActivity) getActivity).getContact()((HostActivity) getActivity).setContact(billGates)方法设置和获取联系对象。

相关问题