在 TabLayout 中导航时打开键盘

Keyboard opens when navigating in TabLayout

我正在使用一个简单的 TabLayout 在我的应用程序中显示三个由 RecyclerViewAdapter 组成的不同列表,如下所示:

问题是每次加载此 activity 或更改选项卡时,键盘都会自行显示。

这是我的不同文件:

MainActivity.java

private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    setContentView(R.layout.activity_main);

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

    final ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView != null) {
        setupDrawerContent(navigationView);
    }

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    if (viewPager != null) {
        setupViewPager(viewPager);
    }

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    if (tabLayout != null){
        tabLayout.setupWithViewPager(viewPager);
    }

    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.nav_view);

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    if (navigationView != null) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            mDrawerLayout.closeDrawers();

            switch (menuItem.getItemId()) {
                // Drawer options
            }
        });
    }
}


/*
@Override
public boolean onCreateOptionsMenu(Menu item) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, item);

    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) item.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    return true;
}*/


@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.options_menu, menu);

    final MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (android.widget.SearchView) searchItem.getActionView();

    final MenuItem filterItem = menu.findItem(R.id.action_filter);

    searchView.setQueryHint(getString(R.string.search_hint));
    searchView.setIconified(false);
    searchView.onActionViewExpanded();

    return super.onCreateOptionsMenu(menu);
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}


private void setupViewPager(ViewPager viewPager) {
    Adapter adapter = new Adapter(getSupportFragmentManager());
    adapter.addFragment(new ListFragment(), "1");
    adapter.addFragment(new ListFragment(), "2");
    adapter.addFragment(new ListFragment(), "3");
    viewPager.setAdapter(adapter);
}

private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    mDrawerLayout.closeDrawers();
                    return true;
                }
            });
}

static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public Adapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

ListFragment.java

private RecyclerView mRecyclerView;

private PostAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();

}

public int getViewID() {
    return R.layout.fragment_post_list;
}

public void bindViews(View v) {
    mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
    loadUpdates();
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    if (mRecyclerView == null){
        return;
    }

}


private void loadUpdates() {
    showListView(false);

    //Gets information from the server and call setAdapter()

}

private void setAdapter() {

    if ( ((MainActivity)getActivity()).getPostList() == null) {
        Log.i(MainActivity.TAG, "not ready to set Adapter");
        return;
    }

    showListView(true);

    mAdapter = new PostAdapter(((MainActivity)getActivity()).getPostList(),this.getContext());
    mRecyclerView.setAdapter(mAdapter);

}


private void showListView(boolean show) {
    mRecyclerView.setVisibility(show ? View.VISIBLE : View.GONE);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView rv = (RecyclerView) inflater.inflate(
            R.layout.fragment_post_list, container, false);

    setupRecyclerView(rv);
    mRecyclerView = rv;
    loadUpdates();
    return rv;
}

private void setupRecyclerView(RecyclerView recyclerView) {
    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));

}


public static class SimpleStringRecyclerViewAdapter
        extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

    private final TypedValue mTypedValue = new TypedValue();
    private int mBackground;
    private List<String> mValues;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public String mBoundString;

        public final View mView;
        public final TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mTextView = (TextView) view.findViewById(R.id.post_name);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mTextView.getText();
        }
    }

    public String getValueAt(int position) {
        return mValues.get(position);
    }

    public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
        context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
        mBackground = mTypedValue.resourceId;
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        view.setBackgroundResource(mBackground);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mBoundString = mValues.get(position);
        holder.mTextView.setText(mValues.get(position));
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }
}

希望我没有遗漏任何内容,在此先感谢您的帮助。

编辑:

这里是我的 activity 清单,因为建议在这里更改一些东西

    <activity android:name=".MainActivity"
        android:theme="@style/Base.Theme.NoTitleBar"
        android:windowSoftInputMode="stateAlwaysHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

只需在您的清单中添加 android:windowSoftInputMode="stateAlwaysHidden" 出现问题的特定 activity

我认为 OnTabChangeListener是执行此类操作的最佳位置。

tabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId){
    hideKeyboard()
}
});

public void hideKeyboard()
{
   InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
   imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

请同时检查this

将此行放入您的 .Xml 文件

android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true"

用这个检查

            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethodManager != null)
                inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);

好的,我找到了导致键盘显示的原因,我有一个带有搜索按钮的菜单,但效果不佳,结果是我的菜单造成了所有这些麻烦,无论如何,谢谢求助!