0

我有一个现有的适配器类,它在 android.support.design.widget.CoordinatorLayout 父级中有 android.support.v7.widget.RecyclerView 和 android.support.design.widget.AppBarLayout 。见下文。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background_grey"
style="@style/layoutParent">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_red_blue_gradient"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="@dimen/_8sdp"
        android:paddingStart="@dimen/_8sdp"
        android:paddingRight="@dimen/_8sdp"
        android:paddingEnd="@dimen/_8sdp"
        app:layout_collapseMode="none"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

        <com.android.somecustomlayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


    <com.android.someothercustomlayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/_5sdp"
    android:layout_marginLeft="@dimen/_5sdp"
    android:layout_marginStart="@dimen/_5sdp"
    android:layout_marginRight="@dimen/_5sdp"
    android:layout_marginEnd="@dimen/_5sdp"
    android:layout_marginBottom="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

这是适配器类的代码

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

private ArrayList<ResultModel> mResultModel;
private Context mContext;

public ABC (Context context, ArrayList<ResultModel> resultModel) {
    mContext = context;
    this.mResultModel = resultModel;

     mParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.abc, parent, false);
    return new RowItemViewHolder(headerView);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

    RowItemViewHolder rowItemViewHolder = (RowItemViewHolder) holder;

rowItemViewHolder.textview.settext(mResultModel.get(position).mName.get(0));
 //   computations and logic for views


}

@Override
public int getItemCount() {
    if (null != ResultModel) {
        return mResultModel.size();
    } else {
        return 0;
    }
}



}


@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.itemView.clearAnimation();
}



private static class RowItemViewHolder extends RecyclerView.ViewHolder {

    private textview textview1;
.....

    RowItemViewHolder(View itemView) {
        super(itemView);

textview = (textview) itemView.findViewById(R.id.some_id_here);

}


/**
 * Method help to set listener on {@link ResultRowItemListener}
 *
 * @param itemListener instance of {@link ResultRowItemListener}
 */
public void setResultRowItemListener(ResultRowItemListener itemListener) {
    mRowItemClickListener = itemListener;
}

/**
 * Listener that provide method on row item click
 */
public interface FlightSearchResultRowItemListener {
    void onResultRowItemClick(ResultModel ResultModel, int position);
}
}

一切正常,直到我将 compileSdkVersion 从 23 更新到 27。我还更新了 buildToolsVersion、com.android.support:appcompat-v7、com.android.support:support-v4、com.android.support:design、com.android。支持:cardview-v7,com.android.support:recyclerview-v7 到 27.1.1。

请参阅下面的 build.gradle

 apply plugin: 'com.android.library'

repositories {
maven { url 'http://repo1.maven.org/maven2' }
jcenter { //noinspection GroovyAssignabilityCheck
    url 'http://jcenter.bintray.com/' }
}



android {
signingConfigs {
    release {
        keyAlias 
        keyPassword 
        storeFile file('')
        storePassword ''
    }
    debug {
        keyAlias 
        keyPassword 
        storeFile file('')
        storePassword ''
    }
}
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
    minSdkVersion 17
    targetSdkVersion 27
    signingConfig signingConfigs.release
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
}

dependencies {
testCompile 'junit:junit:4.12'
//noinspection GradleCompatible,GradleDependency
compile 'com.android.support:appcompat-v7:27.1.1'
//noinspection GradleDependency
compile 'com.android.support:support-v4:27.1.1'
//noinspection GradleDependency
compile 'com.android.support:design:27.1.1'
//noinspection GradleDependency
compile 'com.android.support:cardview-v7:27.1.1'
//noinspection GradleDependency
compile 'com.android.support:recyclerview-v7:27.1.1'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.8.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.3'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.pkmmte.view:circularimageview:1.1'
compile 'com.google.android.gms:play-services-auth:15.0.1'
compile 'com.google.firebase:firebase-core:16.0.1'
compile 'com.android.support:multidex:1.0.3'
implementation 'br.com.simplepass:loading-button-android:1.14.0'
}

在此之后,我的 recyclerview 呈现为空白。当我尝试调试适配器类时,只调用了该类的构造函数,并且调试永远不会到达任何适配器方法,如 onCreateViewHolder 和 onBindViewHolder。

经过一番调查,我得出了几个我想分享的结论:

  • 如果在适配器布局中,对于 RecyclerView,如果我将宽度和高度更改为每个 500dp,我可以看到列表,但当它们是 match_parent 时看不到。
  • 如果不是 CoordinatorLayout 作为父级,而是更改为 Relativelayout,则列表视图可见但不使用 CoordinatorLayout 。

我已经研究过其他解决方案,例如Recyclerview 不调用任何适配器方法:onCreateViewHolder,onBindViewHolder,

但他们没有帮助,因为问题仅在将 compilesdkversion 更新为 27 后才出现。

4

0 回答 0