我有一个实现ActionBar.TabListener的活动,以提供3个导航标签。在其中一个标签中,我引入了一个RatingBar来评估该元素。目前,更改评级会触发确认所选评级的祝酒词。
当旋转设备的方向时,即使额定值没有改变,也会再次触发此吐司 - 我知道我可能正在重新绘制此选项卡的片段(尽管在这种情况下我不明白为什么评级仍然存在)但我不明白如何阻止它。我试图在OnCreateView中检查SavedInstanceState但是如果我只在非null时应用侦听器,则旋转设备会完全删除侦听器。
活动onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pub);
Intent intent = getIntent();
pubname = intent.getStringExtra(MainActivity.PUB);
setTitle(pubname);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
actionBar.setSelectedNavigationItem(1);
}
FragmentPAgerAdapter类
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return DirectionsFragment.newInstance(position + 1);
case 1:
return DetailsFragment.newInstance(position + 1);
case 2:
return RelationsFragment.newInstance(position + 1);
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1);
case 1:
return getString(R.string.title_section2);
case 2:
return getString(R.string.title_section3);
}
return null;
}
}
DetailsFragment Class
public static class DetailsFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static DetailsFragment newInstance(int sectionNumber) {
DetailsFragment fragment = new DetailsFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_pub_details, container, false);
RatingBar ratingBar = (RatingBar)rootView.findViewById(R.id.rating);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(ratingBar.getContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
我错过了哪些明显的伎俩?
答案 0 :(得分:0)
好的,所以我已经改变了onRatingChanged函数,只有当fromUser布尔值为真时才触发它的内容 - 显然当系统重新绘制标签时它就不会触发吐司; <\ n / p>
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if(fromUser){
Toast.makeText(ratingBar.getContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
}
}
});
我感到不舒服的是,我不知道这是否是一个黑客,或者这是否是预期的android进程 - 我的标签明显被破坏和重建 - 这是出于布局目的还是我的实施有缺陷?
我很难理解最佳做法,所以请随时指出正确的方法&#39;如果不是这样的话。