我知道这个问题可能会很奇怪,但在按下拇指向下的按钮之前先听我说完。
TabLayout
两个选项卡的一个自定义视图,我做了所有正确的事情,直到我想改变自定义选项卡视图时,它变得选中或不,基本上,我想它使选中的视图的alpha=1和未选中=0.3f。
我找到了一个方法,效果很好
但是
仅当应用程序语言为英语时。。当我把它改成RTL语言,比如阿拉伯语,它会给我一个
NullPointerException
获取自定义选项卡视图时。!
(检查注释)
private void initTabs() {
adapter = new TabsPagerAdapter(getFragmentManager());
viewpager.setAdapter(adapter);
tabs.setupWithViewPager(viewpager);
for (int i = 0; i < tabs.getTabCount(); i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
tab.setCustomView(adapter.getTabView(i));
}
adapter.SetOnSelectView(tabs, 0); // this makes the first tab as selected
tabs.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int c = tab.getPosition();
adapter.SetOnSelectView(tabs, c);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int c = tab.getPosition();
adapter.SetUnSelectView(tabs, c);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public class TabsPagerAdapter extends FragmentPagerAdapter {
private String tabTitles[] = new String[]{getString(R.string.tab1), getString(R.string.tab2)};
LinearLayout main;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
public View getTabView(int position) {
View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab_custom, null);
main = v.findViewById(R.id.main_view);
TextView title_tv = v.findViewById(R.id.tab_title_tv);
TextView services_tv = v.findViewById(R.id.services_num);
TextView products_tv = v.findViewById(R.id.products_num);
title_tv.setText(tabTitles[position]);
services_tv.setText("3 " + getString(R.string.services));
products_tv.setText("4 " + getString(R.string.products));
return v;
}
public void SetOnSelectView(TabLayout tabLayout, int position) {
try {
TabLayout.Tab tab = tabLayout.getTabAt(position);
// NPE here due getCustomView == null only when locale == ar
View selected = tab.getCustomView();
LinearLayout main = selected.findViewById(R.id.main_view);
main.setAlpha(1);
} catch (Exception e) {
e.printStackTrace();
}
}
public void SetUnSelectView(TabLayout tabLayout, int position) {
try {
TabLayout.Tab tab = tabLayout.getTabAt(position);
View selected = tab.getCustomView();
LinearLayout main = selected.findViewById(R.id.main_view);
main.setAlpha(0.3f);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Fragment getItem(int i) {
return new TabDeatilsFragment();
}
@Override
public int getCount() {
return tabTitles.length;
}
}
Logcat公司
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com._Fragment$TabsPagerAdapter.SetOnSelectView(_Fragment.java:156)
at com._Fragment$1.onTabSelected(OrderDetails_Fragment.java:88)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1608)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1601)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1569)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:1330)
at android.support.design.widget.TabLayout$PagerAdapterObserver.onChanged(TabLayout.java:2850)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:294)
at com.duolingo.open.rtlviewpager.DelegatingPagerAdapter.notifyDataSetChanged(DelegatingPagerAdapter.java:106)
at com.duolingo.open.rtlviewpager.RtlViewPager.onRtlPropertiesChanged(RtlViewPager.java:67)