我正在关注片段示例https://gist.github.com/daichan4649/2947119。
我在按钮点击事件后动态添加片段。但是片段与按钮重叠。当我在此屏幕上按回键时,活动将关闭。
需要帮助才能在单击按钮后仅显示片段,如果在添加片段后按下背面,则只应关闭顶部片段。
这是我的测试代码。 MainActivity.java
public class MainActivity extends FragmentActivity {
private static Button mBtn;
private static final String TAG_LIST = "list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
return;
}
mBtn = (Button) findViewById(R.id.clickme);
mBtn.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = getFragmentManager().findFragmentByTag(TAG_LIST);
if (fragment == null) {
fragment = TestListFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, fragment, TAG_LIST);
ft.commit();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TestListFragment.java
public class TestListFragment extends ListFragment {
public static TestListFragment newInstance() {
return new TestListFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(android.R.id.content, container, false);
return rootView;
}
@SuppressLint("NewApi")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text);
setListAdapter(adapter);
adapter.addAll(createDataList(10));
}
private static List<String> createDataList(int counts) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < counts; i++) {
list.add("i=" + i);
}
return list;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Toast.makeText(getActivity(), "List Item Clicked", Toast.LENGTH_SHORT).show();
//On item click, launch the DialogFragment
TestDialogFragment moveFragment = new TestDialogFragment( );
moveFragment.show(getFragmentManager(), "Test List Fragment");
}
TestDilogFragment.java
public class TestDialogFragment extends DialogFragment {
private View testDialogFragment = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Fill the layout as per Your requirement
testDialogFragment = inflater.inflate(R.layout.activity_list_fragment, container);
return testDialogFragment;
}
}
活性-main.xml中
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clickme"
android:text="ClickMe" />
</RelativeLayout>
activity_list_fragment.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
答案 0 :(得分:0)
更改您的activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parentt" >
<Button
android:id="@+id/clickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<FrameLayout
android:id="@+id/container"
android:layout_above="@id/clickme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</FrameLayout>
</RelativeLayout>
底部有Button
,顶部有ViewGroup
FrameLayou
t。您将Fragment
添加到容器中。
在ManiActivtiy改为
ft.add(R.id.container, fragment, TAG_LIST);