在viewPager android中使用箭头导航?我尝试了不同的例子,但没有成功
作者:互联网
>我想使用左右箭头进行导航..
>我已经厌倦了使用按钮,但没有什么好开心.
我试过的是以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instruction);
preferenceHelper = new PreferenceHelper(Instruction.this);
viewPager = (ViewPager) findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
txtrank = (TextView)findViewById(R.id.rank);
tvRight=(TextView)findViewById(R.id.tvRight);
tvLeft=(TextView)findViewById(R.id.tvLeft);
/* tvRight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(viewPager.getCurrentItem() < viewPager.getRight())
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
}
});
tvLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewPager.getCurrentItem() > viewPager.getLeft())
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
}
});*/
// Generate sample data
rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
/* sharedPreferences = getSharedPreferences("ShaPreferences",
Context.MODE_PRIVATE); SharedPreferences.Editor editor =
sharedPreferences.edit(); boolean firstTime =
sharedPreferences.getBoolean("first", true);
if (firstTime) {
editor.putBoolean("first", false); editor.commit();
viewPager.setAdapter(myPagerAdapter);
Intent intent = new Intent(Instruction.this, LoginActivity.class);
startActivity(intent);
} else { Intent intent = new Intent(Instruction.this,
LoginActivity.class); startActivity(intent); }*/
}
private class MyPagerAdapter extends PagerAdapter {
int NumberOfPages = 9;
int[] res = { R.drawable.a0, R.drawable.b1, R.drawable.c2,
R.drawable.d3, R.drawable.e4, R.drawable.f5, R.drawable.g6,
R.drawable.h7, R.drawable.i8 };
/*
* int[] backgroundcolor = { 0xFF101010, 0xFF202020, 0xFF303030,
* 0xFF404040, 0xFF505050, 0xFF101010, 0xFF202020, 0xFF303030,
* 0xFF404040, 0xFF303030};
*/
@Override
public int getCount() {
return NumberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
/*
* TextView textView = new TextView(Instruction.this);
* textView.setTextColor(Color.WHITE); textView.setTextSize(20);
* textView.setTypeface(Typeface.DEFAULT_BOLD);
* textView.setText(String.valueOf(position));
*/
txtrank.setText(rank[position]);
ImageView imageView = new ImageView(Instruction.this);
imageView.setImageResource(res[position]);
LayoutParams imageParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
imageView.setLayoutParams(imageParams);
LinearLayout layout = new LinearLayout(Instruction.this);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// layout.setBackgroundColor(backgroundcolor[position]);
layout.setLayoutParams(layoutParams);
// layout.addView(textView);
layout.addView(imageView);
final int page = position;
/*layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
* Toast.makeText(Instruction.this, "Page " + page +
* " clicked", Toast.LENGTH_LONG) .show();
}
});*/
if (viewPager.getCurrentItem() == 7) {
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent);
}
Log.e("at page number", String.valueOf(viewPager.getCurrentItem()));
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.left:
if(viewPager.getCurrentItem() < viewPager.getLeft())
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
break;
case R.id.right:
if(viewPager.getCurrentItem() < viewPager.getRight())
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
break;
}
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
– 我想要的是..用户可以通过滑动以及使用向右或向左箭头移动到下一个图像..我能够滑动..但箭头不起作用.
解决方法:
如果你想使用滑动和箭头,那么你必须使用这样的左右听众.
right.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(pager.getCurrentItem() < pager.getRight())
pager.setCurrentItem(pager.getCurrentItem()+1,true);
}
});
left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pager.getCurrentItem() > pager.getLeft())
pager.setCurrentItem(pager.getCurrentItem()-1,true);
}
});
标签:viewpagerindicator,android,android-viewpager 来源: https://codeday.me/bug/20190829/1761710.html