2017-01-10 4 views
1

私は私がグリッドビューでアイテムを持っているが、私は、PIC内の1つのようなアイテムのサイズ/色をしたいけれども、このGridViewアイテムごとに異なる色、背景、高さ、幅などを設定する方法はありますか?

enter image description here

作るしようとしています。

は、ここで私はとても親切に私にそのレイアウトを構築するための手順を提供してください初心者です私のコード

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    // Constructor 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageButton imageView; 

     if (convertView == null) { 
      imageView = new ImageButton(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 

     } 
     else 
     { 
      imageView = (ImageButton) convertView; 
     } 
     imageView.setImageResource(mThumbIds[position]); 
     imageView.setBackgroundColor(mThumbIds[0].); 
     return imageView; 
    } 

    // Keep all Images in array 
    public Integer[] mThumbIds = { 
      R.drawable.town_hall1, 
      R.drawable.town_hall2, 
      R.drawable.town_hall3, 
      R.drawable.town_hall4, 
      R.drawable.town_hall5, 
      R.drawable.town_hall6, 
      R.drawable.town_hall7, 
      R.drawable.town_hall8, 
      R.drawable.town_hall9, 
      R.drawable.town_hall10, 
      R.drawable.town_hall11, 
    }; 
} 

です。

public int getRandomColor(int position) { 

     int[] colorArray = {Color.RED, Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY}; 

     if (position > colorArray.length) 
      position %= colorArray.length; 

     return colorArray[position]; 
    } 

としてそれを使用します:

imageView.setBackgroundColor(getRandomColor(position));

PS:どんなことなく、それを作った、あなたの意見に異なる背景色を取得したい場合は おかげ

+0

グリッドビューが邪魔になる場合http://www.technotalkative.com/lazy-productive-android-developer-part-6-staggered-gridview/ – Anjali

答えて

1

あなたはこのような何かを試すことができますエディタにはスペルミスが含まれている可能性があります。

3

あなたはmThumbIdsにダミーデータを持っているならば、あなたはこのようgetView()でスイッチケースを使用することができますが:

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageButton imageView; 

    if (convertView == null) { 
    switch(position){ 
    case 1: 
     imageView = new ImageButton(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(10, 10, 10, 10); 
     imageView.setImageResource(mThumbIds[1]); 
     imageView.setBackgroundColor(mThumbIds[0].); 
     break; 

    case 2: 
     imageView = new ImageButton(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(8, 8, 8, 8); 
     imageView.setImageResource(mThumbIds[2]); 
     imageView.setBackgroundColor(mThumbIds[0].); 
     break; 

    case 3: 
     imageView = new ImageButton(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(50, 50)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(5, 5, 5, 5) 
     imageView.setImageResource(mThumbIds[3]); 
     imageView.setBackgroundColor(mThumbIds[0].); 
     break; 
    } 
     } 
    else 
    { 
     imageView = (ImageButton) convertView; 
    } 

    return imageView; 
} 

あなたはあなたのイメージの数に等しい場合の数を取ることができます。異なる画像に異なるプロパティを設定しています。

2

GridViewを上記のサンプルのようにするには、RecyclerView.ItemDecorationを拡張するクラスを作成する必要があります。 getItemOffsets()を上書きしてください。

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { 

    private int spanCount; 
    private int spacing; 
    private boolean includeEdge; 

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { 
     this.spanCount = spanCount; 
     this.spacing = spacing; 
     this.includeEdge = includeEdge; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     int position = parent.getChildAdapterPosition(view); // item position 
     int column = position % spanCount; // item column 
     Log.d("position: : ", String.valueOf(Float.valueOf(position % spanCount))); 

     if (includeEdge) { 
      outRect.left = spacing - column * spacing/spanCount; // spacing - column * ((1f/spanCount) * spacing) 
      outRect.right = (column + 1) * spacing/spanCount; // (column + 1) * ((1f/spanCount) * spacing) 
      Log.d("spacing: ", String.valueOf(spacing)); 
      Log.d("spacingL: ", String.valueOf(outRect.left)); 

      if (position < spanCount) { // top edge 
       outRect.top = spacing; 
      } 
      outRect.bottom = spacing; // item bottom 
     } else { 
      outRect.left = column * spacing/spanCount; // column * ((1f/spanCount) * spacing) 
      outRect.right = spacing - (column + 1) * spacing/spanCount; // spacing - (column + 1) * ((1f/ spanCount) * spacing) 
      if (position >= spanCount) { 
       outRect.top = spacing; // item top 
      } 
     } 
    } 
} 

そして、単にパラメータとしてGridSpacingItemDecorationクラスの新しいインスタンスを渡すRecyclerViewaddItemDecoration()メソッドを呼び出します。

関連する問題