RecyclerView没有刷新onClick

时间:2017-06-16 14:49:34

标签: android android-recyclerview refresh

当我在主要活动中调用它时,RecyclerView正在刷新,但是当我使用FloatingActionButton它不再有效时,它没有给出任何错误。< / p>

onCreate

中的MainActivity方法
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initially get the events
    dh.getEvents();

    //Initially setting the list fragment
    EventListFragment eventListFragment = new EventListFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame, eventListFragment, "list_fragment");
    fragmentTransaction.commit();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dh.getEvents();
        }
    });
}

这是getEvents()

中的DataHandaling方法
public void getEvents() {

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(enviroClass.url() + "/api/events/" + 7)
            .build();

    client.newCall(request)
            .enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        JSONObject eventObject = new JSONObject(response.body().string());
                        JSONArray eventJsonArray = eventObject.getJSONArray("events");
                        for (int i = 0; i<eventJsonArray.length(); i++) {
                            eventObject = eventJsonArray.getJSONObject(i);
                            eventObject = eventObject.getJSONObject("event");
                            eventArray.add(new Event(eventObject.getString("name"), eventObject.getString("address"), eventObject.getString("image"), "100", eventObject.getString("description"), eventObject.getString("start_date"), eventObject.getString("end_date"), eventObject.getInt("id")));
                        }
                        EventListAdapter adapter = new EventListAdapter(eventArray, null);
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
}

更新

这是正在使用的RecyclerAdapter

public class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> {

    DataHandaling dh = new DataHandaling();

    private static ArrayList<Event> mValues;
    private OnListFragmentInteractionListener mListener = null;

    public EventListAdapter(ArrayList<Event> items, OnListFragmentInteractionListener listener) {
        if (items != null) {
            mValues = items;
        }
        if (listener != null ) {
            mListener = listener;
        }
    }

    public void swap(ArrayList<Event> eventArray) {
        mValues.clear();

        mValues.addAll(eventArray);
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText(mValues.get(position).getStarTime());
        holder.mContentView.setText(mValues.get(position).getName());
        Picasso.with(getApplicationContext()).load(mValues.get(position).getImage()).into(holder.mImageView);

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(position);
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public final ImageView mImageView;
        public Event mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.content);
            mImageView = (ImageView) view.findViewById(R.id.eventImage);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}

这是片段onCreateView,其中设置了适配器

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_list, container, false);

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }

        recyclerView.setAdapter(new EventListAdapter(dh.getEventArray(), mListener));
    }
    return view;
}

1 个答案:

答案 0 :(得分:0)

如果您创建新适配器,则需要在RecyclerView

中进行设置
 EventListAdapter adapter = new EventListAdapter(eventArray, null);
 myRecyclerView.setAdapter(adapter)

班级DataHandling应该可以访问RecyclerView

或者您可以使用之前创建的适配器:

在片段中,更改:

recyclerView.setAdapter(new EventListAdapter(dh.getEventArray(), mListener));

通过

mAdapter = new EventListAdapter(dh.getEventArray(), mListener)
recyclerView.setAdapter(mAdapter);

在getEvents()中:

mAdapter.swap(eventArray);

这意味着DataHandling类应该可以访问mAdapter,您可以在构造函数中传递它,或者您应该将getEvents()移动到Fragment并删除DataHandling