Kil*_*ler 7 android viewgroup android-fragments
基本上这是我的应用程序(想法)的平板电脑横向:两个片段,左片段是一个由resource.xml文件填充的listfragment(得到这个工作).
正确的片段应该根据用户点击的列表项动态更改片段和布局.到目前为止谷歌搜索告诉我,我需要以编程方式添加和删除视图组中的片段来做到这一点.是对的吗?
基本上问题是:
编辑:
所以这就是我的活动: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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后是我的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>
Run Code Online (Sandbox Code Playgroud)
我填充列表的xml文件中的字符串数组称为"listmenu"
请告诉我在哪里需要输入您写下的代码?
看看这些片段主题:
基本上你会想让左边的片段告诉父Activity选择哪个项目.然后,Activity可以在右窗格中添加/删除正确的片段.
请记住,创建/销毁片段对系统来说是很多工作.如果您可以在右侧窗格中使用单个片段,则效率会更高.然后,您可以在该一个Fragment实例上调用方法,而无需构建新的Fragments.
编辑(示例):
使用自定义方法的主要活动实现:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
该onListItemClick实施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);
}
Run Code Online (Sandbox Code Playgroud)
最后,你的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_group_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- More views here... -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24352 次 |
| 最近记录: |