【问题标题】:Fragment not appearing properly in two pane tablet mode片段在两窗格平板电脑模式下未正确显示
【发布时间】:2015-08-24 20:44:37
【问题描述】:

我正在尝试在细节片段中启动我的片段并在两个窗格模式下突出显示选定的列表项(代码在 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>

【问题讨论】:

  • 变量 mTwoPane 在 FragmentWCLine.java 中被声明为私有。而那个变量永远不会改变。只需将其设置为 true 作为 hack 修复,它会更好。我相信我在以前的帖子中说过这一点,但从未得到足够的关注。是时候做点什么了,就同一问题发表新的帖子也无济于事。

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


【解决方案1】:

我猜你需要在片段交易之前初始化布尔值:

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);
                }
            }

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 不是完整的解决方案,但您的回答部分有帮助,因此无论如何我都会给您积分。
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2014-07-04
  • 2018-10-18
  • 2014-09-10
相关资源
最近更新 更多