我有一个关于 GridView 实现的问题。在我的项目中创建的 GridView 是一个 3 X 3 的网格视图。每个网格由随机数组成。这个 gridview 不是一个完全成熟的 3 X 3 网格,而是 gridview 中只有 8 个网格。原因是我正在创建一个滑动游戏拼图,其中用户触摸空网格占位符旁边的网格并且触摸的网格移动到空网格占位符并且当前网格变为空。这为其他网格在被触摸时放置在这里提供了空间。请参阅滑动游戏
以下是此代码:
活动
public class SlidingGameActivity extends Activity {
GridView gridView;
GridAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_game);
gridView = (GridView)findViewById(R.id.gridView);
adapter = new GridAdapter(this);
gridView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sliding_game, menu);
return true;
} }
适配器
public class GridAdapter extends BaseAdapter {
Context context;
LayoutInflater mInflater;
int num ;
Integer[] numbers = {1,2,3,4,5,6,7,8} ;
public GridAdapter(Context context){
this.context = context;
mInflater = LayoutInflater.from(context);
numbers = storeRandomNumbers(8);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return numbers.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
GridDragListener dragListener = new GridDragListener();
GridTouchListener touchListener = new GridTouchListener();
if(convertView == null){
convertView = mInflater.inflate(R.layout.block, null);
holder = new ViewHolder();
holder.block = (RelativeLayout)convertView.findViewById(R.id.block);
holder.img = (ImageView)convertView.findViewById(R.id.blue_rect);
holder.textView = (TextView)convertView.findViewById(R.id.number);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.block.setOnTouchListener(touchListener);
holder.block.setOnDragListener(dragListener);
holder.img.setImageResource(R.drawable.blue_rectangle);
holder.textView.setText(String.valueOf(numbers[position]));
return convertView;
}
static class ViewHolder{
RelativeLayout block;
ImageView img;
TextView textView;
}
public ArrayList<Integer> randomize(int size){
Random random = new Random();
ArrayList<Integer> generated = new ArrayList<Integer>();
for(int i = 0; i < size; i++){
while(true){
Integer next = random.nextInt(size) + 1;
if(!generated.contains(next)){
generated.add(next);
break;
}
}
}
return generated;
}
public Integer[] storeRandomNumbers(int size){
ArrayList<Integer> list = randomize(size);
Collections.shuffle(list);
Integer[] nums = new Integer[size];
list.toArray(nums);
return nums;
}
}
触摸监听器
public class GridTouchListener implements View.OnTouchListener{
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
return true;
} else {
view.setVisibility(View.VISIBLE);
return false;
}
}
}
我的问题
1. 如何确定 gridview 中的空网格占位符?
2.如何将触摸到的网格放置在空网格占位符中?
请参考我提供的代码。如果有人实现了这个想法并有自己的代码或任何想法或建议,请提供。