gpt4 book ai didi

android - 如何自定义 PagerTitleStrip android

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:15 27 4
gpt4 key购买 nike

我必须以这种方式在 ViewPager 中自定义一个 PagerTitleStrip: enter image description here

我必须用与内页对应的数字创建圆圈,中间的圆圈必须更大。有人有什么建议吗?

最佳答案

您可以使用以下代码为您的 View Pager 创建自定义 View Pager Indicator

 public class  SimpleViewPagerIndicator extends LinearLayout implements
OnPageChangeListener {
private static final String TAG = SimpleViewPagerIndicator.class
.getSimpleName();

private Context context;
private ViewPager pager;
private OnPageChangeListener onPageChangeListener;
private LinearLayout itemContainer;
private List<ImageView> items;
private OnClickListener itemClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();

pager.setCurrentItem(position);
}
};

public SimpleViewPagerIndicator(Context context) {
super(context);
this.context = context;
setup();
}

public SimpleViewPagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setup();
}

public SimpleViewPagerIndicator(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setup();
}

private void setup() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (inflater != null) {
inflater.inflate(R.layout.view_pager_indicator, this);

itemContainer = (LinearLayout) findViewById(R.id.pager_indicator_container);

items = new ArrayList<ImageView>();
}
}

/**
* Notifies the pager indicator that the data set has changed. Be sure to
* notify the pager as well (though you may wish to place that call in here
* yourself).
*/
public void notifyDataSetChanged() {
if (pager != null && pager.getAdapter() != null) {

// remove the old items (if any exist)
itemContainer.removeAllViews();

// I'm sure this could be optimised a lot more, eg,
// by reusing existing ImageViews, but it
// does the job well enough for now.
items.removeAll(items);

// now create the new items.
for (int i = 0; i < pager.getAdapter().getCount(); i++) {

ImageView item = new ImageView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
item.setLayoutParams(lp);
if (i == pager.getCurrentItem()) {
item.setImageResource(R.drawable.selected_dot);
} else {
item.setImageResource(R.drawable.un_selected_dot);
}

item.setTag(i);
item.setOnClickListener(itemClickListener);
items.add(item);

itemContainer.addView(item);
}
}
}

public ViewPager getViewPager() {
return pager;
}

public void setViewPager(ViewPager pager) {
this.pager = pager;
this.pager.setOnPageChangeListener(this);
}

public OnPageChangeListener getOnPageChangeListener() {
return onPageChangeListener;
}

public void setOnPageChangeListener(
OnPageChangeListener onPageChangeListener) {
this.onPageChangeListener = onPageChangeListener;
}

private void setCurrentItem(int position) {
if (pager != null && pager.getAdapter() != null) {
int numberOfItems = pager.getAdapter().getCount();

for (int i = 0; i < numberOfItems; i++) {
ImageView item = items.get(i);
if (item != null) {
if (i == position) {
item.setImageResource(R.drawable.selected_dot);
} else {
item.setImageResource(R.drawable.un_selected_dot);
}
}
}
}
}

@Override
public void onPageScrollStateChanged(int state) {
if (this.onPageChangeListener != null) {
this.onPageChangeListener.onPageScrollStateChanged(state);
}
}

@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
if (this.onPageChangeListener != null) {
this.onPageChangeListener.onPageScrolled(position, positionOffset,
positionOffsetPixels);
}
}

@Override
public void onPageSelected(int position) {
setCurrentItem(position);
if (this.onPageChangeListener != null) {
this.onPageChangeListener.onPageSelected(position);
}
}
}

您只需更改可绘制对象即可。

关于android - 如何自定义 PagerTitleStrip android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720221/

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