片段无法在双窗格平板电脑模式下正常显示

时间:2015-06-09 20:21:55

标签: java android xml android-layout android-fragments

我试图在细节片段中发布我的片段&在两个窗格模式中突出显示所选列表项(代码在 FragmentWCLine.java 中)但由于某种原因它不会这样做。似乎在单窗格模式下工作正常,但是当涉及到两个窗格模式时,它似乎忽略了我在 FragmentWCLine.java 中的if(mTwoPane)部分中的代码,然后决定启动一个活动而不是显示细节片段中的片段&突出显示所选列表项。在 FragmentWCLine.java 中,我不确定如何处理startActivity(new Intent(getActivity(), mWC[position].fragmentClass));。另外FragmentLineChooserList newFragment = new FragmentLineChooserList();需要更改为其他内容,但我不知道它会是什么。如何防止mTwoPane代码被忽略,以便它能够实现我想要实现的上述目标?

WCLineActivity.java

    public class WCLineActivity extends ActionBarActivity {

        /**
         * Whether or not the activity is in two-pane mode, i.e. running on a tablet
         * device.
         */
        private boolean mTwoPane;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ActionBar actionBar = getSupportActionBar();
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#66CCCC")));
            actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));

            if (findViewById(R.id.detail_container) != null) {
                mTwoPane = true;
            }

            FragmentWCLine newFragment = new FragmentWCLine();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.master_container, newFragment);
            transaction.commit();
        }   
    }

FragmentWCLine.java

public class FragmentWCLine extends android.support.v4.app.Fragment {

    private class WC {
        private CharSequence station;
        private CharSequence zone;
        private Class<? extends Activity> activityClass;
        private Class<? extends android.support.v4.app.Fragment> fragmentClass;

        public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
            this.fragmentClass = fragmentClass;
            this.activityClass = activityClass;
            this.station = getResources().getString(stationResId);
            this.zone = getResources().getString(zoneResId);
        }

        @Override
        public String toString() { return station.toString(); }
        public String getzone(){ return zone.toString(); }
    }

    private static WC[] mWC;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    public boolean mTwoPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_wc_line, container, false);


        if (getActivity().findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        }else{
            mTwoPane = false;
        }

        // Instantiate the list of stations.
        mWC = new WC[]{
                new WC(R.string.bank, R.string.zone_1, WCAActivity.class, FragmentWCA.class),
                new WC(R.string.wat, R.string.zone_1, WCBActivity.class, FragmentWCB.class)
        };

        final ListView listView = (ListView)v.findViewById(R.id.list_wc);
        listView.setAdapter(new MyAdapter(getActivity(), mWC));
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mTwoPane) {
                    setItemNormal();
                    View rowView = view;
                    setItemSelected(rowView);
                    Fragment newFragment;
                    switch (position) {
                        case 0:
                            newFragment = new FragmentWCA();
                            break;
                        case 1:
                            newFragment = new FragmentWCB();
                            break;
                        default:
                            newFragment = new FragmentWCA();
                            break;
                    }
                    WCLineActivity activity = (WCLineActivity) view.getContext();
                    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.detail_container, newFragment);
                    transaction.commit();
                } else {
                    Intent intent;
                    switch (position) {
                        case 0:
                            intent = new Intent(getActivity(), WCAActivity.class);
                            break;
                        case 1:
                            intent = new Intent(getActivity(), WCBActivity.class);
                            break;
                        default:
                            intent = new Intent(getActivity(), WCAActivity.class);
                            break;
                    }
                    startActivity(intent);
                }
            }

            public void setItemSelected(View view) {
                View rowView = view;
                view.setBackgroundColor(Color.parseColor("#66CCCC"));

                TextView tv0 = (TextView) rowView.findViewById(R.id.list_item_station);
                tv0.setTextColor(Color.parseColor("#000099"));

                TextView tv1 = (TextView) rowView.findViewById(R.id.list_item_zone);
                tv1.setTextColor(Color.parseColor("#000099"));
            }

            public void setItemNormal() {
                for (int i = 0; i < listView.getChildCount(); i++) {
                    View v = listView.getChildAt(i);
                    v.setBackgroundColor(Color.TRANSPARENT);

                    TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
                    tv0.setTextColor(Color.WHITE);

                    TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
                    tv1.setTextColor(Color.parseColor("#B5B5B5"));
                }
            }
        });

        return v;
    }

    static class MyAdapter extends BaseAdapter {

        static class ViewHolder {
            TextView station;
            TextView zone;
        }

        LayoutInflater inflater;
        WC[] mWC;

        public MyAdapter(Context contexts, WC[] samples) {
            this.mWC = samples;
            inflater = LayoutInflater.from(contexts);
        }

        @Override
        public int getCount() {
            return mWC.length;
        }

        @Override
        public Object getItem(int position) {
            return mWC[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item_dualline, null);
                viewHolder = new ViewHolder();

                viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
                viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.station.setText(mWC[position].station);
            viewHolder.zone.setText(mWC[position].getzone());


            return convertView;
        }
    }
}

FragmentWC.java

public class FragmentWC extends android.support.v4.app.Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_wc, container, false);

        return v;
    }
}

fragment_wc.xml布局

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Bank"
        android:id="@+id/textView0"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

fragment_wc_line.xml布局

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

    <ListView
        android:id="@+id/list_wc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="false"
        android:layout_centerHorizontal="true"/>

</LinearLayout>

activity_main.xml布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/master_container"
    android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

activity_main.xml布局(sw600dp)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/master_container"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/detail_container"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

我的猜测是你需要在片段事务之前初始化boolean:

if (findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        }
FragmentWCLine newFragment = new FragmentWCLine();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();

<强>更新

同时更新点击监听器:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mTwoPane){
                    setItemNormal();
                    View rowView = view;
                    setItemSelected(rowView);
                    Fragment newFragment;
                    switch (position){
                         case 0:
                            newFragment = new FragmentWCBank ();
                            break;
                         case 1:
                            newFragment = new FragmentWCWAT();
                            break;
                         default:
                            newFragment = new FragmentWCBank ();
                            break;
                    }
                    WCLineActivity activity = (WCLineActivity) view.getContext();
                    FragmentTransaction transaction = activity .getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.detail_container, newFragment);
                    transaction.commit();
                }
                else{
                    Intent intent; 
                    switch (position){
                         case 0:
                            intent = new Intent(getActivity(), WCBankActivity.class);
                            break;
                         case 1:
                            intent = new Intent(getActivity(), WCWATActivity.class);
                            break;
                         default:
                            intent = new Intent(getActivity(), WCBankActivity.class);
                            break;
                    }
                    startActivity(intent);
                }
            }