gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:21:16 24 4
gpt4 key购买 nike

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


}

最佳答案

我已通过将 HashMap 替换为 LinkedHashMap 并在幻灯片更改时附加文本来解决 Random images 进入 slider 的问题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;


}

关于java - 如何使用 daimajia/AndroidImageSlider 库在图像滑动时更改 TextView 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com