notifyDataSetChanged()发出自定义ArrayAdapter

时间:2018-02-22 23:37:25

标签: android android-arrayadapter notifydatasetchanged

我使用

进行自定义水平网格实现

Android-DraggableGridViewPager

我的流程如下

1.我设置了一个带有空列表的初始适配器。

2.我进行网络呼叫并更新相同的列表并在该适配器上调用notifyDataSetChanged()

碎片onCreateView代码:

    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();
    }

}

在我的片段中,当我收到网络响应时,我会调用我的适配器中的刷新方法,如下所示:

public void setupAppsGrid(List<App> appsToBeDisplayed) {
            if (appsToBeDisplayed != null && appsToBeDisplayed.size() > 0) {  
                appsList.clear();           
                appsList.addAll(appsToBeDisplayed);
                adapterObj.refresh(appsList);
                dashboardFragBindedContainerObj.executePendingBindings();   
                }        
            }

当我调试我看到响应进来时,包含值但视图的列表不会更新。

如果我重置适配器,我的网格就会被填充。

 dashboardFragBindedContainerObj.appGridview.setAdapter(new DashboardAppGridAdapter(getActivity(), 0, appsToBeDisplayed));

每一次,我都要更新。

0 个答案:

没有答案