0

我無法將RecyclerView添加到我的片段。 超級類「RecyclerView」以紅色顯示。 幫我一把!無法將回收站添加到片段

java代碼:

package com.invincible.fragedit; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class TableList extends Fragment { 

    private RecyclerView recyclerView;//error line 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_list_table,container,false); 
    } 
} 

我已經連接在片段的XML文件recyclerview,但我不能讓RecyclerView的對象在我的片段類

XML代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
    android:id="@+id/frag_2" 
    android:layout_marginTop="80dp" 
    android:background="#c61414" 
    tools:context="com.invincible.fragedit.ListTable"> 

    <!-- TODO: Update blank fragment layout --> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </android.support.v7.widget.RecyclerView> 

    <TextView 
     android:id="@+id/table_value" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 
+0

您是否將jar文件作爲庫添加到項目中? –

+0

您是否在'build.gradle'中添加了依賴關係'compile'c​​om.android.support:appcompat-v7:25.1.1''? – Kapouter

回答

0

將導入添加到您的片段中,並檢查是否將它添加到您的build.gradle中,如compile 'com.android.support:recyclerview-v7:25.1.0'

import android.support.v7.widget.RecyclerView; 
+0

完成!但仍然是導入包中的錯誤以及片段類 –

+0

@ashu_legacy更新了我的回答 –

+0

thanx它的工作! :) –

0

請確保您的build.gradle添加

compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.android.support:cardview-v7:23.1.1' 
    compile 'com.android.support:recyclerview-v7:23.1.1' 
0

在您的Java類import語句缺少回收站視圖。 請執行以下步驟。

1-在build.gralde文件'compile'c​​om.android.support:appcompat-v7:25.0.1''中添加以下行。 2 - 清理並重新重建。在你的導入部分'import android.support.v7.widget.RecyclerView'中粘貼這一行;