我的项目包含显示图像网格的片段。我知道要显示所有图像,但我想只显示几张图像,怎么样?

时间:2017-04-04 12:31:57

标签: android gridview

1. 
public class FragmentOne extends Fragment {

    // Displaying Grid of Images in GridView.
    private GridView gridView;
    // Using ArrayList for drawable Images.
    ArrayList<Drawable> allDrawableImages = new ArrayList<>();
    private TypedArray allImages;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // Return the rootView of Grid Images
        View rootView = inflater.inflate(R.layout.fragment_one, null);
        getAllWidgets(rootView);
        setAdapter();
        return rootView;
    }

    private void getAllWidgets(View view) {
        gridView = (GridView) view.findViewById(R.id.gridViewFragmentOne);
        allImages = getResources().obtainTypedArray(R.array.all_images);
    }

    private void setAdapter()
    {
        // Displaying Images from Drawable folder.
        // Adding the Images to Grid from Drawable folder
        for (int i = 0; i < allImages.length(); i++) {
            allDrawableImages.add(allImages.getDrawable(i));
        }

        // Drawing Images using ImageAdapter into activity
        // Displaying Images into GridView.
        ImageAdapter imageAdapter = new ImageAdapter(MainActivity.getInstance(), allDrawableImages);
        gridView.setAdapter(imageAdapter);
    } 
}

//这是代码,如何编辑ArrayList中的代码,只显示我想要显示的图像。

1 个答案:

答案 0 :(得分:0)

只需创建一个过滤列表?创建一个新的ArrayListList<>),并将原始文章中的项目添加到您要保留的for循环中。如果您的列表不断变化,请在自定义Adapter中执行此操作,以避免一直重新创建Adapter

或者只是在这里放入您想要显示的图像:

for (int i = 0; i < allImages.length(); i++) {
    if (iWantToDisplayImage(i)) {
        allDrawableImages.add(allImages.getDrawable(i));
    }
}