Android:默认情况下会显示多窗格布局详细信息视图

时间:2013-09-04 03:26:10

标签: android android-fragments

在我的应用程序中,我需要Multi Payne布局。在此布局中,第一个片段是ListView,显示项目列表。单击列表项后,将在列表项的右侧打开详细视图。但是,就我而言,当我在平板电脑上运行我的应用时,默认情况下,详细信息视图会与ListView一起显示。虽然,我想它应该出现在点击列表项目。

以下是我的代码:

活动类:

public class OrderActivity extends FragmentActivity implements
    OnOrderSelectedListener {
private static final String TAG = "OrderActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate called");
    setContentView(R.layout.order_details);

    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }

        OrderListFragment orderListFragment = new OrderListFragment();
        orderListFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, orderListFragment).commit();
    }
}

@Override
public void onOrderSelected(int position) {
    Log.d(TAG, "onOrderSelected called");
    OrderDetailFragment detailsFrag = (OrderDetailFragment) getSupportFragmentManager()
            .findFragmentById(R.id.order_detail_fragment);

    if (detailsFrag != null) {
        if (!detailsFrag.isVisible()) {
            detailsFrag.setUserVisibleHint(true);
            detailsFrag.updateOrderView(position);
        }
    } else {
        OrderDetailFragment newFragment = new OrderDetailFragment();
        Bundle args = new Bundle();
        args.putInt(OrderDetailFragment.ARG_POSITION, position);
        newFragment.setArguments(args);;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
}

LIst Fragment

public class OrderListFragment extends ListFragment {
private static final String TAG = "OrderListFragment";
OnOrderSelectedListener mOnOrderSelectedListener;

public interface OnOrderSelectedListener {
    public void onOrderSelected(int position);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate called");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView called");
    return inflater.inflate(R.layout.order_list, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d(TAG, "onActivityCreated called");
    ArrayList<Data> mDataList = new ArrayList<Data>();
    Data mData1 = new Data("1", "11001", "08/07/2013", "GAUGE_RUN",
            "Dispatched", "Terminal:", "Rail Terminal:", "New York",
            "Washington DC");
    Data mData2 = new Data("1", "11002", "08/07/2013", "GAUGE_RUN",
            "Dispatched", "Terminal:", "Rail Terminal:", "New York",
            "Washington DC");
    mDataList.add(mData1);
    mDataList.add(mData2);
    setListAdapter(new OrderAdapter(getActivity(),
            R.layout.order_list_item, mDataList));
}

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart called");
    if (getFragmentManager().findFragmentById(R.id.order_detail_fragment) != null) {
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d(TAG, "onAttach called");
    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception.
    try { 
        mOnOrderSelectedListener = (OnOrderSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Log.d(TAG, "onListItemClicked");
    mOnOrderSelectedListener.onOrderSelected(position);
    getListView().setItemChecked(position, true);
}
}

详情片段

public class OrderDetailFragment extends Fragment {
public final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static final String TAG = "OrderDetailFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView called");
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    return inflater.inflate(R.layout.acceptance_details, container, false);
}

@Override
public void onStart() {
    Log.d(TAG, "onStart called");
    super.onStart();
    Bundle args = getArguments();
    if (args != null) {
        updateOrderView(args.getInt(ARG_POSITION));
    } else if (mCurrentPosition != -1) {
        updateOrderView(mCurrentPosition);
    }
}

public void updateOrderView(int position) {
    Log.d(TAG, "updateOrderView called");
    /*
     * TextView article = (TextView)
     * getActivity().findViewById(R.id.article);
     * article.setText(Ipsum.Articles[position]); btnNext = (Button)
     * getActivity().findViewById(R.id.btnNext);
     * btnNext.setOnClickListener(this); mCurrentPosition = position;
     */
}

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.d(TAG, "onSaveInstanceState called");
    super.onSaveInstanceState(outState);
    outState.putInt(ARG_POSITION, mCurrentPosition);
}
}

活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal" >

<com.dzo.dispatchcrude.driverapp.ui.HeaderBar
    android:id="@+id/headerBar"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true" >
</com.dzo.dispatchcrude.driverapp.ui.HeaderBar>

<LinearLayout
    android:id="@+id/linOrderView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/headerBar"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/order_list_fragment"
        android:name="com.dzo.dispatchcrude.driverapp.ui.OrderListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/order_detail_fragment"
        android:name="com.dzo.dispatchcrude.driverapp.ui.OrderDetailFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="30dp"
        android:layout_weight="2" />
</LinearLayout>

</RelativeLayout>

列出片段布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:dividerHeight="1dp" />

</LinearLayout>

订单明细碎片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/corner_shape"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtHeaderType"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginBottom="-5dp"
    android:background="@drawable/upper_corner"
    android:gravity="center"
    android:text="@string/acceptance_details"
    android:textColor="@android:color/white"
    android:textSize="30sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/txtHeaderSource"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@drawable/rectangle"
    android:gravity="center"
    android:textSize="25sp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtTruck"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:paddingLeft="15dp"
        android:text="@string/truck"
        android:textColor="@color/text_color"
        android:textSize="20sp"
        android:typeface="sans" />

    <Spinner
        android:id="@+id/truckSpinner"
        style="@android:style/Widget.Spinner"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_weight="3"
        android:background="@drawable/bg_edit_text"
        android:gravity="center"
        android:spinnerMode="dropdown" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/divider_color" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtTrailor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:paddingLeft="15dp"
        android:text="@string/trailor"
        android:textColor="@color/text_color"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/trailorSpinner"
        style="@android:style/Widget.Spinner"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_weight="3"
        android:background="@drawable/bg_edit_text"
        android:gravity="center"
        android:spinnerMode="dropdown" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/divider_color" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtTrailor2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:paddingLeft="15dp"
        android:text="@string/trailor2"
        android:textColor="@color/text_color"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/trailor2Spinner"
        style="@android:style/Widget.Spinner"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_weight="3"
        android:background="@drawable/bg_edit_text"
        android:gravity="center"
        android:spinnerMode="dropdown" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/divider_color" />

<Button
    android:id="@+id/btnAccept"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginBottom="25dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="25dp"
    android:background="@drawable/input_button"
    android:gravity="center"
    android:text="@string/accept"
    android:textColor="@android:color/white"
    android:textSize="30sp"
    android:textStyle="bold" />

</LinearLayout>

我确实经历了Android开发者网站上提供的Fragments Basic Demo,但无法弄清楚我的错误。

1 个答案:

答案 0 :(得分:0)

试试这个

listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setItemChecked(0,false);