首页 > 解决方案 > 为什么recyclerview 在android pie 上不起作用?

问题描述

我有一个问题。

将 build.gradle 更新为 Android Support Revision 28.0.0 后,recyclerview 不会在 android 9 模拟器上加载。在物理设备上也会发生同样的情况。在以前的版本上工作正常。

然后我尝试使用 android x 进行迁移,但没有任何改变。我使用调试器来查找 gradle 是否进入 recyclerview 适配器,但它没有。这是我的构建。毕业文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "ge.softservice.photobucket"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1-beta01', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha02'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.squareup.retrofit2:retrofit:2.2.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
}

这是我的 recyclerview 适配器

package ge.softservice.photobucket;

import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import ge.softservice.photobucket.model.Printing;

/**
 * Created by Soft Service on 22-Mar-17.
 */

public class PrintingAdapter extends RecyclerView.Adapter<PrintingAdapter.Holder> {

    private static final String TAG = PrintingAdapter.class.getSimpleName();

    private List<Printing> mPrinting;

        public PrintingAdapter() {
        mPrinting = new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new Holder(item);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        Printing printingObject = mPrinting.get(position);
        holder.mName.setText(printingObject.getName());
        String link = printingObject.getImage();
        Picasso.with(holder.itemView.getContext())
                .load(link).into(holder.mPhoto);
    }

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

    public void addPrinting(Printing printing) {
        mPrinting.add(printing);
        notifyDataSetChanged();
        Log.d(TAG, printing.getImage());
    }

    public class Holder extends RecyclerView.ViewHolder {

        private ImageView mPhoto;
        private Button mName;

        public Holder(View itemView) {
            super(itemView);
            mPhoto = itemView.findViewById(R.id.photo);
            mName = itemView.findViewById(R.id.button);
        }
    }
}

应该是这样的

但是是这样的

这是片段中的recyclerview java代码

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        rootView.setTag(TAG);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mPrintingAdapter = new PrintingAdapter();
        mRecyclerView.setAdapter(mPrintingAdapter);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mRecyclerView.addOnScrollListener(new MyRecyclerScroll() {
            @Override
            public void show() {
                fab.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
            }

            @Override
            public void hide() {
                fab.animate().translationY(fab.getHeight() + fabMargin).setInterpolator(new AccelerateInterpolator(2)).start();
            }
        });

        mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), mRecyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(getActivity().getApplicationContext(), "You just tapped # " + position, Toast.LENGTH_SHORT).show();
                 switch (position) {
                     case 0:
                     Fragment magnetListFragment = new MagnetListFragment();
                     FragmentTransaction transaction = getFragmentManager().beginTransaction();
                     transaction.replace(R.id.fragment_container, magnetListFragment);
                     transaction.addToBackStack(TAG_MAGNET_LIST_FRAGMENT);
                     transaction.commit();
                     break;
                     case 1:
                         Fragment calendarFragment = new CalendarFragment();
                         FragmentTransaction transactionCalendar = getFragmentManager().beginTransaction();
                         transactionCalendar.replace(R.id.fragment_container, calendarFragment);
                         transactionCalendar.addToBackStack(TAG_CALENDAR);
                         transactionCalendar.commit();
                         break;
                 }
            }
        }));

// it continues

RestManager mManager;
        mManager = new RestManager();
        Call<List<Printing>> listCall = mManager.getPrintingService().getAllPrintings();
        listCall.enqueue(new Callback<List<Printing>>() {
            @Override
            public void onResponse(Call<List<Printing>> call, Response<List<Printing>> response) {
                if (response.isSuccessful()) {
                    List<Printing> printingList = response.body();

                    for (int i = 0; i <printingList.size() ; i++) {
                        Printing printing = printingList.get(i);
                        mPrintingAdapter.addPrinting(printing);
                    }
                }
                else {
                    int sc = response.code();
                    switch (sc) {

                    }
                }
            }

            @Override
            public void onFailure(Call<List<Printing>> call, Throwable t) {

            }
        });

这是xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- A RecyclerView with some commonly used attributes -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@drawable/ic_photo_camera_black_24dp" />

</FrameLayout>

在这里,LinearLayoutManager 是红色的。是不是因为这个?

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/magnetList"
    android:name="ge.softservice.photobucket.ItemFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context="ge.softservice.photobucket.MagnetListFragment"
    tools:listitem="@layout/magnet_item" />

标签: javaandroid

解决方案


推荐阅读