2017-02-17 51 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)方法設置和獲取聯繫對象。

相關問題