Retrofit和RecyclerView不会在启动时显示项目

时间:2017-01-13 21:01:06

标签: android android-recyclerview retrofit

在我的应用程序中是Retrofit 1.9和RecyclerView。在启动时,HTTP请求正在发送,我接收数据,但不显示。在再次下载SwipeRefresh数据之后,但在此之前,Recycler正在显示以前的数据。

片段:

package com.example.bolek.testy.Fragments;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.DefaultItemAnimator;
import android.widget.Toast;

import com.example.bolek.testy.Adapters.ChaptersAdapter;
import com.example.bolek.testy.Interfaces.WebService;
import com.example.bolek.testy.Pojo.Chapter;
import com.example.bolek.testy.R;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

import java.util.ArrayList;
import java.util.List;

public class RetrofitFragment extends Fragment {
    private List<Chapter> chapterList = new ArrayList<>();
    private RecyclerView recyclerView;
    private ChaptersAdapter mAdapter;

    RestAdapter retrofit;
    WebService webService;
    SwipeRefreshLayout refreshLayout;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_retrofit, container, false);
    }

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
        recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        refreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeRefreshLayout);



        RecyclerView.LayoutManager mLayoutManager;
        int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

        //sprawdzanie rozmiaru ekranu
        switch (screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 1);
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 5);
                } else {
                    mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 4);
                }
                break;
            default:
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 4);
                } else {
                    mLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), 2);
                }
        }

        mAdapter = new ChaptersAdapter(chapterList, getContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        Log.i("aaaaaaaaaaaa", "startup");
        prepareData(true);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("aaaaaaaaaaaa", "onRefresh called from SwipeRefreshLayout");
                prepareData(true);
            }
        });
    }

    private void prepareData(final boolean refresh) {

        retrofit = new RestAdapter.Builder()
                .setEndpoint("http://bolex.cba.pl/")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();
        webService = retrofit.create(WebService.class);

        try {
            webService.getData(new Callback<Chapter[]>() {

                @Override
                public void success(Chapter[] data, Response response) {
                    if (refresh) {
                        chapterList.clear();
                    }

                    for (Chapter item : data) {
                        chapterList.add(item);
                    }
                    refreshLayout.setRefreshing(false);
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(getActivity().getApplicationContext(), error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    Log.d("Rest", error.getLocalizedMessage());
                }
            });
        } catch (Exception e) {
            Log.d("Rest", e.toString());
        }
        mAdapter.notifyDataSetChanged();
    }
}

适配器:

package com.example.bolek.testy.Adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import com.example.bolek.testy.Pojo.Chapter;
import com.example.bolek.testy.R;

public class ChaptersAdapter extends RecyclerView.Adapter<ChaptersAdapter.MyViewHolder> {
    private List<Chapter> chapterList;
    private Context mContext;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView date, number, title;
        private ImageView cover;

        public MyViewHolder(View view) {
            super(view);
            date = (TextView) view.findViewById(R.id.date);
            cover = (ImageView) view.findViewById(R.id.cover);
            number = (TextView) view.findViewById(R.id.number);
            title = (TextView) view.findViewById(R.id.title);
        }
    }

    public ChaptersAdapter(List<Chapter> chapterList, Context mContext) {
        this.chapterList = chapterList;
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chapter_list_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Chapter chapter = chapterList.get(position);
        holder.title.setText(chapter.getTitle());
        if(chapter.getDate() == null){
            Log.d("aaaaaaaaaaaaaaaaaaaaa","null");
        }
        Glide.with(mContext).load("https://novelki.pl/uploads/"+chapter.getImage())
                .thumbnail(0.5f)
                .error(R.drawable.no_foto)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.cover);
        holder.date.setText(chapter.getDate());
        holder.number.setText("Tom " + chapter.getTomNumber() + " chapter " + chapter.getChapterNumber());
    }

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

有什么想法吗?

P.S。 抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

我认为您可能在数据到来时没有更新适配器:

$('.imageholder').click(function() {
  var $desc = $(this).next();
  $('.description').not($desc).slideUp('fast');
  $(this).next().slideToggle("fast");
});