【发布时间】:2017-07-23 03:06:25
【问题描述】:
我的 RecyclerView 在 android 5.1.1 上完美运行,如下图所示
但在 Android 6 中(在模拟器和设备中).. 它只显示第一行,如下图所示..(但在 2 天之前它工作得很好..奇怪)
在 android 6 和 7 中只显示第一行
请检查我的代码..
应用 Gradle
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'
}
类别片段
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>
我不知道如何解决这个问题...
【问题讨论】:
-
请发布你的布局
-
单项布局和片段布局都增加了..
-
在适配器的 onCreateViewHolder 中替换你的充气线,如下所示,View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_category_level_one, parent, false);
-
我试过了..没有改变发生@HAXM
标签: java android android-studio android-recyclerview retrofit2