图像滑块未显示所有幻灯片

时间:2017-05-19 09:53:12

标签: android

我正在开发一个欢迎屏幕,将显示四张幻灯片。我已经创建了适配器并将其设置在活动中。每件事都很好但问题是屏幕只显示第一张和第三张幻灯片。但如果我手动滚动,我可以看到所有的屏幕。我无法弄清楚为什么会这样。

这是我的活动课程:

public class ProgramLandingActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG = ProgramLandingActivity.class.getSimpleName();
    ActivityProgramLandingBinding binding;
    private ViewPager viewPager;
    private MyViewPagerAdapter myViewPagerAdapter;
    private int[] layouts = {
            R.drawable.slider3,
            R.drawable.slider1,
            R.drawable.slider2,
           R.drawable.slider4};

    private int[] icons = {
            R.drawable.note,
            R.drawable.mic,
            R.drawable.up_arrow,
            R.drawable.weight};

    private String[] text = {
            "Personalized Running Programs \n and flexible calender",
            "Audio tips in Hindi and English \n From your mentors,\n Gul Panag and Milind Soman",
            "Track your running, see achievements and share daily progress","includes weight loss and running performance nutrition plans"};

    private ArrayList<Integer> layoutArray = new ArrayList<Integer>();
    private ArrayList<Integer> layouticons = new ArrayList<Integer>();
    private ArrayList<String> layouttext = new ArrayList<String>();
    private static int currentPage = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_program_landing);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        intializeViews();

        setAdapter();

    }

    private void setAdapter() {
        for (int i = 0; i < layouts.length; i++) {
            layoutArray.add(layouts[i]);
            layouticons.add(icons[i]);
            layouttext.add(text[i]);
            myViewPagerAdapter = new MyViewPagerAdapter(this, layoutArray,layouticons,layouttext);
            viewPager.setAdapter(myViewPagerAdapter);
            CirclePageIndicator indicator = (CirclePageIndicator) findViewById(R.id.indicator);
            indicator.setViewPager(viewPager);

            Log.d("laypou_length", String.valueOf(layouts.length));

        // Auto start of viewpager
            final Handler handler = new Handler();
            final Runnable Update = new Runnable() {
                public void run() {
                    if (currentPage == layouts.length) {
                        currentPage = 0;
                    }
                    viewPager.setCurrentItem(currentPage++, true);
                }
            };
            Timer swipeTimer = new Timer();
            swipeTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.post(Update);
                }
            }, 2500, 2500);

            indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    currentPage = position;

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }


    private void intializeViews() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);

    }

我的适配器类:

class MyViewPagerAdapter extends PagerAdapter {


    private ArrayList<Integer> layouts;
    private ArrayList<Integer> icons;
    private ArrayList<String> text;
    private LayoutInflater inflater;
    private Context context;

    public MyViewPagerAdapter(Context context,ArrayList<Integer> layouts, ArrayList<Integer> icons,ArrayList<String> text ) {
        this.context = context;
        this.layouts=layouts;
        this.icons = icons;
        this.text=text;
        inflater = LayoutInflater.from(context);
    }

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

        View myImageLayout = inflater.inflate(R.layout.slider,container,false);
        ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.image1);
        ImageView icon = (ImageView)myImageLayout.findViewById(R.id.icon);
        TextViewWithFont slider_text = (TextViewWithFont)myImageLayout.findViewById(R.id.text_program_landing_body);

        myImage.setImageResource(layouts.get(position));
        icon.setImageResource(icons.get(position));
        slider_text.setText(text.get(position));
        container.addView(myImageLayout, 0);
        return myImageLayout;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}

有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个 您可以像这样用户自动滚动

Timer timer;
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, 3000); // delay*/

 private class RemindTask extends TimerTask {
    int current = viewPager.getCurrentItem();

    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            public void run() {

                if (current < layouts.length) {
                    viewPager.setCurrentItem(current);
                    current += 1;
                } else {
                    timer.cancel();
                }
            }
        });

    }
}

答案 1 :(得分:0)

看起来问题出在&#39; viewpager.setCurrentPage&#39;为'&#39; currentPage&#39;在那里,看看它是否被调用两次。

让我知道评论中的观察结果。 干杯:)