我知道这个问题可能很奇怪,但是请在按下大拇指按钮之前先听我说。
我有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)
答案 0 :(得分:0)
我遇到了同样的问题,就解决了。
您的问题是,当调用notifyDataSetChanged时,所有以前的CustomViews都被重置了
您需要检查SetOnSelectView()中的自定义视图是否为空,然后在notifyDataSetChanged之后重新分配customViews。
希望这会有所帮助