gpt4 book ai didi

android - 在 viewPager android 中使用箭头进行导航?我尝试了不同的例子但没有成功

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:49 25 4
gpt4 key购买 nike

  • 我想使用向右和向左箭头进行导航..
  • 我已经厌倦了使用按钮,但没有任何反应。-我试过的如下:

    @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);
}
});

关于android - 在 viewPager android 中使用箭头进行导航?我尝试了不同的例子但没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34017619/

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