作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了以下自定义 ViewPager
适配器:
public class CustomPagerAdapter extends PagerAdapter {
private int[] image_resources = {
android.R.color.transparent,
R.drawable.1,
R.drawable.2,
};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomPagerAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (RelativeLayout) o);
}
@Override
public CharSequence getPageTitle(int position) {
String[] titlesArray = {
"",
"Image 1",
"Image 2",
};
return titlesArray[position];
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
imageview.setImageResource(image_resources[position]);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
我在另一个类中设置了 Adapter
:
private void init(Context context) {
View view = inflate(context, R.layout.listview_item, this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ViewPager viewPager;
CustomPagerAdapter adapter;
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new CustomPagerAdapter(context);
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(wrappedAdapter);
//Hide the button unless showing image
Button selectButton = (Button) findViewById(R.id.selectButton);
selectButton .setVisibility(GONE);
}
我如何隐藏 ViewPager
的第一个“幻灯片”上的按钮,即图像是 android.R.color.transparent
的位置,但显示它每隔一张幻灯片?
最佳答案
我无法完全理解您的问题,但我相信这应该可以解决您的问题:
private void init(Context context) {
View view = inflate(context, R.layout.listview_item, this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ViewPager viewPager;
CustomPagerAdapter adapter;
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new CustomPagerAdapter(context);
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(wrappedAdapter);
//Hide the button unless showing image
final Button selectButton = (Button) findViewById(R.id.selectButton);
selectButton .setVisibility(GONE);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(position==0)
selectButton.setVisibility(View.GONE);
else
selectButton.setVisibility(View.VISIBLE);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
关于java - 在 CustomPagerAdapter 中隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33886979/
我编写了以下自定义 ViewPager 适配器: public class CustomPagerAdapter extends PagerAdapter { private int[] image_
我是一名优秀的程序员,十分优秀!