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

How to change Text of the TextView on sliding of images using daimajia/AndroidImageSlider Library?

我正在尝试在我的应用程序中使用 daimajia/AndroidImageSlider 图像滑块库。我正在尝试通过更改滑块中的图像来更改 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;


}

尝试将您的以下方法放入 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;
           .
           .
           .

            }
}

我已经解决了 Random images 进入滑块的问题,方法是将 HashMap 替换为 LinkedHashMap 并在 [=14 的回答的帮助下随幻灯片的变化附加文本=]. 我希望下面的代码也能帮助其他开发人员。

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


    }