1
我有我的片段類這樣。適配器不更新和更改recyclerview,notifyDataSetChanged不工作
@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是我用來打電話的排球方法。