如何使用daimajia / AndroidImageSlider库更改图像滑动时TextView的文本?

时间:2017-12-23 08:57:58

标签: java android androidimageslider

我正在尝试在我的应用中使用daimajia / AndroidImageSlider库for imageslider。我试图改变TextView的文本与滑块中的图像的更改,但我无法在TextView内部看到任何文本,还有一件事,图像不是按照我插入内部的顺序进入的HashMap中。

我的代码是: -

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_home, container, false);

    tv_slider=(TextView)v.findViewById(R.id.tv_slider);
    tv_slider.setTypeface(EasyFonts.robotoLight(this.getActivity()));
    tv_button=(TextView)v.findViewById(R.id.tv_button);
    tv_button.setTypeface(EasyFonts.robotoLight(this.getActivity()));


    mViewPager = (SliderLayout) v.findViewById(R.id.pager);


    HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
    file_maps.put("Ecommerce",R.drawable.ecommerce);
    file_maps.put("Digital Marketing",R.drawable.digital_marketing);
    file_maps.put("Explainer Videos",R.drawable.explainer);
    file_maps.put("It Services", R.drawable.it_services);
    file_maps.put("Mobile App",R.drawable.mobile_app);
    file_maps.put("SEO",R.drawable.seoimage);
    file_maps.put("Software",R.drawable.software);
    file_maps.put("Web Design", R.drawable.webdesign);

    for(String name : file_maps.keySet()){
        DefaultSliderView textSliderView = new DefaultSliderView(getActivity());
        // initialize a SliderLayout
        textSliderView

                .image(file_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);


        textSliderView.bundle(new Bundle());
        textSliderView.getBundle()
                .putString("extra",name);

        mViewPager.addSlider(textSliderView);
    }

    mViewPager.setPresetTransformer(SliderLayout.Transformer.Accordion);
    mViewPager.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
    mViewPager.setDuration(4000);
    mViewPager.setCustomIndicator((PagerIndicator) v.findViewById(R.id.indicator));


    mViewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch(motionEvent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    Log.w("touched","down");
                    mViewPager.stopAutoCycle();
                    return true;


                case MotionEvent.ACTION_UP:
                    Log.w("touched","up");
                    mViewPager.startAutoCycle();
                    return true;

            }



            return true;
        }
    });


    mViewPager.addOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            switch (position){

                case 0:
                    tv_slider.setText(getString(R.string.es_content));
                    break;

                case 1:
                    tv_slider.setText(getString(R.string.dm_content));
                    break;

                case 2:
                    tv_slider.setText(getString(R.string.ev_content));
                    break;

                case 3:
                    tv_slider.setText(getString(R.string.it_content));
                    break;

                case 4:
                    tv_slider.setText(getString(R.string.md_content));
                    break;

                case 5:
                    tv_slider.setText(getString(R.string.seo_content));
                    break;

                case 6:
                    tv_slider.setText(getString(R.string.sd_content));
                    break;

                case 7:
                    tv_slider.setText(getString(R.string.wd_content));
                    break;

            }

        }

        @Override
        public void onPageSelected(int position) {

            currentPage = position;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });


    return v;


}

2 个答案:

答案 0 :(得分:1)

我已修复Random images进入滑块的问题,将HashMap替换为LinkedHashMap,并在ABDevelopers的答案帮助下附加幻灯片更改文字。 我希望下面的代码可以帮助其他开发人员。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        tv_slider=(TextView)v.findViewById(R.id.tv_slider);
        tv_slider.setTypeface(EasyFonts.robotoLight(this.getActivity()));
        tv_button=(TextView)v.findViewById(R.id.tv_button);
        tv_button.setTypeface(EasyFonts.robotoLight(this.getActivity()));


        mViewPager = (SliderLayout) v.findViewById(R.id.pager);


        final LinkedHashMap<String,Integer> file_maps = new LinkedHashMap<String, Integer>();
        file_maps.put("Ecommerce",R.drawable.ecommerce);
        file_maps.put("Digital Marketing",R.drawable.digital_marketing);
        file_maps.put("Explainer Videos",R.drawable.explainer);
        file_maps.put("It Services", R.drawable.it_services);
        file_maps.put("Mobile App",R.drawable.mobile_app);
        file_maps.put("SEO",R.drawable.seoimage);
        file_maps.put("Software",R.drawable.software);
        file_maps.put("Web Design", R.drawable.webdesign);

        for(String name : file_maps.keySet()){
            DefaultSliderView textSliderView = new DefaultSliderView(getActivity());
            // initialize a SliderLayout
            textSliderView

                    .image(file_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);


            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",name);

            mViewPager.addSlider(textSliderView);
        }

        mViewPager.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mViewPager.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
        mViewPager.setDuration(4000);
        mViewPager.setCustomIndicator((PagerIndicator) v.findViewById(R.id.indicator));
        mViewPager.addOnPageChangeListener(this);

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch(motionEvent.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.w("touched","down");
                        mViewPager.stopAutoCycle();
                        return true;
                    //break;

                    case MotionEvent.ACTION_UP:
                        Log.w("touched","up");
                        mViewPager.startAutoCycle();
                        return true;
                    //break;
                }



                return true;
            }
        });


        mViewPager.addOnPageChangeListener(new OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                switch (position){

                    case 0:
                        tv_slider.setText(getString(R.string.es_content));
                        break;

                    case 1:
                        tv_slider.setText(getString(R.string.dm_content));
                        break;

                    case 2:
                        tv_slider.setText(getString(R.string.ev_content));
                        break;

                    case 3:
                        tv_slider.setText(getString(R.string.it_content));
                        break;

                    case 4:
                        tv_slider.setText(getString(R.string.md_content));
                        break;

                    case 5:
                        tv_slider.setText(getString(R.string.seo_content));
                        break;

                    case 6:
                        tv_slider.setText(getString(R.string.sd_content));
                        break;

                    case 7:
                        tv_slider.setText(getString(R.string.wd_content));
                        break;

                }



            }


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




            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });






        return v;


    }

答案 1 :(得分:0)

尝试将以下方法放在OnPageSelected

 @Override
public void onPageSelected(int position) {

switch (position){

         case 0:
                tv_slider.setText(getString(R.string.es_content));
                    break;

         case 1:
                tv_slider.setText(getString(R.string.dm_content));
                    break;

         case 2:
                  tv_slider.setText(getString(R.string.ev_content));
                    break;
           .
           .
           .

            }
}