对于我的愚蠢标题感到抱歉,我将在下面清楚地描述:
场合
我的ViewPager
内有4 OnBoardingFragment
个。每个Fragment
都具有与同一xml文件中inflated
完全相同的布局。此布局包含Button
,我调用了btnNext
,并为其设置了OnClickListener
。
我的PagerAdapter的功能getItem
@Override
public Fragment getItem(int position) {
String title, description, button;
int resource;
boolean end = false;
switch (position) {
case 0:
title = context.getString(R.string.on_boarding_title_1);
description = context.getString(R.string.on_boarding_description_1);
resource = R.drawable.on_boarding_bg_0;
button = context.getString(R.string.on_boarding_button_1);
break;
case 1:
title = context.getString(R.string.on_boarding_title_2);
description = context.getString(R.string.on_boarding_description_2);
resource = R.drawable.on_boarding_bg_1;
button = context.getString(R.string.on_boarding_button_2);
break;
case 2:
title = context.getString(R.string.on_boarding_title_3);
description = context.getString(R.string.on_boarding_description_3);
resource = R.drawable.on_boarding_bg_2;
button = context.getString(R.string.on_boarding_button_3);
break;
default:
title = context.getString(R.string.on_boarding_title_4);
description = context.getString(R.string.on_boarding_description_4);
resource = R.drawable.on_boarding_bg_3;
button = context.getString(R.string.on_boarding_button_4);
end = true;
}
return OnBoardingFragment.newInstance(title, description, resource, button, end);
}
@Override
public int getCount() {
return 4;
}
OnBoardingFragment
的功能:
public static OnBoardingFragment newInstance(String title, String description, int resource,
String button, boolean end) {
Bundle bundle = new Bundle();
bundle.putString(EXTRA_TITLE, title);
bundle.putString(EXTRA_DESCRIPTION, description);
bundle.putInt(EXTRA_IMAGE, resource);
bundle.putString(EXTRA_BUTTON, button);
bundle.putBoolean(EXTRA_END, end);
OnBoardingFragment fragment = new OnBoardingFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString(EXTRA_TITLE);
description = bundle.getString(EXTRA_DESCRIPTION);
button = bundle.getString(EXTRA_BUTTON);
end = bundle.getBoolean(EXTRA_END);
imageResource = bundle.getInt(EXTRA_IMAGE);
show log ---> Log.e(this.toString() + "/" + end + "/" + title);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.on_boarding_fragment, container, false);
TextView tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
TextView tvDescription = (TextView) rootView.findViewById(R.id.tv_description);
TextView btnDiscovery = (TextView) rootView.findViewById(R.id.tv_discovery);
imageView = (ImageView) rootView.findViewById(R.id.iv_image);
final Button btnNext = (Button) rootView.findViewById(R.id.btn_next);
if (end) {
btnDiscovery.setVisibility(View.VISIBLE);
btnDiscovery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRegionActivity();
}
});
}
btnNext.setText(button);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show log ---> Log.e(OnBoardingFragment.this.toString() + "/" + end + "/" + title + "/" + ((Button)v).getText());
if (end) {
((SplashActivity) getActivity()).gotoNextPage();
} else {
startLoginActivity();
}
}
});
tvTitle.setText(title);
tvDescription.setText(description);
ImageUtils.loadBitmap(getActivity(), imageResource, imageView, 0.8f, new ImageUtils.LoadBitmapCallback() {
@Override
public void onLoadComplete(ImageView imageView) {
}
@Override
public void onLoadFail() {
}
});
return rootView;
}
问题是
当我的应用开始时,它显示第一个 Fragment
,然后我按下Button
并触发onItemClick()
方法,该方法被引用到< strong>最后 Fragment
。
创建Fragment
OnBoardingFragment{a8ede47 id=0x7f10013b}/false/XIN CHÀO!
OnBoardingFragment{8787674 #0 id=0x7f10013b}/false/BÁN LIỀN TAY, KIẾM TIỀN NGAY
OnBoardingFragment{119f79d #1 id=0x7f10013b}/false/CHAT MIỄN PHÍ
OnBoardingFragment{1ee7412 #2 id=0x7f10013b}/true/NGƯỜI THẬT, HÀNG THẬT
触发onClickListener()
时记录
OnBoardingFragment{1ee7412 #2 id=0x7f10013b}/true/NGƯỜI THẬT, HÀNG THẬT/Đi chợ ngay nào!
初始化btnNext
时记录:
button: android.support.v7.widget.AppCompatButton{17c69e97 VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{31941c VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{30765b87 VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
button: android.support.v7.widget.AppCompatButton{9cf19e VFED..C. ......I. 0,0-0,0 #7f1002c8 app:id/btn_next}
问题
为什么会发生以及如何解决?
答案 0 :(得分:2)
请参阅以下链接中的示例,以使用包含多个片段的视图寻呼机:https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter
答案 1 :(得分:0)
当用户点击按钮时,将该事件发送回活动(或者您有viewpager的参考)。从那里,您应该检查viewpager中的哪些片段是可见的(请参阅Determine when a ViewPager changes pages和How to determine when Fragment becomes visible in ViewPager)然后您将知道如何处理该事件。
答案 2 :(得分:0)
您可以使用侦听器接口来获取片段并创建一个获取位置的方法,并将其发送到您声明viewpager的位置 1.首先在该片段中创建该接口的匿名对象 2.其次将该对象传递给PagerAdapter 3.第三,您可以在PagerAdapter中使用该对象,并使用片段位置调用接口中使用的方法 4.您可以在该接口的匿名内部类中的方法中包含片段布局处理代码
请试试这个,它会起作用。希望你能理解我所说的话。
答案 3 :(得分:0)
最后使用case 3:
break
,default
,只需输入一个break
而不是其他语句。
答案 4 :(得分:0)
在默认块中添加break语句,并在0,1,2
的情况下传递参数end = false