我遇到了一些碎片问题。我的主活动包含一个抽屉布局和一个框架布局,如下所示:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!—- Main view (fragments) -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cor_view"/>
<!-- Navigation drawer -->
<ListView android:id="@+id/lista"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/cor_divider_drawer"
android:dividerHeight="1dp"
android:background="@color/background_drawer"
android:textColor="@color/cor_texto_drawer"/>
</android.support.v4.widget.DrawerLayout>
根据我的导航抽屉中的每个项目,一个特定的片段将显示在id/content_frame中。这很好,但问题是,一个特定的片段在操作栏上有一个搜索选项,当我提交查询时,结果不会显示在该片段的同一"位置"。我的意思是,搜索结果没有填充framelayout(id/content_frame)。我怎么能那样做?碎片置换?这只适用于使用活动吗?
这是我的主要活动:
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navigation.NavList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
{
// According to each item from menu, I’ll choose which fragment will be shown.
navigation.showFragment(position, MainActivity.this);
}
});
// When the app starts, by default, the home will be shown.
navigation.showFragment(0, MainActivity.this);
}
}
在showFragment方法中,我创建了这样的fragment对象(在本例中,位置将设置为指向ABCFragment):
Fragment fragment = new ABCFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
这是我的片段,有搜索视图:
public class ABCFragment extends Fragment implements OnQueryTextListener
{
/*
onCreateView …
*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
// Action bar.
inflater.inflate(R.menu.activity_main_actions, menu);
searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.
setSearchableInfo(searchManager.getSearchableInfo(getActivity()
.getComponentName()));
}
@Override
public boolean onQueryTextSubmit(String query)
{
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
return false;
}
}
菜单文件夹中的我的操作栏:
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<item android:id="@+id/action_new"
android:icon="@drawable/ic_action_new_event"
android:title="@string/action_new_event"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_refresh"
android:icon="@drawable/ic_action_refresh"
android:title="@string/action_refresh"
android:showAsAction="ifRoom" />
我在XML文件夹中的可搜索文件:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:inputType="number"
android:label="@string/app_name" />
我的Android清单:
<!-- Main Activity -->
<activity
android:name="com.test.MainActivity"
android:label="@string/app_name">
</activity>
<!-- ABC Fragment -->
<activity
android:name="com.test.ABCFragment"
android:label="@string/app_name">
<meta-data
android:name="android.app.default_searchable"
android:value="com.test.SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name="com.test.SearchResultsActivity"
android:parentActivityName="com.test.ABCFragment" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
我通过片段中的custum操作栏直接使用searchview在这里我们找不到活动,但使用getActivity可以获得所有窗口小部件
searchView=(searchView)getActivity().findViewById(R.id.searchView1);