1.在build文件中进行配置
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.slider:library:1.1.5@aar'2.权限
3.布局:
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp" />
android:id="@+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
custom:selected_color="#FF5500"
custom:unselected_color="#55333333"
custom:shape="rect"
custom:selected_padding_left="2dp"
custom:selected_padding_right="2dp"
custom:unselected_padding_left="2dp"
custom:unselected_padding_right="2dp"
custom:selected_width="16dp"
custom:selected_height="3dp"
custom:unselected_width="16dp"
custom:unselected_height="3dp"
android:layout_gravity="center" />
4.功能实现:
public class HomeFragment extends Fragment {
private SliderLayout mSliderLayout;
private PagerIndicator indicator;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false); //容器
mSliderLayout = (SliderLayout) view.findViewById(R.id.slider); //指示器,那些小点
indicator= (PagerIndicator) view.findViewById(R.id.custom_indicator);
initSlider();
return view;
}
private void initSlider(){ //SliderView有两种DefaultSliderView,TextSliderView用法一致,但是DefaultSliderView没有显示文字的功能 //显示图片和文字 TextSliderView textSliderView = new TextSliderView(this.getActivity()); textSliderView.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t2416/102/20949846/13425/a3027ebc/55e6d1b9Ne6fd6d8f.jpg"); textSliderView.description("新品推荐");
textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override
public void onSliderClick(BaseSliderView baseSliderView) {
Toast.makeText(HomeFragment.this.getActivity(), "新品推荐", Toast.LENGTH_LONG).show();
}
});
TextSliderView textSliderView2 = new TextSliderView(this.getActivity()); textSliderView2.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1507/64/486775407/55927/d72d78cb/558d2fbaNb3c2f349.jpg"); textSliderView2.description("新品水果");
textSliderView2.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override
public void onSliderClick(BaseSliderView baseSliderView) {
Toast.makeText(HomeFragment.this.getActivity(), "新品水果", Toast.LENGTH_LONG).show();
}
});
TextSliderView textSliderView3 = new TextSliderView(this.getActivity()); textSliderView3.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1363/77/1381395719/60705/ce91ad5c/55dd271aN49efd216.jpg"); textSliderView3.description("零食秒杀");
textSliderView3.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override
public void onSliderClick(BaseSliderView baseSliderView) {
Toast.makeText(HomeFragment.this.getActivity(), "零食秒杀", Toast.LENGTH_LONG).show();
}
}); //添加到容器中
mSliderLayout.addSlider(textSliderView);
mSliderLayout.addSlider(textSliderView2);
mSliderLayout.addSlider(textSliderView3); //使用默认的指示器 // mSliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); mSliderLayout.setCustomIndicator(indicator); //阴影显示的动画效果
mSliderLayout.setCustomAnimation(new DescriptionAnimation()); //图片的转场效果 // mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp);
mSliderLayout.setDuration(3000);
mSliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
Log.d(TAG,"onPageScrolled"); }
@Override
public void onPageSelected(int i) {
Log.d(TAG,"onPageSelected");
}
@Override
public void onPageScrollStateChanged(int i) {
Log.d(TAG,"onPageScrollStateChanged");
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
mSliderLayout.stopAutoCycle();
}
}