如何在Android中获取ViewPager指标onClick

时间:2017-04-11 06:21:41

标签: android android-viewpager

我希望ViewPager根据其indicator点击进行滑动。我已经使用JakeWharton的library作为ViewPager indicator,但根据我的要求,我没有任何正确的解决方案。我在这个网站上看到很多关于这个的问题,但没有得到任何适当的解决方案。任何人都可以帮助我如何通过点击indicator view pager来获取此类功能。下面是我的MainActivity.class

    public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private ArrayList<Integer> alPhoto;
    private CirclePageIndicator mIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loadArrayList();

        // Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(MainActivity.this,alPhoto);
        viewPager.setAdapter(adapter);

        // Set for ViewPager indicator.
        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);
        mIndicator.setSnap(true);

    }

    private void loadArrayList() {
        try {
            alPhoto = new ArrayList<>();
            alPhoto.add(R.drawable.first);
            alPhoto.add(R.drawable.second);
            alPhoto.add(R.drawable.third);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的适配器。

    public class ViewPagerAdapter  extends PagerAdapter {

    private Context context;
    private ArrayList<Integer> alPhoto;
    private LayoutInflater inflater;


    public ViewPagerAdapter(Context mainActivity,ArrayList<Integer> alPhoto) {
        this.context= mainActivity;
        this.alPhoto=alPhoto;
    }

    @Override
    public int getCount() {
        return alPhoto.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imgflag;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.viewpager_item, container,false);

        imgflag = (ImageView) itemView.findViewById(R.id.flag);
        imgflag.setImageResource(alPhoto.get(position));

        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试设置onClick侦听器来处理指标? 像这样:

SELECT g FROM Game g LEFT JOIN g.rollbackGames rb WHERE rb.game IS NULL