I have a custom horizontal grid implementation using
Android-DraggableGridViewPager
My flow is as follows
1.I set an initial adapter with an empty list.
2.I make a network call and update the same list and call notifyDataSetChanged() on that adapter
Fragment onCreateView code:
public class DashboardFragment extends Fragment {
private DashboardAppsFragmentBinding dashboardFragBindedContainerObj;
private List<App> appsList;
private DashboardAppGridAdapter adapterObj;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
dashboardFragBindedContainerObj = DataBindingUtil.inflate(
inflater, R.layout.dashboard_apps_fragment, container, false);
View view = dashboardFragBindedContainerObj.getRoot();
appsList = new ArrayList<>();
adapterObj = new DashboardAppGridAdapter(getActivity(), 0, appsList);
dashboardFragBindedContainerObj.appGridview.setColCount(3);
dashboardFragBindedContainerObj.appGridview.setRowCount(3);
dashboardFragBindedContainerObj.appGridview.setAdapter(adapterObj);
return view;
}
AdapterCode:
public class DashboardAppGridAdapter extends ArrayAdapter<App> {
private Context ctx;
private List<App> appList;
public DashboardAppGridAdapter(Context ctx, int resource, List<App> appList) {
super(ctx, 0, appList);
this.ctx = ctx;
this.appList = appList;
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
AppListingRowItemBinding appListingBindedContainerObj;
appListingBindedContainerObj =
DataBindingUtil.inflate(
LayoutInflater.from(getContext()),
R.layout.app_listing_row_item, parent, false);
convertView = appListingBindedContainerObj.getRoot();
if (appList != null && appList.size() > 0) {
appListingBindedContainerObj.lblAppTitle.setText(getItem(position).getDisplayName());
appListingBindedContainerObj.lblAppDesc.setText(getItem(position).getDisplayDescription());
}
return convertView;
}
/**
* Method to update adapter with new data
*
* @param appList
*/
public void refresh(List<App> appList) {
this.appList.clear();
this.appList.addAll(appList);
this.notifyDataSetChanged();
}
}
In my fragment when i get a network response, i call the refresh method in my adapter like so:
public void setupAppsGrid(List<App> appsToBeDisplayed) {
if (appsToBeDisplayed != null && appsToBeDisplayed.size() > 0) {
appsList.clear();
appsList.addAll(appsToBeDisplayed);
adapterObj.refresh(appsList);
dashboardFragBindedContainerObj.executePendingBindings();
}
}
When i debug i see the response coming in, the list containing the values but the view doesn't update.
If i reset the adapter, my grid is populated.
i.e
dashboardFragBindedContainerObj.appGridview.setAdapter(new DashboardAppGridAdapter(getActivity(), 0, appsToBeDisplayed));
every time, i have to update.