使用Gridview的对话框有时无法加载

时间:2018-06-15 00:54:48

标签: android gridview dialog android-dialogfragment dialogfragment

我制作了一个使用GridView显示子类别的应用。 我在屏幕上显示了类别,单击时会出现一个对话框,其中显示子类别的GridView和用于查看该类别中所有内容的按钮。

问题在于: 有时GridView不会在第一次点击时加载,我必须关闭它然后再次单击该类别才能打开它。类别基本上是带有textview的图像。图片从互联网上加载。

对话代码:

public class dialogFragment extends android.support.v4.app.DialogFragment {

    private Button mViewAll;
    private GridView mSubCatsView;
    public ArrayList<category> mCats = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_fragment, container);
        mCats.addAll(((shopActivity)getActivity()).mDialogCats);
        mViewAll = v.findViewById(R.id.viewAllBTN);
        mSubCatsView = v.findViewById(R.id.subCatsView);
        final gridAdapter adapada = new gridAdapter(getActivity(), mCats);
        mSubCatsView.setAdapter(adapada);
        mSubCatsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ArrayList<category> subSubCats = new ArrayList<>();
                for (int y = 0; y < ((shopActivity)getActivity()).mCategoriesAll.size(); y++){
                    if(mCats.get(i).getId() == ((shopActivity)getActivity()).mCategoriesAll.get(y).getParent()){
                        subSubCats.add(((shopActivity)getActivity()).mCategoriesAll.get(y));
                    }
                }
                if(subSubCats.size() > 0){
                    mCats.clear();
                    mCats.addAll(subSubCats);
                    adapada.notifyDataSetChanged();
                }
            }
        });
        mViewAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

如果其他人有此问题,我可以使用RecyclerView + GridLayoutManager模仿GridView来解决。