虽然我实现了方法,但我的搜索栏无法正常工作

时间:2019-08-13 16:58:15

标签: android

我有搜索栏,它将搜索片段中的列表项,而该片段位于MainActivity.class中,但不知道它不起作用

注意:我在每个listViews中都有三个Listfragment类,我试图从MainActivity.class中搜索这些listViews中的项目

package com.example.mymehnat;


import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.support.v7.widget.SearchView;



 public class MainActivity extends AppCompatActivity /*implements NavigationView.OnNavigationItemSelectedListener*/ {

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private NavigationView navigation;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    tabLayout = findViewById(R.id.tablayout);
    viewPager = findViewById(R.id.view_pager);
    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.AddFragment(new home_fragment(), "English");
    adapter.AddFragment(new hindi_fragment(), "Hindi");
    adapter.AddFragment(new genre_fragment(), "Tags");
    adapter.AddFragment(new fav_fragment(), "Favourites");

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(0).setText("English");
    tabLayout.getTabAt(1).setText("Hindi");
    tabLayout.getTabAt(2).setText("Category");
    tabLayout.getTabAt(3).setText("Favourites");


    Toolbar toolbar1 = findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar1);



    drawerLayout = findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, toolbar1, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();

    navigation = findViewById(R.id.nav_view);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            switch (id) {
                case R.id.nav_cd:
                    Intent intent = new Intent(MainActivity.this, contact_developer1.class);
                    startActivity(intent);
                    break;
                case R.id.nav_atapp:
                    Intent intent1 = new Intent(MainActivity.this, about_the_app.class);
                    startActivity(intent1);
                    break;
                case R.id.nav_atchurch:
                    Intent intent2 = new Intent(MainActivity.this, about_the_church.class);
                    startActivity(intent2);
                    break;
                case R.id.nav_ods:
                    Intent intent3 = new Intent(MainActivity.this, our_doctrinal_statement.class);
                    startActivity(intent3);
                    break;
                case R.id.nav_omp:
                    Intent intent4 = new Intent(MainActivity.this, our_musical_philosophy.class);
                    startActivity(intent4);
                    break;
                case R.id.nav_share:
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    sendIntent.setType("text/plain");
                    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hey, download this app!");
                    startActivity(sendIntent);
                    break;
            }
            return false;
        }
    });

}
@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item))
        return true;

    int id = item.getItemId();
    if (id == R.string.app_name) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else {
        super.onBackPressed();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.item_search);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView)  menu.findItem(R.id.item_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    return true;
}
}

0 个答案:

没有答案