我在一个活动中有2个Tab片段。活动在中心有一个圆形图像视图。当我刷第一片时,我需要显示第一张图片&与第二个标签滑动相同,不同的图像。
更新
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putInt("position", position);
switch (position) {
case 0:
imgProfile.setImageResource(R.drawable.login_logo);
return new LogInFragment();
case 1:
imgProfile.setImageResource(R.drawable.add_image);
return new SignUpFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LOGIN";
case 1:
return "SIGNUP";
}
return null;
}
}
答案 0 :(得分:1)
var margin: CGFloat {
return 10.0
}
var width: CGFloat {
return 100 - margin
}
答案 1 :(得分:1)
好的,我现在明白你想做什么: 首先删除更改图像的所有代码,然后尝试:
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
yourImageView.setImageResource(R.id.yourResource1);
fragment = new LogInFragment();
break;
case 1:
yourImageView.setImageResource(R.id.yourResource2);
fragment = new SignUpFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
String str = "";
switch (position) {
case 0:
str = "LOGIN";
break;
case 1:
str = "SIGNUP";
break;
}
return str;
}
}
这就是你所需要的一切
答案 2 :(得分:0)
最后通过使用以下代码在@sasikumar回答中进行了一些修改。
我没有把 onPageScrollStateChanged 放在 onPageSelected
上。viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int i = 0;
if (position == 0) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.login_logo);
} else if (position == 1) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.add_image);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});