gpt4 book ai didi

java - 在 CustomPagerAdapter 中隐藏按钮

转载 作者:行者123 更新时间:2023-11-29 06:56:04 25 4
gpt4 key购买 nike

我编写了以下自定义 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/

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