我正在尝试在我的应用中使用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;
}
答案 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;
.
.
.
}
}