2017-04-04 87 views
0
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中的代碼以僅顯示我想要顯示的圖像。我的項目包含顯示圖像網格的片段。我知道要顯示所有的圖像,但我只想顯示幾張圖像,怎麼樣?

回答

0

只需創建一個過濾列表?創建一個新的ArrayListList<>),並將原始內容中的項目添加到您要保留的for循環中。如果您的列表不斷變化,請在自定義Adapter中執行此操作,以免始終重新創建Adapter

或者只是在這裏把你的列表要顯示圖像:

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

哦,這是偉大的,因爲我是新來的編碼我是至少轉化成邏輯代碼。你可以告訴我如何做ArrayList。 – Rajashekar

+0

檢查編輯,我不知道根據你想要顯示的圖像,但只是在你的循環條件。 – etan