我在searchView
中遇到Fragments
的问题。
因此,当我按下search_icon
中的actionbar
时,会执行方法searchClick()
。
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="@string/app_name"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:onClick="searchClick"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
/>
</menu>
MainActivity.Java
package formelhelfer5_10.formelhelfer5_10;
import android.app.SearchManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SearchView.OnCloseListener, SearchView.OnQueryTextListener
{
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent sIntent = getIntent();
if (Intent.ACTION_SEARCH.equals(sIntent.getAction())) {
String query = sIntent.getStringExtra(SearchManager.QUERY);
}
}
public void searchClick(MenuItem item){
Fragment frag = new Results();
Fragment rem = new FaecherFrag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(rem).add(R.id.FragFaecher,frag).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnCloseListener(this);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onClose() {
Fragment frag = new Results();
Fragment rem = new FaecherFrag();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(rem).add(R.id.results,frag).commit();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
TextView t = (TextView) findViewById(R.id.tv);
if (query.isEmpty()){return false;}else
{t.setText(query);}
return false;
}
}
searchClick
然后删除当前的Fragment
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/main_content"
tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="formelhelfer5_10.formelhelfer5_10.MainActivity"
tools:showIn="@layout/activity_main"
android:background="?android:colorBackground">
<fragment
android:id="@+id/FragFaecher"
android:name="formelhelfer5_10.formelhelfer5_10.FaecherFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
FaecherFrag.Java
package formelhelfer5_10.formelhelfer5_10;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
public class FaecherFrag extends ListFragment {
sql sqlo = new sql();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] m_fach = sqlo.getFaecher(getContext());
ListAdapter adptFach =
new ArrayAdapter<>(
getActivity(), android.R.layout.simple_list_item_1, m_fach);
setListAdapter(adptFach);
//Array m_fach wird als Liste angezeigt
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
transFach(position);
//uebergibt die Position des Fachs an die naechste Methode
}
private void transFach(int position){
String[] m_fach = sqlo.getFaecher(getContext());
String sql_fach = m_fach[position]; //String für die SQL-Abfrage. Zeigt welches Fach ausgewaehlt wurde
String[] m_thema = sqlo.getThemen(getContext(), sql_fach);
Intent toThemen = new Intent(getActivity(),Themen.class);
toThemen.putExtra("thema_key",m_thema);
startActivity(toThemen);
}
}
并添加另一个Fragment
,在TextView
fragment_results.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/results"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="?android:colorBackground" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="Text"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"/>
</RelativeLayout>
Results.Java
package formelhelfer5_10.formelhelfer5_10;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Results extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_results, container, false);
return rootView;
}
}
我的第一个问题是,当我提交查询时,第二个Fragment
(fragment_results
)&#34;消失了#34;并显示正常ListFragment
(activity_main
)。我不知道如何解决这个问题。
第二个问题是,当我解除/关闭Search Widget
时,我希望OnCloseListener
删除第二个Fragment
(fragment_results
)并显示ListFragment
(activity_main
)。虽然我在OnCloseListener
中实现了这一点,但没有任何反应。或者我是否将OnCloseListener
与其他东西混淆了?
如果你能帮助我,我会很高兴的。
答案 0 :(得分:0)
对于第一个问题,您在菜单项中设置onClick in xml。这是错误的,不会打电话。请使用https://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu
实现onItemMenuSelected