Android SliderLayout 轮播
引入依赖:
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.daimajia.slider:library:1.1.5@aar'
错误信息:
Program type already present: android.support.v4.app.ActionBarDrawerToggle$DelegateProvider
Message{kind=ERROR, text=Program type already present: android.support.v4.app.ActionBarDrawerToggle$DelegateProvider, sources=[Unknown source file], tool name=Optional.of(D8)}
所以还需要引入
implementation 'com.android.support:support-v4:27.0.2'
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.daimajia.slider.library.SliderLayout
android:id="@+id/slider_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="InvalidId">
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="@+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
custom:selected_color="@color/colorPrimary"
custom:selected_height="3dp"
custom:selected_padding_left="2dp"
custom:selected_padding_right="2dp"
custom:selected_width="16dp"
custom:shape="rect"
custom:unselected_color="#55333333"
custom:unselected_height="3dp"
custom:unselected_padding_left="2dp"
custom:unselected_padding_right="2dp"
custom:unselected_width="16dp" />
</com.daimajia.slider.library.SliderLayout>
</RelativeLayout>
</LinearLayout>
MainActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SliderLayout sliderLayout;
private PagerIndicator indicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initImageSlider();
}
private void initImageSlider() {
sliderLayout = findViewById(R.id.slider_layout);
indicator = findViewById(R.id.custom_indicator);
List<String> imageUrls = new ArrayList<>();
final List<String> descriptions = new ArrayList<>();
imageUrls.add("http://m.360buyimg.com/mobilecms/s300x98_jfs/t2416/102/20949846/13425/a3027ebc/55e6d1b9Ne6fd6d8f.jpg");
imageUrls.add("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1507/64/486775407/55927/d72d78cb/558d2fbaNb3c2f349.jpg");
imageUrls.add("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1363/77/1381395719/60705/ce91ad5c/55dd271aN49efd216.jpg");
descriptions.add("新品推荐");
descriptions.add("时尚男装");
descriptions.add("家电秒杀");
for (int i = 0; i < imageUrls.size(); i++) {
TextSliderView tsv = new TextSliderView(this);
tsv.image(imageUrls.get(i)).description(descriptions.get(i));
final int finalI = i;
tsv.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
@Override
public void onSliderClick(BaseSliderView slider) {
Toast.makeText(MainActivity.this,descriptions.get(finalI), Toast.LENGTH_SHORT).show();
}
});
sliderLayout.addSlider(tsv);
}
sliderLayout.setCustomAnimation(new DescriptionAnimation());
sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
sliderLayout.setDuration(3000);
sliderLayout.setCustomIndicator(indicator);
}
}