适配器不更新和更改recyclerview,notifyDataSetChanged不工作

时间:2016-07-22 14:33:09

标签: java android android-fragments android-recyclerview android-adapter

我的片段类就像这样。

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

    View rootView = inflater.inflate(R.layout.fragment_facebookfeeds,container,false);
    mLayoutManager = new LinearLayoutManager(getActivity());


    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view_facebook);
    mRecyclerView.setHasFixedSize(true);


    GetTimeline(TimelineUrl, next, new ServerCallback() {
        @Override
        public void onSuccess(Facebook result) {
            next = result.getNext();
            List<Facebook> newList = new ArrayList<>();
            newList.add(result);
            facebookList.clear();
            facebookList.addAll(newList);
            mAdapter = new FacebookAdapter(facebookList,getActivity());
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
            mRecyclerView.setAdapter(mAdapter);
            //mAdapter.notifyDataSetChanged();

        }
    });




    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            visibleItemCount = mRecyclerView.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loading && (totalItemCount - visibleItemCount)
                    <= (firstVisibleItem + visibleThreshold)) {
                GetTimeline(TimelineUrl, next, new ServerCallback() {
                    @Override
                    public void onSuccess(Facebook result) {
                        next = result.getNext();
                        List<Facebook> newFacebook = new ArrayList<Facebook>();
                        newFacebook.add(result);
                        facebookList.clear();
                        facebookList.addAll(newFacebook);
                        mAdapter.notifyDataSetChanged();
                        System.out.println("New Adapter Item Count "+mAdapter.getItemCount());
                    }
                });
                loading = true;
            }
        }
    });

    // Inflate the layout for this fragment
    return rootView;
}

我的适配器是这样的。

private List<Facebook> facebook;
private List<String> feed = new ArrayList<>();
private List<String> feedImage = new ArrayList<>();
int sum;
private String image;
private String user;
private Context context;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView fbfeed;
    public ImageView fbImage;
    public ViewHolder(View view) {
        super(view);
        fbfeed = (TextView) view.findViewById(R.id.fbfeed);
        fbImage = (ImageView) view.findViewById(R.id.fbprofile_url);

    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public FacebookAdapter(List<Facebook> facebook,Context context) {

    this.facebook = facebook;
    this.context = context;
    notifyDataSetChanged();
    String singleTweet = "";
    String singleImage;
        System.out.println("Coming inside facebook adapter");
        for(int i=0;i<facebook.size();i++){
            for(int j=0;j<facebook.get(i).getFeeds().size();j++){
                singleTweet  = facebook.get(i).getFeeds().get(j).getStatus();
                feed.add(singleTweet);
                singleImage = facebook.get(i).getFeeds().get(j).getImage();
                if(singleImage!=null){
                    feedImage.add(singleImage);
                }
                else{
                    feedImage.add("No Image");
                }

            }
      }
        //new stuff
        sum = 0;
        for(int i=0;i<facebook.size();i++){
            sum = sum + facebook.get(i).getFeeds().size();
        }

}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
                                     int viewType) {
    // create a new view
    View itemview = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fbfeed_row, parent, false);
    // set the view's size, margins, paddings and layout parameters
    return new ViewHolder(itemview);
}

不确定问题是什么,我已经阅读了很多答案尝试了许多事情,但似乎没有任何帮助。
GetTimeline是我用来打电话的Volley方法。

1 个答案:

答案 0 :(得分:1)

好的我修好了。 我只是将适配器的构造函数中编写的逻辑移动到片段类,并将数据传递给适配器。最后,adapter.notifyDataSetChanged()工作了!