我的项目包含显示图像网格的片段。我知道要显示所有的图像,但我只想显示几张图像,怎么样?
问题描述:
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中的代码以仅显示我想要显示的图像。我的项目包含显示图像网格的片段。我知道要显示所有的图像,但我只想显示几张图像,怎么样?
答
只需创建一个过滤列表?创建一个新的ArrayList
(List<>
),并将原始内容中的项目添加到您要保留的for循环中。如果您的列表不断变化,请在自定义Adapter
中执行此操作,以免始终重新创建Adapter
。
或者只是在这里把你的列表要显示图像:
for (int i = 0; i < allImages.length(); i++) {
if (iWantToDisplayImage(i)) {
allDrawableImages.add(allImages.getDrawable(i));
}
}
哦,这是伟大的,因为我是新来的编码我是至少转化成逻辑代码。你可以告诉我如何做ArrayList。 – Rajashekar
检查编辑,我不知道根据你想要显示的图像,但只是在你的循环条件。 – etan