我到学校制作了一个android应用程序,我想在recyclerview中实现searchview。 我的第一个活动是NavigationDrawer,然后当我使用菜单进行所需的活动时,它打开了很多操作系统的cardview。 主要问题是searchview小部件没有显示,所以我无法使用过滤器
我的布局
主要布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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="@android:color/white"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/visitasrecyclerview" />
</android.support.v4.widget.DrawerLayout>
Recyclerview布局
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:showIn="@layout/activity_visitas">
tools:context=".VisitasActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/visitasRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="745dp"
tools:layout_editor_absoluteY="-51dp" />
<ProgressBar
android:id="@+id/visitas_loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/visitasProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/visitas_loader"
android:text="A carregar"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
列表布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="@color/color_white"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<!--1ª linha-->
<TextView
android:text="Escola:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameEscola"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewEscolaV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nameEscola"
android:layout_marginLeft="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_marginStart="5dp"/>
<!--2ª linha-->
<TextView
android:text="Destino:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewEscolaV"
android:id="@+id/nameDestino"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewDestino"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDestino"
android:layout_marginLeft="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_marginStart="5dp"
android:justificationMode="inter_word"/>
<!--saida-->
<TextView
android:text="Datas da Visita:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewDestino"
android:id="@+id/nameDatasVisitas"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<!--Saída-->
<TextView
android:text="Partida:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDatasVisitas"
android:id="@+id/nameDataSaida"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewDataSaida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDatasVisitas"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_toEndOf="@+id/nameDataSaida"
android:layout_toRightOf="@id/nameDataSaida"
android:layout_marginLeft="5dp"
android:paddingLeft="9dp" android:paddingStart="9dp"/>
<TextView
android:text="Hora:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDatasVisitas"
android:id="@+id/nameHoraSaida"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"
android:layout_toEndOf="@+id/textViewDataSaida"
android:layout_toRightOf="@id/textViewDataSaida"
android:paddingLeft="15dp" android:paddingStart="15dp"/>
<TextView
android:id="@+id/textViewHoraSaida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_below="@+id/nameDatasVisitas"
android:layout_toEndOf="@+id/nameHoraSaida"
android:layout_toRightOf="@id/nameHoraSaida"
android:paddingLeft="5dp"
/>
<!--Chegada-->
<TextView
android:text="Chegada:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDataSaida"
android:id="@+id/nameDataChegada"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewDataChegada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDataSaida"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_toEndOf="@+id/nameDataChegada"
android:layout_toRightOf="@id/nameDataChegada"
android:layout_marginLeft="5dp"
/>
<TextView
android:text="Hora:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDataSaida"
android:id="@+id/nameHoraChegada"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"
android:layout_toEndOf="@+id/textViewDataChegada"
android:layout_toRightOf="@id/textViewDataChegada"
android:paddingLeft="15dp" android:paddingStart="15dp"/>
<TextView
android:id="@+id/textViewHoraChegada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_below="@+id/nameDataSaida"
android:layout_toEndOf="@+id/nameHoraChegada"
android:layout_toRightOf="@id/nameHoraChegada"
android:paddingLeft="5dp"
android:paddingStart="5dp"/>
<!--Turmas-->
<TextView
android:text="@string/turma_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDataChegada"
android:id="@+id/nameTurmas"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewTurmas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDataChegada"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_toEndOf="@+id/nameTurmas"
android:layout_toRightOf="@id/nameTurmas"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:text="@string/tipo_de_turma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameTurmas"
android:id="@+id/nameTipoTurmas"
tools:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold"/>
<TextView
android:id="@+id/textViewTipoTurmas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewTurmas"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_toEndOf="@+id/nameTipoTurmas"
android:layout_toRightOf="@id/nameTipoTurmas"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="@+id/pdfButton"
android:layout_below="@+id/textViewTipoTurmas"
android:background="@drawable/ic_pdf_download32"
android:textColor="@android:color/white"
android:layout_marginLeft="1dp"
android:layout_marginStart="1dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
还有我的主要代码
public class VisitasActivity extends AppCompatActivity implements VisitasAdapter.visitasAdapterListener {
private static final String URL_ATIVIDADES = "http://esmaior.pt/miga/db/getFromDatabase.php?&getAllVisitasMobile=getAllVisitasMobile";
private RecyclerView recyclerView;
private LinearLayoutManager vlinearLayoutManager;
private DividerItemDecoration vdividerItemDecoration;
private List<Visitas> visitasLista;
private VisitasAdapter mAdapter;
static ProgressBar visitas_loader;
static TextView visitasProgress;
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visitasrecyclerview);
recyclerView = findViewById(R.id.visitasRecyclerView);
visitas_loader = findViewById(R.id.visitas_loader);
visitasProgress = findViewById(R.id.visitasProgress);
visitasLista = new ArrayList<>();
mAdapter = new VisitasAdapter(this, visitasLista, this);
whiteNotificationBar(recyclerView);
vlinearLayoutManager = new LinearLayoutManager(this);
vlinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
vdividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), vlinearLayoutManager.getOrientation());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
loadVisitas();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
mAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
mAdapter.getFilter().filter(query);
return false;
}
});
return true;
}
@Override
public void onVisitasSelected(Visitas visitas) {
Toast.makeText(getApplicationContext(), "Selected: " + visitas.getDestino() + ", " + visitas.getTurmas(), Toast.LENGTH_LONG).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.search) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
// close search view on back button pressed
if (!searchView.isIconified()) {
searchView.setIconified(true);
return;
}
super.onBackPressed();
}
}
更新
菜单小部件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".VisitasActivity">
<item
android:id="@+id/search"
android:title="@string/app_name"
android:icon="@drawable/ic_near_me"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
更新日志
06-20 11:54:33.655 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: ViewPostIme pointer 0
06-20 11:54:33.794 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: ViewPostIme pointer 1
06-20 11:54:33.887 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 524114235392} changed=false
06-20 11:54:33.896 30067-30067/gmc.aessm V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@c7c0cc0 nm : gmc.aessm ic=null
06-20 11:54:33.896 30067-30067/gmc.aessm I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
06-20 11:54:36.018 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: ViewPostIme pointer 0
06-20 11:54:36.129 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: ViewPostIme pointer 1
06-20 11:54:36.336 30067-30153/gmc.aessm I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
06-20 11:54:36.351 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: setView = DecorView@c21ec87[VisitasActivity] TM=true MM=false
06-20 11:54:36.354 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
06-20 11:54:36.366 30067-30067/gmc.aessm V/InputMethodManager: Not IME target window, ignoring
06-20 11:54:36.367 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: dispatchAttachedToWindow
06-20 11:54:36.381 30067-30067/gmc.aessm V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
06-20 11:54:36.382 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][1080,1920] result=0x7 surface={valid=true 524047654912} changed=true
06-20 11:54:36.390 30067-30090/gmc.aessm D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000, [1080x1920]-format:1
06-20 11:54:36.390 30067-30090/gmc.aessm D/OpenGLRenderer: eglCreateWindowSurface = 0x7a01598aa0
06-20 11:54:36.413 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
MSG_WINDOW_FOCUS_CHANGED 1
06-20 11:54:36.419 30067-30067/gmc.aessm V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@ec5994c nm : gmc.aessm ic=null
06-20 11:54:36.419 30067-30067/gmc.aessm I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
06-20 11:54:36.452 30067-30090/gmc.aessm D/OpenGLRenderer: eglDestroySurface = 0x7a11b6b850
06-20 11:54:36.462 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
06-20 11:54:36.498 30067-30072/gmc.aessm I/zygote64: Compiler allocated 8MB to compile void android.view.ViewRootImpl.performTraversals()
06-20 11:54:36.747 30067-30072/gmc.aessm I/zygote64: Do full code cache collection, code=252KB, data=194KB
After code cache collection, code=248KB, data=163KB
06-20 11:54:36.804 30067-30072/gmc.aessm I/zygote64: Do partial code cache collection, code=252KB, data=177KB
After code cache collection, code=252KB, data=177KB
Increasing code cache capacity to 1024KB
06-20 11:54:36.902 30067-30067/gmc.aessm D/ViewRootImpl@d37e285[MainActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=false 0} changed=false
06-20 11:55:06.133 30067-30067/gmc.aessm V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@a776755 nm : gmc.aessm ic=null
06-20 11:55:06.133 30067-30067/gmc.aessm I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
06-20 11:55:11.238 30067-30090/gmc.aessm D/OpenGLRenderer: eglDestroySurface = 0x7a01598aa0
06-20 11:55:11.265 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
06-20 11:55:11.266 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: MSG_WINDOW_FOCUS_CHANGED 0
06-20 11:55:11.310 30067-30067/gmc.aessm D/ViewRootImpl@988e4c6[VisitasActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=false 0} changed=false
更新应用图像
MainActivity
[![在此处输入图片描述] [1]] [1]
VisitasActivity(应该在其中显示searchview)
[![在此处输入图片描述] [2]] [2]
使用此代码,我的搜索视图不会显示,并且我无法使用过滤器小部件
有帮助吗?
谢谢
答案 0 :(得分:1)
在您的menu.xml中替换
app:showAsAction="ifRoom|collapseActionView"
使用
app:showAsAction="always"
编辑:-
在您的活动中添加main_layout.xml
。因为工具栏位于main_layout内部,而不在recyclerVie的xml中。
答案 1 :(得分:1)
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Search");
获取工具栏ID并将其传递到setSupportActionBar