以编程方式将片段添加到viewgroup

时间:2012-03-01 04:10:05

标签: android android-fragments viewgroup

基本上这是我的应用程序(想法)的平板电脑横向: 两个片段,左片段是一个由resource.xml文件填充的listfragment(得到了它)。

正确的片段应该根据用户点击的列表项动态更改片段和布局。到目前为止谷歌搜索告诉我,我需要以编程方式添加和删除视图组中的片段来做到这一点。是吗?

基本上问题是:

  1. 如何创建viewgroup和where(Main.java或menufragment.java)?
  2. 如何将动态“用户点击ID 3放在列表中,以便将片段3添加到视图组”
  3. 我要将哪些内容添加到main.xml文件中?得到了listfragment的片段,为动态视图组添加了什么?
  4. 修改

    所以这就是我的活动: Main.java

    public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        }
    }
    

    这是我的listfragment MenuFragment.java

    public class MenuFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.listfragment, container, false);
    
    }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.listmenu)));
        }    
    
    
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
            Main activity = (Main) getActivity();
        }
    
    
    }
    

    最后是我的main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <fragment
        android:id="@+id/list"
        android:layout_width="200dp"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        class="com.mwerner.fragmentstest.MainMenu" />
    
    <View
        android:id="@+id/contentview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/list" />
    
    </RelativeLayout>
    

    我填充列表的xml文件中的字符串数组称为“listmenu”

    请告诉我在哪里需要输入您记下的代码?

1 个答案:

答案 0 :(得分:7)

看看这些片段主题:

基本上,您希望让 left 片段告诉父Activity选择哪个项目。然后,活动可以在右侧窗格中添加/删除正确的片段。

请记住,创建/销毁片段对系统来说是很多工作。如果您可以在右侧窗格中使用单个片段,则效率会更高。然后,您可以在一个Fragment实例上调用方法,而无需构建新的碎片。

编辑(示例):

使用自定义方法实现主要活动:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** Called by the left fragment */
    public void updateRightPane(long id) {
        // Get the data with the selected item id
        // ...

        // Create a new fragment
        MyFragment fragment = new MyFragment(data);

        // Update the layout
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // The id specified here identifies which ViewGroup to
        // append the Fragment to.
        ft.add(R.id.view_group_id, fragment);
        ft.commit();
    }
}

onListItemClick的{​​{1}}实施:

MenuFragment.java

最后,您的布局文件:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // Get the parent Activity
    Main activity = (Main) getActivity();

    // Pass the selected item id to the Activity method
    // Note: Feel free to update this method to accept additional
    //       arguments (e.g. position).
    activity.updateRightPane(id);
}
相关问题