RecyclerView没有显示

时间:2017-04-28 16:18:52

标签: android android-fragments android-recyclerview picasso mvp

我遇到了一个问题,我的RecyclerView在从服务器端获取数据后没有显示出来。 当我尝试调试我的代码时 - 有时它会转到DataAdapter类 - 有时候不会。不幸的是,我不明白为什么会这样。

我正在使用MVP模式,也许我正在将数据设置为适配器错误。

我会尝试将代码放在这里并解释我在做什么。

首先我的卡片查看xml文件:

<?xml version="1.0" encoding="utf-8"?>

<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="match_parent"
    android:layout_height="200dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <ImageView android:id="@+id/info_image"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1.0"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/info_text"
            android:layout_marginLeft="5dp"
            android:layout_marginBottom="5dp"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

这是我的适用于Recycler的适配器视图:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private AutoService[] autoServices = new AutoService[]{};
    private Context context;

    private static final int IMAGE_WIDTH = 120;
    private static final int IMAGE_HEIGHT = 60;

    //TODO check how to properly set data on view from presenter
    public void setAutoServices(AutoService[] autoServices) {
        this.autoServices = autoServices;
    }

    public DataAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).
                inflate(R.layout.auto_service_card_view, parent, false);
        return new ViewHolder(cardView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CardView cardView = holder.cardView;
        ImageView imageView = (ImageView)cardView.findViewById(R.id.info_image);
        Picasso.with(context).load(autoServices[position].getImageURL()).resize(IMAGE_WIDTH, IMAGE_HEIGHT).into(imageView);
        TextView textView = (TextView)cardView.findViewById(R.id.info_text);
        textView.setText(autoServices[position].getServiceName());
    }

    @Override
    public int getItemCount() {
        return autoServices.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private CardView cardView;

        public ViewHolder(CardView card) {
            super(card);
            cardView = card;
        }
    }
}

在我的片段(视图)中,我有一个setter,用于设置适配器的数据。 日期来自演示者,该演示者从服务器端使用休息模板调用数据。

这是我的主持人:

public class RecyclerViewPresenter implements RecyclerViewContract.Presenter {

    private RestTemplate restTemplate;
    private RecyclerViewContract.View view;

    public RecyclerViewPresenter(RecyclerViewContract.View view){
        this.view = view;
        new RecyclerViewTask().execute();
    }

    private class RecyclerViewTask extends AsyncTask<Void, Void, AutoService[]>{

        @Override
        protected AutoService[] doInBackground(Void... voids) {
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            ResponseEntity<AutoService[]> autoServiceEntity = restTemplate.getForEntity(URL.getAllAutoServices(), AutoService[].class);
            return autoServiceEntity.getBody();
        }

        @Override
        protected void onPostExecute(AutoService[] autoServices) {
            view.setAutoServiceListOnAdapter(autoServices);
        }
    }
}

出于某种原因,当我的片段(RecyclerView)活动开始时 - 有时不会显示recyclelerView someTimes。可能是我使用MVP设置适配器错误的数据吗?

此外,我正在使用Picasso库 - 图片未从网址路径加载,例如网址:https://pbs.twimg.com/profile_images/616076655547682816/6gMRtQyY.jpg。 可能是毕加索图书馆的问题? 有什么建议吗?

public class RecyclerViewFragment extends Fragment implements RecyclerViewContract.View{

    private RecyclerViewContract.Presenter presenter;
    private DataAdapter dataAdapter;

    public RecyclerViewFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(presenter == null){
            presenter = new RecyclerViewPresenter(this);
        }
    }

    public static RecyclerViewFragment getInstance(){
        return new RecyclerViewFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_recycler_view, container, false);
        dataAdapter = new DataAdapter(getActivity());
        recyclerView.setAdapter(dataAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        return recyclerView;
    }

    @Override
    public void setPresenter(RecyclerViewContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void setAutoServiceListOnAdapter(AutoService[] autoServices) {
        dataAdapter.setAutoServices(autoServices);
    }

此外,当我尝试使用回收站视图打开活动时,它没有显示我有下一个日志:

W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe1b01280, error=EGL_SUCCESS
D/OpenGLRenderer: endAllStagingAnimators on 0xeb8d2e80 (RippleDrawable) with handle 0xe1b2dab0

当我用Recycler视图重新打开活动并显示时 - 我有下一个日志:

W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
D/OpenGLRenderer: endAllStagingAnimators on 0xeb8d2e80 (RippleDrawable) with handle 0xe25165b0
A/libc: Fatal signal 4 (SIGILL), code 2, fault addr 0xf6f1d46e in tid 2539 (16/6gMRtQyY.jpg)

并且应用程序在此之后崩溃。

检查我的回答。

2 个答案:

答案 0 :(得分:0)

尝试返回正确的片段根视图组,而不是小部件视图。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
View  view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
        dataAdapter = new DataAdapter(getActivity());
        recyclerView.setAdapter(dataAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        return view;
    }

答案 1 :(得分:0)

修改:我在适配器获取数据的位置添加了dataAdapter.notifyDataSetChanged();行。

@Override
        public void setAutoServiceListOnAdapter(AutoService[] autoServices) {
            dataAdapter.setAutoServices(autoServices);
            dataAdapter.notifyDataSetChanged();
        }