gpt4 book ai didi

android - ViewPager 内 fragment 的按钮在错误引用时触发 onClickListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:29 44 4
gpt4 key购买 nike

对不起,我的标题很蠢,我会在下面描述清楚:

情况

我有一个 ViewPager,里面有 4 个 OnBoardingFragment。每个 Fragment 都有完全相同的布局,它是从同一个 xml 文件膨胀的。此布局包含一个 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() 方法,并且该方法被引用到最后一个 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}

问题

为什么会发生,如何解决?

最佳答案

请引用以下链接中的示例,以使用具有多个 fragment 的 View 寻呼机:https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter

关于android - ViewPager 内 fragment 的按钮在错误引用时触发 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973200/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com