我有一个现有的适配器类,在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.support: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 not call any Adapter method :onCreateViewHolder,onBindViewHolder,
但是他们没有帮助,因为问题仅在将compilesdkversion更新为27之后出现。