代码之家  ›  专栏  ›  技术社区  ›  Boardy

更新片段中的内容

  •  0
  • Boardy  · 技术社区  · 13 年前

    我正在做一个android项目,我正试图找出如何使用碎片为我的应用程序制作一个平板电脑友好的UI。但我不确定如何根据片段A中发生的事情来更新片段B。我知道我需要某种接口,但我不知道如何实现它。

    基本上,我有一个名为MainActivity的活动,它为片段设置布局。

    在横向模式下,XML文件为。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment android:name="com.BoardiesITSolutions.FragmentTest.FragmentA"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent">
        </fragment>
        <FrameLayout android:id="@+id/viewer"
            android:layout_weight="1"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:background="?android:attr/detailsElementBackground">
        </FrameLayout>
    </LinearLayout>
    

    在纵向模式下

    目前,MainActivity只是使用将内容视图设置为上面的XML文件 SetContentView onCreate 方法下面是它的样子。

    Fragment layout

    在FragmentA类文件中,它扩展了ListFragment并包含项目的ListView,我希望能够做的是根据在片段a中选择的内容更新片段B中的文本视图。

    下面是片段A的代码。

    @Override
        public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
        {
            return inflator.inflate(R.layout.fragment_a, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
    
            myListView = getListView();
    
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add("Item1");
            arrayList.add("Item2");
            arrayList.add("Item3");
    
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
                    android.R.layout.simple_list_item_activated_1, arrayList);
            setListAdapter(arrayAdapter);
    
            View fragmentB = getActivity().findViewById(R.id.viewer);
            mDualPane = fragmentB != null && fragmentB.getVisibility() == View.VISIBLE;
    
            if (savedInstanceState != null)
            {
                mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
            }
    
            if (mDualPane)
            {
                myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                showDetails(mCurCheckPosition);
            }
        }
    
        @Override
        public void onListItemClick(ListView l, View view, int position, long id)
        {
            showDetails(position);
        }
    
        private void showDetails(int index)
        {
            mCurCheckPosition = index;
            if (mDualPane)
            {
                myListView.setItemChecked(index, true);
                FragmentB details = (FragmentB)getFragmentManager().findFragmentById(R.id.viewer);
                if (details == null || details.getShownIndex() != index)
                {
                    details = FragmentB.newInstance(index);
    
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.viewer, details);
                    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    fragmentTransaction.commit();
                }
            }
            else
            {
                Intent intent = new Intent(getActivity(), FragmentBActivity.class);
                intent.putExtra("index", index);
                startActivity(intent);
            }
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState)
        {
            super.onSaveInstanceState(outState);
            outState.putInt("curChoice", mCurCheckPosition);
        }
    

    FragmentB包含以下代码,此类扩展Fragment

    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
        {
            if (container == null)
            {
                return null;
            }
            View view = inflator.inflate(R.layout.fragment_b, container, false);
            return view;
        }
    
        public static FragmentB newInstance(int index)
        {
            FragmentB fragmentB = new FragmentB();
            Bundle args = new Bundle();
            args.putInt("index", index);
            //rgs.putString("content", content);
            fragmentB.setArguments(args);
    
            return fragmentB;
        }
    
        public int getShownIndex()
        {
            return getArguments().getInt("index", 0);
        }
    

    在FragmentB的活动文件中,它包含以下内容

    @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
                finish();
                return;
            }
    
            if (savedInstanceState == null)
            {
                FragmentB fragmentB = new FragmentB();
                fragmentB.setArguments(getIntent().getExtras());
                getFragmentManager().beginTransaction().add(android.R.id.content, fragmentB).commit();
            }
        }
    

    正如你从上面的屏幕截图中看到的,我有片段工作的基础,当我点击每个项目时,它会显示当前选择的项目是什么,但我不知道如何告诉它根据用户从片段a点击的内容更新片段b中的文本视图,以及在纵向和横向模式下如何处理。

    谢谢你能提供的任何帮助。

    2 回复  |  直到 13 年前
        1
  •  2
  •   Andy Res    13 年前

    您可以覆盖 onActivityCreated() 方法 FragmentB ,按该TextView的id查找视图,然后更新它。

    下面是一个模拟:

    public class FragmentB extends Fragment{
    
        //......
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            TextView textView = getActivity().findViewById(R.id.my_textview);
            textView.setText("Hello World!");
        }
    }
    
        2
  •  0
  •   CommonsWare    13 年前

    但我不知道如何告诉它根据用户从片段a点击的内容更新片段b中的文本视图,以及在纵向和横向模式下如何处理。

    让Fragment在托管活动上调用一个方法,让它知道用户点击了什么。然后,托管活动可以调用片段B上的方法(如果该片段存在),或者启动片段B(如果片段不存在但有空间容纳它),或者开始将负责显示片段B的活动(例如,在电话上)。

    我不会做的是你正在做的事情:让片段A创建片段B。片段A不应该在乎片段B是否存在;这就是活动的工作。片段A应该只关心片段A,以及将必要的事件传递给活动。