在TabLayout

时间:2016-05-30 11:50:30

标签: android android-tablayout android-keypad

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

Android TabLayout

问题在于,每次加载此活动或更改选项卡时,键盘都会自动显示。

以下是我的不同文件:

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 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>

5 个答案:

答案 0 :(得分:1)

好的,我已经找到了导致键盘显示的内容,我有一个带有搜索按钮的菜单,但是工作得很好,原来是我的菜单造成了所有麻烦,无论如何,谢谢所有的帮助!

答案 1 :(得分:0)

只需在清单中为发生问题的特定活动添加android:windowSoftInputMode =“stateAlwaysHidden”

答案 2 :(得分:0)

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

var initialPlay = false;

$("#vid-pages").find("video").on('canplay', function() {
  if (initialPlay) return;
  initialPlay = true;

  $(this)[0].currentTime = $(this)[0].duration / 2;
  console.log($(this)[0].currentTime);
});

请同时查看this

答案 3 :(得分:0)

将这些行放入.Xml文件

机器人:descendantFocusability =&#34; beforeDescendants&#34; 机器人:focusableInTouchMode =&#34;真&#34;

答案 4 :(得分:0)

用这个检查

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