将 ViewPagerIndicator 添加到 Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25702884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:39:45  来源:igfitidea点击:

Add ViewPagerIndicator to Android Studio

androidgradleandroid-studioviewpagerindicator

提问by carstenbaumhoegger

i'm trying to get Jake Wharton's ViewPagerIndicator working with Android Studio but unfortunately it won't work.
I downloaded the .aar file from hereand included it in my libs folder.
I referenced it like this:

我正在尝试让 Jake Wharton 的 ViewPagerIndicator 与 Android Studio 一起工作,但不幸的是它不起作用。
我从这里下载了 .aar 文件并将其包含在我的 libs 文件夹中。
我是这样引用的:

compile files('src/main/libs/viewpagerindicator_2.4.1.aar')

Android Studio gives me the following error:

Android Studio 给了我以下错误:

Error:duplicate files during packaging of APK

I'm not very familiar with gradle and don't know what to do when it gives me this error.
Can you please help me with this one?

我对 gradle 不是很熟悉,当它给我这个错误时不知道该怎么做。
你能帮我解决这个问题吗?

Here's my complete build.gradle:

这是我完整的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "de.xxx"
        minSdkVersion 15
        targetSdkVersion 19
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }

    buildTypes {
     release {
     runProguard false
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
    }
}

dependencies {
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.github.amlcurran.showcaseview:library:5.0.0'
    compile files('src/main/libs/PdfViewer.jar')
    compile files('src/main/libs/viewpagerindicator_2.4.1.aar')
}

回答by Víctor Albertos

UPDATE

更新

Based on the answer given by Jürgen 'Kashban' Wahlmann, it is now possible to add ViewPagerIndicator via gradle:

根据Jürgen 'Kashban' Wahlmann 给出答案,现在可以通过 gradle 添加 ViewPagerIndicator:

Top Level Build.gradle:

顶级 Build.gradle:

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
    }
}

App's build.gradle:

应用程序的 build.gradle:

compile 'com.viewpagerindicator:library:2.4.1@aar'

Also, based on the answer given by Enrico Susatyonow it seems possible to download the library from Jitpack maven repositories. Do it as follows:

此外,根据Enrico Susatyo 给出答案,现在似乎可以从 Jitpack maven 存储库下载该库。请按以下步骤操作:

In root build.grade:

在根 build.grade 中:

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

In project build.grade:

在项目 build.grade 中:

dependencies {
            compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
    }

------------

------------

To use Android-ViewPagerIndicator in Android Studio, you can't download it from gradle. Instead, you must import the library as an “Existing Project” to your current one.

在 Android Studio 中使用 Android-ViewPagerIndicator 是不能从 gradle 下载的。相反,您必须将该库作为“现有项目”导入到您当前的项目中。

Follow these steps:

按着这些次序:

#1 Download source codefrom GitHub.

#1从 GitHub下载源代码

#2 In your Android Studio Project: File -> Project Structure -> add (+ symbol) -> Import Existing Project. Import just the folder called ”library”, not the entire project (leave the import options as Android Studio suggests).

#2 在您的 Android Studio 项目中:文件 -> 项目结构 -> 添加(+ 符号)-> 导入现有项目。只导入名为“library”的文件夹,而不是整个项目(保留 Android Studio 建议的导入选项)。

# 3 If the "compileSdkVersion" specified in your build.gradle doesn't match with the one specified in the Android-ViewPagerIndicator project, change the second one. The same apply with any other property, such as "minSdkVersion" or even the current support library.

#3 如果你的build.gradle中指定的“compileSdkVersion”与Android-ViewPagerIndicator项目中指定的不匹配,请更改第二个。这同样适用于任何其他属性,例如“minSdkVersion”甚至当前的支持库。

# 4 Add Android-ViewPagerIndicator project as a dependency to your build.gradle module:

# 4 将 Android-ViewPagerIndicator 项目添加为 build.gradle 模块的依赖项:

dependencies {
    compile project(':library')
}

# 5 Sync project with gradle files.

# 5 同步项目与 gradle 文件。

回答by Jürgen 'Kashban' Wahlmann

It can be imported by Gradle like this:

Gradle 可以像这样导入它:

Top Level Build.gradle:

顶级 Build.gradle:

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
    }
}

In your App's build.gradle add this to your dependencies section:

在您的 App 的 build.gradle 中,将其添加到您的依赖项部分:

compile 'com.viewpagerindicator:library:2.4.1@aar'

(Add @aar to avoid "packaging for apklib is not supported error")

(添加@aar 以避免“不支持打包apklib 错误”)

Works fine for me.

对我来说很好用。

回答by Enrico Susatyo

As of today (March 2016), Jitpack's maven repos work for me: https://jitpack.io/#JakeWharton/ViewPagerIndicator/2.4.1/aar

截至今天(2016 年 3 月),Jitpack 的 maven repos 为我工作:https://jitpack.io/#JakeWharton/ViewPagerIndicator/2.4.1/aar

In root build.gradle:

在根build.gradle

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

In project build.gradle:

在项目中build.gradle

dependencies {
            compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
    }

回答by Hitesh Sahu

You can do it without any third party library

您可以在没有任何第三方库的情况下完成

Final Result:

最后结果:

enter image description here

在此处输入图片说明

1) Use frame layout as a container for Viewpager and then add a Linearlayout at the bottom of it.

1)使用框架布局作为Viewpager的容器,然后在其底部添加一个Linearlayout。

<FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

                <android.support.v4.view.ViewPager
                    android:id="@+id/product_images_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:layout_gravity="bottom|center_horizontal"
                    android:layout_margin="10dp">

                    <LinearLayout
                        android:id="@+id/indicator_root"
                        android:layout_width="20dp"
                        android:layout_height="match_parent"
                        android:layout_gravity="bottom|center_horizontal"
                        android:gravity="center_horizontal"
                        android:orientation="horizontal">

                    </LinearLayout>
                </HorizontalScrollView>

            </FrameLayout>

2) Define size and margin for indicators

2) 定义指标的大小和边距

  //define globaly 
   private LinearLayout.LayoutParams imageParam;

   //init params
      int margin = Utils.pxFromDp(getActivity(), 5);
        int width = Utils.pxFromDp(getActivity(), 8);
        imageParam = new LinearLayout.LayoutParams(width, width);
        imageParam.setMargins(margin, margin, margin, margin);

3) Add Indicators in Linear Layout

3) 在线性布局中添加指标

 for (int indicatorCount = 0; indicatorCount < productFromShoppingList.getProductImages().size();
             indicatorCount++) {

            ImageView imageIndicator =
                    new ImageView(getActivity());

            imageIndicator.setAdjustViewBounds(true);
            imageIndicator.setScaleType(ImageView.ScaleType.FIT_XY);
            imageIndicator.setLayoutParams(imageParam);

            indicatorContainer.addView(imageIndicator);
            indicators.add(imageIndicator);
            imageIndicator.setBackgroundResource(R.drawable.indicator_unselected);
        }

4) Initialize indicator at 0 position

4) 在 0 位置初始化指标

 indicators.get(0).setBackgroundResource(R.drawable.indicator_unselected);

5) Update indicator on view pager page change

5) 在查看寻呼机页面更改时更新指示器

carousalViewPager.setAdapter(new slidingPagerAdapter(getActivity(),
            productFromShoppingList.getProductImages()));

    carousalViewPager
            .addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position,
                                           float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                    currentPageIndex = position;
                    updateIndicators(currentPageIndex);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });


private void updateIndicators(int selectedPostion) {
    for (int indicatorPosition = 0; indicatorPosition < indicators.size(); indicatorPosition++) {
        indicators.get(indicatorPosition).setBackgroundResource(indicatorPosition == selectedPostion ? R.drawable.indicator_selected
                : R.drawable.indicator_unselected);
    }
}

Last but not least add this 2 drawable for indicator

最后但并非最不重要的一点是为指标添加这 2 个 drawable

indicator_unselected.xml

indicator_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="@color/white" />
</shape>

indicator_selected.xml

indicator_selected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="100dp" />
    <solid android:color="@android:color/white" />
</shape>

回答by cV2

For me trying to integrate it from maven didn't work out. I always go the following build exceptions and styles couldn't be reoslved:

对我来说,尝试从 Maven 集成它没有成功。我总是去以下构建异常和样式无法解决:

no ressource found that matches the given name vpiCirclePageIndicatorStyle vpiTitlePageIndicatorStyle

没有找到与给定名称匹配的资源 vpiCirclePageIndicatorStyle vpiTitlePageIndicatorStyle

Adding the dependency via jitpack works just perfect:

通过 jitpack 添加依赖非常完美:

In root build.gradle:

在根build.gradle

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

In project build.gradle:

在项目中build.gradle

dependencies {
            compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
    }