sit*_*awl 15 android android-fragments
我有一个标签,其中包含带有searchview和listview的线性布局.当用户单击其中一个搜索结果时,我想将该布局替换为包含所选内容详细信息的另一个布局.
当我替换搜索片段时,只会替换布局的listview部分会发生什么; 搜索视图在屏幕上仍然可见并处于活动状态.
这是我的搜索布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SearchView
android:id="@+id/public_calendar_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
</SearchView>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:choiceMode="singleChoice" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是详细布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detail_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<Space
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:text="@string/label_Name" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".1" />
</LinearLayout>
.........
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的代码替换搜索片段:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack.
DetailFragment fragment = DetailFragment.newInstance(cal);
transaction.replace(R.id.search_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
Run Code Online (Sandbox Code Playgroud)
细节片段创建:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.detail_layout, container, false);
return v;
}
static DetailFragment newInstance(String cal) {
DetailFragment d = new DetailFragment();
return d;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何才能更换整个搜索布局.
谢谢!
sit*_*awl 16
我想出了我的问题.马可不完全正确,但他指出了我正确的方向.
问题是我传递给replace方法的容器ID是我要替换的片段的ID,而不是片段容器的ID.这似乎解释了为什么一些原始的片段控件在替换后仍然存在 - 整个片段没有被替换.
我改变它以获取片段容器视图ID,这是有效的!这是代码:
transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment);
Run Code Online (Sandbox Code Playgroud)
我找到了获取片段的容器视图ID的答案,获取片段的容器视图ID.
lio*_*ssi 11
我使用的是正确的ID,然后它也没有消失.原来你需要Background color在新的Fragment布局中设置所需的颜色,就像这样.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white_color">
Run Code Online (Sandbox Code Playgroud)
小智 5
要解决此问题,请在片段布局文件中将android:background属性设置为"?android:windowBackground".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
tools:context=".MainFragment">
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在Fragment_2中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
tools:context=".SomeFragment">
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15673 次 |
| 最近记录: |