如何导入 RecyclerView for Android L-preview

Posted

技术标签:

【中文标题】如何导入 RecyclerView for Android L-preview【英文标题】:How to import RecyclerView for Android L-preview 【发布时间】:2014-08-17 21:59:13 【问题描述】:

尝试使用支持库中的新 RecyclerView。我使用 SDK 管理器下载了支持库的 20 更新。

我已将 jar 文件添加到 libs 文件夹 - 并添加到构建路径 - 使用 RecyclerView 运气不好。

根据android Developer's API 还尝试使用 gradle 依赖项 - 不确定这是否是正确的查看位置 - 此页面与 AndroidTV 更多相关:

 com.android.support:recyclerview-v7:20.0.+

无法同步项目。

有什么想法吗?

【问题讨论】:

【参考方案1】:

想通了。

您必须添加以下 gradle 依赖项:

compile 'com.android.support:recyclerview-v7:+'

我编译的另一个问题是compileSdkVersion。显然你必须针对android-L编译它

您的 build.gradle 文件应如下所示:

apply plugin: 'android'
android 
    compileSdkVersion 'android-L'
    buildToolsVersion '19.1.0'
    [...]

dependencies 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:+'

【讨论】:

compileSdkVersion 'android-L' - 提供 INSTALL_FAILED_OLDER_SDK 你是如何用 Eclipse 做的? this reddit post 在收到 INSTALL_FAILED_OLDER_SDK 错误后让我更进一步。 为什么支持库依赖于最新版本的Android?这违背了拥有支持库的全部目的。 如果您想针对旧版本的 Android 进行编译,您可以。为了摆脱 INSTALL_FAILED_OLDER_SDK 错误,您需要将 AndroidManifest.xml 文件中的 <uses-sdk /> 声明替换为:<uses-sdk tools:node="replace" />【参考方案2】:

我使用以下几行创建,这对我有用。

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'

AndroidX

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'

【讨论】:

错误:任务 ':app:dexDebug' 执行失败。 > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' 以非完成-零退出值2。当我在项目中添加第二个依赖项时运行项目时出现此错误。拜托,任何人修复它??。 我的应用程序的 gradle.build 已经编译了 'com.android.support:appcompat-v7:23.2.1'。所以我尝试添加 compile 'com.android.support:recyclerview-v7:23.2.1' - 与 appcompat 相同的版本,但 gradle 同步失败,尝试更改为“+”,如 'com.android.support: recyclerview-v7:23.2.+',也失败了:(【参考方案3】:

这对我有用:

compile 'com.android.support:recyclerview-v7:21.0.0-rc1'

【讨论】:

这不是问题,你写的只是他已经包含的另一个版本【参考方案4】:

现在可以通过在 app gradle 中编译设计依赖来添加 RecyclerView:

dependencies 
...
compile 'com.android.support:design:24.0.0'

【讨论】:

投反对票github.com/android/platform_frameworks_support/blob/master/…的人可以在这里看到【参考方案5】:

如果您已编译 SDK 版本 22.2.0,则添加以下依赖项以用于回收站视图和 cardview 附加以支持 cardView

// 用于将所有库包含在目录lib中compile fileTree(include: ['*.jar'], dir: 'libs') // 支持 appcompatcompile 'com.android.support:appcompat-v7:22.2.0' //用于包含 google 支持设计(这使得从 2.3 及更高版本实现材料设计主题成为可能) `编译'com.android.support:design:22.2.0'

要添加回收器视图,请使用以下依赖项 compile 'com.android.support:recyclerview-v7:22.2.0'

之后点击 Build->rebuild project 就完成了。

【讨论】:

【参考方案6】:
compile 'com.android.support:recyclerview-v7:24.2.1'

这对我有用。 试试看。

【讨论】:

【参考方案7】:

我面前的步骤只是少了一步。

修改build.gradle(Module:app)并添加以下依赖项后:

编译'com.android.support:cardview-v7:21.0.+' 编译'com.android.support:recyclerview-v7:21.0.+'

(必要时添加卡片视图)

然后您必须转到 Build > Clean Project 以消除任何错误

【讨论】:

谢谢,我在 Android Studio 上苦苦挣扎了好几个小时,你的 Clean Project 想法成功了,再次感谢。【参考方案8】:
dependencies 
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.0'

只需在 build.gradle 文件中创建您的依赖项,对我有用。

【讨论】:

【参考方案9】:

在我的情况下,我通过将 compile 'com.android.support:recyclerview-v7:22.0.0' 作为依赖项放入我的 gradle 构建来修复它

(使用 Android studio v. 1.2.1.1 和所有 sdk 更新。)

当代码更新得如此之快,IDE 无法跟踪它们,并且您必须手动修复它们时,这真的很烦人,浪费时间和资源。

不过好吧,终于成功了。

【讨论】:

【参考方案10】:

build.gradle 中包含依赖项,并将项目与gradle 文件同步

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:25.1.0'

    //include the revision no, i.e 25.1.1
    implementation 'com.android.support:recyclerview-v7:25.1.1'

包括修订版(此处为 25.1.1)以避免不可预测的构建,请查看 library revisions

【讨论】:

【参考方案11】:
implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.android.support:recyclerview-v7:28.0.0'

以上在 build.gradle 文件中对我有用

【讨论】:

【参考方案12】:

我使用了一个小技巧来在旧设备上使用 RecyclerView。我刚刚进入我的本地 m2 存储库并拿起 RecyclerView 源文件并将它们放入我的项目中。

你可以在这里找到源代码:

\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1\recyclerview-v7-21.0.0-rc1-sources.jar

【讨论】:

【参考方案13】:

其他答案对我不起作用。我不得不添加这一行:

编译'com.android.support:recyclerview-v7:21.0.0'

【讨论】:

【参考方案14】:

-转到activity_main.xml中的DESIGN部分 - 在拖放托盘中选择 appCompactivity - 在 appCompactivity 中选择 RecyclerView - 在选择时会出现一个对话框,点击OK - 你的项目 app:gradle 会自动更新

【讨论】:

【参考方案15】:

我的依赖;

dependencies 
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.1.0'

    //RecyclerView dependency
    compile 'com.android.support:recyclerview-v7:25.1.0'

    // Instrumentation dependencies use androidTestCompile
    // (as opposed to testCompile for local unit tests run in the JVM)
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support:support-annotations:25.1.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'

我只添加了compile 'com.android.support:recyclerview-v7:25.1.0'。重要的是添加与 appcompat

相同版本 的 RecycleView 依赖项

【讨论】:

【参考方案16】:

如果您使用 Android Studio 的更新版或 2018 版...

compile 'com.android.support:recyclerview-v7:+'

会给你一个错误并显示以下消息 “配置‘编译’已过时,已替换为‘实现’和‘API’。 它将在 2018 年底被删除。”

试试这个

implementation 'com.android.support:recyclerview-v7:+'

【讨论】:

【参考方案17】:

我用这个对我有用。需要考虑的一件事是您使用的是什么appcompat 版本。我正在使用appcompat-v7:26.+,所以这对我有用。

implementation 'com.android.support:recyclerview-v7:26.+'

【讨论】:

【参考方案18】:

将 RecyclerView 导入您的项目的一个好方法是RecyclerViewLib。这是一个开源库,它提取了 RecyclerView 以使其安全且易于实现。你可以阅读作者的博文here。

在代码中添加以下行作为 gradle 依赖项:

dependencies 
    compile 'com.twotoasters.RecyclerViewLib:library:1.0.+@aar'

更多关于如何引入 gradle 依赖的信息:

波斯尼亚你说得对,这很烦人。 Gradle 可能看起来很复杂,但它非常强大和灵活。一切都是用 groovy 语言完成的,学习 gradle 系统就是学习另一种语言,这样你就可以构建你的 Android 应用程序。现在很痛,但从长远来看,你会喜欢它的。

查看同一应用的 build.gradle。 https://github.com/twotoasters/RecyclerViewLib/blob/master/sample/build.gradle 它执行以下操作的地方是将 lib 带入模块(也称为示例应用程序)的地方

compile (project (':library')) 
    exclude group: 'com.android.support', module: 'support-v4' 

注意这个文件的位置。这不是*** build.gradle

因为 lib 源代码在同一个项目中,所以可以使用简单的 ':library' 来完成此操作。 exclude 告诉库使用示例应用程序的支持 v4。这不是必需的,但这是一个好主意。您没有或不想在您的项目中拥有该库的源代码,因此您必须指向互联网。在您的模块/应用程序的 build.gradle 中,您可以将此答案开头的那一行放在同一位置。或者,如果按照示例示例,您可以将 ':library' 替换为 ' com.twotoasters.RecyclerViewLib:library:1.0.+@aar ' 并使用排除项。

【讨论】:

我收到了这个错误:Could not find any version that matches com.twotoasters.RecyclerViewLib:library:1.0.+ :-/ 您是否将它放在 build.gradle 中的正确位置?你把它放在你的模块 build.gradle 中了吗? “正确”的地方在哪里?我只是将这些行附加到我的 build.gradle 中,将其放在 android 的外部/内部,buildscript 的内部/外部,但对我没有任何作用。我是 gradle 的新手,它似乎非常复杂......当我读到“只要把这条线......”之类的东西时,它非常烦人...... :-( @Besnik 我用有关成绩构建系统的更多信息更新了我的答案。我希望这些信息对您有所帮助。 谢谢!我会在接下来的几天里试一试:-)【参考方案19】:
import android.support.v7.widget.RecyclerView;

在 Android Studio 中,导入并不像人们希望的那样直观。尝试导入这个位,看看它有什么帮助!

【讨论】:

问题是关于 Gradle 依赖项,而不是 Java 导入。【参考方案20】:

如果有人仍然有这个问题 - 您不必更改 compileSdkVersion,这只会破坏支持库的全部目的。

相反,在您的 gradle.build 文件中使用这些:

compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:palette-v7:+'`

【讨论】:

不建议使用+符号,至少指定主版本。【参考方案21】:

这对我有用

定义互联网权限

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

添加依赖

compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile ('com.squareup.retrofit2:converter-simplexml:2.1.0')
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'stax', module: 'stax'
        exclude group: 'xpp3', module: 'xpp3'
    

在主要活动中

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

public class MainActivity extends AppCompatActivity 
    private BreakfastMenu breakfastMenu;
    List<BreakfastMenu> list;
    TextView responseText;
    APIInterface apiInterface;
    String name;
    String price;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        responseText=(TextView)findViewById(R.id.reponseText);
        apiInterface = APIClient.getClient().create(APIInterface.class);


        /**
         GET List Resources
         **/
        Call<BreakfastMenu> call = apiInterface.getBreakfastMenu();
        call.enqueue(new Callback<BreakfastMenu>() 
            @Override
            public void onResponse(Call<BreakfastMenu> call, Response<BreakfastMenu> response) 
               Log.d("TAG", response.code() + "");

                String displayResponse = "";
                BreakfastMenu resource = response.body();
                System.out.println(displayResponse+"display response   ");

                for (Food food : resource.getFoodList())
                
                    name=food.getName();
                    price=food.getPrice();
                    System.out.println(name+price+"=========================================");
                    displayResponse += food.getName() + " " + food.getPrice()+"\n"+"\n";
                    Toast.makeText(MainActivity.this,name+price,Toast.LENGTH_LONG).show();
                
                responseText.setText(displayResponse);

            

            @Override
            public void onFailure(Call<BreakfastMenu> call, Throwable t) 
                call.cancel();
            
        );

    

制作 APIClient.java 类

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

class APIClient 

    private static Retrofit retrofit = null;

    static Retrofit getClient() 

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


        retrofit = new Retrofit.Builder()
                .baseUrl("https://www.w3schools.com/")
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .build();



        return retrofit;
    



    enter code here

Make APIInterface.java

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Query;

interface APIInterface 

    @GET("xml/simple.xml")
    @Headers("Accept: application/xml",
            "User-Agent: Retrofit-Sample-App")
    Call<BreakfastMenu> getBreakfastMenu();


In BreakfastMenu.java

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import java.util.List;

@Root(name = "breakfast_menu")
public class BreakfastMenu


    @ElementList(inline = true)
    private List<Food> foodList;

    public BreakfastMenu()
    
    

    public List<Food> getFoodList()
    
        return foodList;
    

    public void setFoodList(List<Food> foodList)
    
        this.foodList = foodList;
    

制作食物.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "food")
public class Food


    @Element(name = "name")
    private String name;

    @Element(name = "price")
    private String price;

    @Element(name = "description")
    private String description;

    @Element(name = "calories")
    private String calories;

    public Food()
    
    

    public String getName()
    
        return name;
    

    public void setName(String name)
    
        this.name = name;
    

    public String getPrice()
    
        return price;
    

    public void setPrice(String price)
    
        this.price = price;
    

    public String getDescription()
    
        return description;
    

    public void setDescription(String description)
    
        this.description = description;
    

    public String getCalories()
    
        return calories;
    

    public void setCalories(String calories)
    
        this.calories = calories;
    

在activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_ >

        <TextView
            android:id="@+id/reponseText"
            android:layout_
            android:layout_
          />


</Linear Layout>

【讨论】:

【参考方案22】:

只是更新:

'compile' 现在已经过时了;它已被“实施”和“API”取代。 我相信它将在 2018 年底被删除。更多信息请见:http://d.android.com/r/tools/update-dependency-configurations.html

所有 com.android.support 库也必须使用完全相同的版本规范;此外,appcompat-v7 和 recyclerview-v7 等支持库不应使用与 compileSdkVersion 不同的版本。

【讨论】:

以上是关于如何导入 RecyclerView for Android L-preview的主要内容,如果未能解决你的问题,请参考以下文章