为什么会发生这种错误,相同的代码但不同的片段,Recyclerview,Retrofit2

时间:2016-09-19 09:50:21

标签: android android-fragments

我在MainActivity上有两个片段。

首先,YellowFragment

public class YellowFragment extends Fragment implements FlowerAdapter.FlowerClickListener{

    private SwipeRefreshLayout swipeContainer_yellow;

    private RecyclerView mRecyclerView;

    private RestManager mRestManager;

    private FlowerAdapter mFlowerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_yellow, container, false);

        swipeContainer_yellow = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_yellow);

        swipeContainer_yellow.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                mRestManager = new RestManager();
                Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers();
                listCall.enqueue(new Callback<List<Flower>>() {
                    @Override
                    public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {

                        if (response.isSuccessful()) {
                            mFlowerAdapter.clear();

                            List<Flower> flowerList = response.body();
                            for(int i =0; i<flowerList.size(); i++) {
                                Flower flower = flowerList.get(i);
                                mFlowerAdapter.addFlower(flower);
                            }
                            swipeContainer_yellow.setRefreshing(false);
                        }
                    }
                    @Override
                    public void onFailure(Call<List<Flower>> call, Throwable t) {
                    }
                });
            }
        });
        configViews(view);
        return view;
    }
    private void configViews(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_yellow);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, false));
        mFlowerAdapter = new FlowerAdapter(this);
        mRecyclerView.setAdapter(mFlowerAdapter);
    }
}

其次,RedFragment (它与YellowFragment完全相同,但swipeContainer_yellow, rv_flower_yellowLinearLayoutManager更改为swipeContainer_red, rv_flower_redGridLayoutManager

public class RedFragment extends Fragment {

    private SwipeRefreshLayout swipeContainer_red;
    private RecyclerView mRecyclerView;
    private RestManager mRestManager;
    private FlowerAdapter mFlowerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_red, null);
        swipeContainer_red = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_red);
        swipeContainer_red.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                mRestManager = new RestManager();
                Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers();
                listCall.enqueue(new Callback<List<Flower>>() {
                    @Override
                    public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
                        if (response.isSuccessful()) {
                            mFlowerAdapter.clear();
                            List<Flower> flowerList = response.body();
                            for(int i =0; i<flowerList.size(); i++) {
                                Flower flower = flowerList.get(i);
                                mFlowerAdapter.addFlower(flower);
                            }
                            swipeContainer_red.setRefreshing(false);
                        }
                    }
                    @Override
                    public void onFailure(Call<List<Flower>> call, Throwable t) {
                    }
                });

            }
        });
        configViews(view);

        return view;
    }

    private void configViews(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_red);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), 3));
        mFlowerAdapter = new FlowerAdapter(this); #Here is problem
        mRecyclerView.setAdapter(mFlowerAdapter);
    }
}

但是在RedFragment中,

enter image description here

为什么会这样?

FlowerAdpater的某些部分

public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.Holder> {
    public FlowerAdapter(FlowerClickListener listener) {
        mFlowers = new ArrayList<>();
        mListener = listener;
    }
    public interface FlowerClickListener {
        void onClick(int position);
    }
}

我首先尝试在不同的视图(Gridlayout和Linearlayout)上创建两个具有相同数据的片段(来自我的RESTapi服务器,使用retrofit2)。

但我放弃了。因为对我来说似乎太难了。

如果有一个很好的方式依赖我的第一次尝试,我会非常感谢。

无论如何,为什么会发生错误?

请让我知道提示。

2 个答案:

答案 0 :(得分:2)

你必须在 RedFragment 片段中实现 FlowerClickListener 界面,如

public class RedFragment extends Fragment implements FlowerAdapter.FlowerClickListener{

答案 1 :(得分:2)

  

无论如何,为什么会发生错误?

因为this不是FlowerClickListener。如果要将此作为委托使用,则Fragment的子类应实现FlowerClickListener接口。

  public class RedFragment extends Fragment implements FlowerClickListener {