2017-03-03 92 views
0

我RecyclerView如我們下面的圖片可以正常使用的android高達5.1.1安卓:RecyclerView Android中6和7個怪,在做工精細5

enter image description here

但在Android的6(無論是在模擬器和設備)。它是隻顯示如下圖所示。(但2天前它的工作perfectly..strange圖片中的第一行)

它只能顯示第一行的Android 6和7

請查看我的代碼..

enter image description here

應用搖籃

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "xxx" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     multiDexEnabled true 
     vectorDrawables.useSupportLibrary = true 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     jackOptions { 
      enabled true 
     } 

    } 

    buildTypes { 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 


    dexOptions { 
     javaMaxHeapSize "2g" 
     preDexLibraries = false 
    } 

    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_8 
     targetCompatibility JavaVersion.VERSION_1_8 
    } 
} 


dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 

    // compile 'com.jakewharton:butterknife:8.4.0' 
    compile 'com.android.support:appcompat-v7:25.2.0' 
    compile 'com.android.support:design:25.2.0' 
    compile 'com.android.support:support-v4:25.2.0' 
    compile 'com.android.support:recyclerview-v7:25.2.0' 
    compile 'com.android.support:cardview-v7:25.2.0' 
    compile 'com.android.support:support-core-utils:25.2.0' 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.afollestad.material-dialogs:core:0.9.2.3' 
    compile 'com.afollestad.material-dialogs:commons:0.9.2.3' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
    compile 'com.squareup.retrofit2:retrofit:2.1.0' 
    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.google.code.gson:gson:2.7' 
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' 
    testCompile 'junit:junit:4.12' 
} 

CategoryFragment

public class CategoryFragment extends Fragment { 


    private List<Category> categoryList; 
    private RecyclerView recyclerCategory; 
    private int catID = 2; 

    private ProgressDialog progressDialog; 

    public CategoryFragment() { 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_category, container, false); 

     recyclerCategory = (RecyclerView) v.findViewById(R.id.recycler_category); 
     recyclerCategory.setNestedScrollingEnabled(false); 

     progressDialog = Utils.generateProgressDialog(getActivity(), false); 


     callCategoryApi(catID); 



     return v; 
    } 

    private void callCategoryApi(final int catId) { 
     progressDialog.show(); 
     Log.e("calling", "api cat"); 
     final CategoryApi categoryApi = ServiceGenerator.createService(CategoryApi.class); 
     categoryApi.getCategories(catId).enqueue(new Callback<CategoryResp>() { 
      @Override 
      public void onResponse(Call<CategoryResp> call, Response<CategoryResp> response) { 
       progressDialog.hide(); 
       if (response.isSuccessful()) { 
        categoryList = new ArrayList<Category>(); 
        CategoryResp resp = response.body(); 
        categoryList = resp.getData().getCategories(); 

        if (categoryList.size() > 0) { 
         recyclerCategory.setLayoutManager(new GridLayoutManager(getContext(), 4)); 

         CategoryAdapter categoryAdapter = new CategoryAdapter(getActivity(), categoryList); 
         recyclerCategory.setAdapter(categoryAdapter); 


        } else { 

        } 


       } 
      } 

      @Override 
      public void onFailure(Call<CategoryResp> call, Throwable t) { 
       progressDialog.hide(); 
      } 
     }); 
    } 


} 

適配器

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> { 

    private List<Category> dataList; 
    private Context mContext; 


    public CategoryAdapter(Context mContext, List<Category> dataList) { 
     this.mContext = mContext; 
     this.dataList = dataList; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_category_level_one, null); 
     v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_category_level_one, null); 

     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, final int position) { 


       Picasso.with(mContext).load(dataList.get(position).getCategoryIcon()).into(holder.imageCategoryItem); 

    } 

    @Override 
    public int getItemCount() { 
     return dataList.size(); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     ImageView imageCategoryItem; 


     public ViewHolder(View itemView) { 
      super(itemView); 
      imageCategoryItem = (ImageView) itemView.findViewById(R.id.image_category_single_item); 
     } 
    } 
} 

單項

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginTop="10dp" 
    android:background="@android:color/white" 
    card_view:cardUseCompatPadding="true"> 

    <LinearLayout 
     android:id="@+id/layout_category" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:padding="3dp"> 

     <ImageView 
      android:id="@+id/image_category_single_item" 
      android:layout_width="80dp" 
      android:layout_height="60dp" 
      android:scaleType="centerInside" /> 

     <TextView 
      android:id="@+id/txt_category_single_item" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_below="@+id/image_category_single_item" 
      android:gravity="center" 
      android:maxLines="2" 
      android:paddingBottom="8dp" 
      android:paddingTop="8dp" 
      android:textSize="12sp" /> 

    </LinearLayout> 

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

片段XML

<FrameLayout 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" 
    android:background="@android:color/white"> 

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

</FrameLayout> 

我不知道該怎麼水庫olve this issue ...

+0

請發表烏爾佈局 –

+0

兩添加單項佈局和片段佈局。 –

+0

通過以下方式替換您的適配器的onCreateViewHolder中的inflater行:View v = LayoutInflater.from(parent.getContext())。inflate(R.layout.single_item_category_level_one,parent,false); – HAXM

回答

1

發現我proplem的解決方案! ..這是因爲我在片斷的XML使用的FrameLayout ..我把它改爲RelativeLayout的..現在代碼適用於所有Android版本

片段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="match_parent" 
    android:background="@android:color/white"> 

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

</RelativeLayout> 
0

不要在項目視圖中使用match_parent作爲高度。一個項目垂直填充整個屏幕,所以你不會看到另一個。

在你的情況的LinearLayout android:id="@+id/layout_category"

+0

我測試了它通過改變它爲「wrap_content」和100dp ..但不能解決問題..如果match_parent是問題,我們可以滾動回收站視圖,並找到下一個項目..但在這種情況下沒有滾動.. –

+0

它與Android 5.1.1工作正常,但在6這個問題來.. –

相關問題