如何从listfragment中选择项目并在下一个片段中显示

时间:2013-11-15 08:51:52

标签: android fragment

大家好我有一个我要在片段中显示的项目列表。从我想要显示下一个片段中选择一个项目后。我有代码,这工作正常。但是我想在用户点击设备的后退按钮后返回相同的片段列表。

private AttendeeListAdapter attendeeListAdapter = null;
private List<DTO> attendeeDTOList;
private boolean showBookmarkedOnly=false;
private Fragment details;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyListFragment list = new MyListFragment();
    attendeeDTOList=ProfileDAO.getInstance().getRecords(DBHandler.getInstance(getActivity()).getDBObject(0),showBookmarkedOnly);
    attendeeListAdapter = new AttendeeListAdapter(getActivity(),attendeeDTOList);
    list.setListAdapter(attendeeListAdapter);
     getChildFragmentManager()
     .beginTransaction()
     .add(R.id.content_frame, list)
     .commit();
    list.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override public void onItemClick(AdapterView<?> l, View v, int position, long id) {
          // Create details fragment based on clicked item's position
            details = new AttendeeFragmentDetails();
               getChildFragmentManager()
              .beginTransaction()
              .replace(R.id.content_frame, details)
              .addToBackStack(null)
              .commit();
            final Bundle arguments = new Bundle();
            arguments.putSerializable("ProfileDTO", (ProfileDTO)attendeeDTOList.get(position));
            details.setArguments(arguments);
        }

    });

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // There has to be a view with id `container` inside `wrapper.xml`
    return inflater.inflate(R.layout.fragment_attendee_framelayout_list, container, false);
}

public static class MyListFragment extends ListFragment
{

    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener l)
    {
        this.listener = l;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
        if(listener != null)
        {
          listener.onItemClick(l, v, position, id);
        }
    }
}

0 个答案:

没有答案