2015-10-24 5 views
2

Я планирую сделать приложение базовой сетки, в котором количество сеток изменится в соответствии с щелчком пользователя. сетка: - 2 * 2, 4 * 4 ... [например, пользователь нажмет на правильную сетку, тогда число сетки увеличится]. Я могу сделать сетку, но она не подходит для макета. так, как я могу изменить количество сетки на клике пользователя и как я могу установить размер сетки динамически.Android: создайте сетку с динамическим размером в gridlayout или gridview

Здесь я прилагаю изображение, которое я хочу достичь.

Когда пользователь нажмите на различной цветовой сетке, Количества сетки увеличится как второго изображения.

Image: 1

enter image description here

Изображение: 2

Количество сетки увеличилось по щелчку пользователя.

enter image description here

+0

hay @Ravi вы нашли ответ? – Error

+0

да я получил ответ @Error –

+0

мог бы дать мне ссылку для ответа или отправить ответ plz? – Error

ответ

4

Наконец я получил ответ на мой вопрос:

Здесь нужно взять SquareImageView для ImageView и программно установить столбец GridView, теперь смотрите ниже код:

SquareImageView.java

public class SquareImageView extends ImageView { 
public SquareImageView(Context context) { 
    super(context); 
} 

public SquareImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to 
                    // width 
} 
} 

Создать адаптер Custome для просмотра инфлятора,

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<com.example.SquareImageView 
    android:id="@+id/image_grid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</com.example.SquareImageView> 

</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity implements OnClickListener { 

int counter = 1; 

CustomGrid adapter; 
static int colum = 2; 

public static GridView gridview; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    adapter = new CustomGrid(MainActivity.this); 
    gridview = (GridView) findViewById(R.id.gridview); 

    gridview.setNumColumns(2); 

    gridview.setAdapter(adapter); 

} 

public class CustomGrid extends BaseAdapter { 
    private Context mContext; 
    CustomGrid adgg; 
    int mNumColumns = 2; 
    private Random rand = new Random(); 
    GridView gg; 
    int color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); 
    float alfa = 0.1f; 
    int rand_ind = rand.nextInt((2 * 2) - 0) + 0; 

    public CustomGrid(Context c) { 
     mContext = c; 
     // this.Imageid = Imageid; 
     // this.web = web; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return mNumColumns * mNumColumns; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View gridView; 

     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View grid = new View(mContext); 
     grid = inflater.inflate(R.layout.layout, null); 
     final View imageView = (ImageView) grid.findViewById(R.id.image_grid); 

     if (convertView == null) { 
      if (position == rand_ind) { 
       imageView.setBackgroundColor(getColorWithAlpha(color, alfa)); 
      } else { 
       imageView.setBackgroundColor(color); 
      } 
     } else { 
      grid = (View) convertView; 
     } 

     grid.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


       if (position == rand_ind) { 

        // Correct postion click time if condition occur.......... 


        if (counter == 2) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.1f; 
        } else if (counter == 6) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.08f; 
        } else if (counter == 8) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.1f; 
        } else if (counter == 12) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.1f; 
        } else if (counter == 14) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.1f; 
        } else if (counter == 16) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.1f; 
        } else if (counter == 18) { 
         mNumColumns = mNumColumns + 1; 
         alfa = alfa + 0.05f; 
        } else if (counter >= 18) { 
         // mNumColumns = mNumColumns + 1; 
         alfa = 0.8f; 

        } 

        gridview.setNumColumns(mNumColumns); 
        gridview.setAdapter(adapter); 
        adapter.notifyDataSetChanged(); 

        rand_ind = rand.nextInt((mNumColumns * mNumColumns) - 0) + 0; 

       } else { 

        // Here code for nagative click............ whem click 
        // nagative at time geanarate this code......... 
       } 
      } 
     }); 

     return grid; 
    } 

    public int getColorWithAlpha(int color, float ratio) { 
     int newColor = 0; 
     int alpha = Math.round(Color.alpha(color) * ratio); 
     int r = Color.red(color); 
     int g = Color.green(color); 
     int b = Color.blue(color); 
     newColor = Color.argb(alpha, r, g, b); 
     return newColor; 
    } 
} 

@Override 
public void onClick(View v) { 
} 
} 

Я надеюсь, что пригодился этот ответ полностью любому.

Смежные вопросы