如何配置Smarteist/Android-Image-Slider?

hgc7kmma  于 2023-11-15  发布在  Android
关注(0)|答案(2)|浏览(114)

我不知道如何使用smarteist/Android-Image-Slider实现后.我写在onCreate(),但SliderView不能解析符号
此实现链接https://github.com/smarteist/Android-Image-Slider

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    SliderView sliderView = findViewById(R.id.imageSlider);

    sliderView.setSliderAdapter(new SliderAdapterExample(this));

    sliderView.setIndicatorAnimation(IndicatorAnimations.SWAP); //set indicator animation by using    SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
    sliderView.setSliderTransformAnimation(SliderAnimations.DEPTHTRANSFORMATION);
    sliderView.setScrollTimeInSec(2); //set scroll delay in seconds :
    sliderView.startAutoCycle();

字符串

ctehm74n

ctehm74n1#

几个星期前,我有这个库的问题。我建议转储它,并创建自己的滑块与ViewHolder。您将需要图像加载库在这里的例子,我们使用Picasso。我们正在使用互联网加载图像添加此权限

<uses-permission android:name="android.permission.INTERNET" />

字符串
请按照以下步骤操作:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.codinginflow.picassoimagesliderexample.MainActivity">

</android.support.v4.view.ViewPager>


ViewPageAdapter.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class ViewPagerAdapter extends PagerAdapter {
    private Context context;
    private String[] imageUrls;

    ViewPagerAdapter(Context context, String[] imageUrls) {
        this.context = context;
        this.imageUrls = imageUrls;
    }

    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        Picasso.get()
                .load(imageUrls[position])
                .fit()
                .centerCrop()
                .into(imageView);
        container.addView(imageView);

        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}


MainActivity.java

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private String[] imageUrls = new String[]{
            "https://cdn.pixabay.com/photo/2016/11/11/23/34/cat-1817970_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/12/24/09/09/road-3036620_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/11/07/00/07/fantasy-2925250_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/10/10/15/28/butterfly-2837589_960_720.jpg"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = findViewById(R.id.view_pager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
        viewPager.setAdapter(adapter);
    }
}


这是我朋友CodingInFlow的代码

baubqpgj

baubqpgj2#

你可以使用Jummania-Slider代替任何其他的滑动库。在这个库中,你可以扩展你自己的布局。

**用法:**XML格式:

<com.jummania.JSlider
    android:id="@+id/jSlider"
    android:layout_width="match_parent"
    android:layout_height="222dp" />

字符串
活动中:

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      val jSlider: JSlider = findViewById(R.id.jSlider)
      jSlider.setSlider(DefaultSlider())

  }

  //Out of onCreate, Create a Class for Slider
      private inner class DefaultSlider : JSlider.DefaultSlider() {
    override fun getView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        return layoutInflater.inflate(R.layout.item_slider2, parent, false) //Inflate you layout
    }

    override fun onSliderCreate(view: View, position: Int) {

        val textView: TextView = view.findViewById(R.id.text_view) //find your child
        val imageView: ImageView = view.findViewById(R.id.image_view)

        Picasso.get()
            .load("https://jummania.com/App/BanglaNatokSamahar/Images/Cover%20Photo.jpg")
            .error(R.drawable.default_error).placeholder(R.drawable.default_loading)
            .into(imageView)

        textView.text = getString(R.string.Developer_Name)

        view.setOnClickListener {

            Toast.makeText(
                this@MainActivity, getString(R.string.Developer_Name), Toast.LENGTH_SHORT
            ).show()
        }

    }

    override fun getCount(): Int {
        return 3
    }

}


如果您需要定制,请查看此库的README.md。

相关问题