我有一个家庭活动和3个子片段,在家庭活动中我调用了api然后我想将结果更新到片段中包含的recycleler视图。现在除了notifydatasetchanged()之外的所有东西都运行得很好。
你对片段有任何想法请帮助我。
HomeActivity.java
// member variable for the first fragment
private FeedsFragment feedsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// initialised in oncreate
feedsFragment = new FeedsFragment();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(feedsFragment, "FEEDS");
}
// this is what i am called when get the api result
feedsFragment.setUpdateArrayList(updateArrayList);
FeedsFragment.java
public class FeedsFragment extends Fragment {
private RecyclerView recyclerView;
private UpdateListAdapter uAdapter;
private ArrayList<Updates> updateArrayList;
private String TAG = UpdatesActivity.class.getSimpleName();
public FeedsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
updateArrayList = new ArrayList<Updates>();
View thisView = inflater.inflate(R.layout.activity_updates, container, false);
recyclerView = (RecyclerView) thisView.findViewById(R.id.recycler_view_updates);
uAdapter = new UpdateListAdapter(getActivity(), updateArrayList, new UpdateListAdapter.UpdatesActionListener() {
@Override
public void onShareButtonTapped(int position) {
}
@Override
public void onOptionsButtonTapped(int position, View v) {
}
@Override
public void onProfileButtonTapped(int position) {
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(uAdapter);
return thisView;
}
public void setUpdateArrayList(ArrayList<Updates> array){
this.updateArrayList = array;
this.recyclerView.getAdapter().notifyDataSetChanged();
//Also tried
// uAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:1)
问题是您更改了片段中的updateArrayList
而不是适配器中的列表。
您可以在UpdateListAdapter
中添加api作为
public void setData(List<Updates> data){...}
然后在致电setUpdateArrayList
之前在notifyDataSetChanged
中拨打该api。
答案 1 :(得分:0)
感谢您支持我,通过重新初始化upadatelistadapter暂时解决问题
public void setUpdateArrayList(ArrayList<Updates> array){
this.updateArrayList = array;
********** <Reinitialized the adapter here >**********
this.recyclerView.getAdapter().notifyDataSetChanged();
}