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

时间:2015-12-01 10:05:23

标签: android android-viewpager viewpagerindicator

  • 我想使用左右箭头进行导航..
  • 我已经厌倦了使用按钮,但没什么好开心的。 - 我尝试过的是:

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

- 我想要的是..用户可以通过滑动以及使用向右或向左箭头移动到下一个图像..我能够滑动..但是箭头不起作用。

4 个答案:

答案 0 :(得分:3)

如果你想使用滑动和箭头,那么你必须使用这样的左右听众。

DATEDIFF('second', [begin date], [end date])

答案 1 :(得分:1)

试试这个

public static HttpContextBase FakeHttpContext(HttpVerbs verbs, NameValueCollection nameValueCollection)
{
    var httpContext = new Mock<HttpContextBase>();
    var user = new ApplicationUser { 
            Id = "abc", 
            TimezoneId = "America/New_York" 
        };

    var request = new Mock<HttpRequestBase>();
    request.Setup(c => c.Form).Returns(nameValueCollection);
    request.Setup(c => c.QueryString).Returns(nameValueCollection);
    request.Setup(c => c.RequestType).Returns(verbs.ToString().ToUpper());

    var response = new Mock<HttpResponseBase>();
    var session = new Mock<HttpSessionStateBase>();
    var server = new Mock<HttpServerUtilityBase>();
    httpContext.Setup(c => c.Request).Returns(request.Object);

    var u = verbs.ToString().ToUpper();

    httpContext.Setup(c => c.Response).Returns(response.Object);
    httpContext.Setup(c => c.Server).Returns(server.Object);
    httpContext.Setup(c => c.User.Identity.GetUserId()).Returns("abc");
    httpContext.Setup(c => c.User.ApplicationUser()).Returns(user);
    return httpContext.Object;
}

答案 2 :(得分:1)

试试这个:

 @Override 
public void onClick(View v) {
    int currentItemPosition = adapter.getItemPosition(viewpager.getCurrentItem());
    int itemCount = adapter.getCount();
    switch (v.getId())
    { 
        case R.id.left:
            if(currentItemPosition > 0) {
                viewPager.setCurrentItem(currentItemPosition - 1, true);
            }
            break; 

        case R.id.right:
            if(currentItemPosition < itemCount-1 ) {
                viewPager.setCurrentItem(currentItemPosition + 1, true);
            }
            break; 

    } 
} 

答案 3 :(得分:0)

尝试以下

为左右箭头实现2个按钮。 在单击函数调用中:

a)如果点击箭头 - viewPager.arrowScroll(View.FOCUS_LEFT);

b)如果点击了右侧箭头 - viewPager.arrowScroll(View.FOCUS_RIGHT);