我正在制作项目,其中我希望GridView中的项目必须像游戏2048一样可滑动。我现在使用GridView进行布局我希望它可以从上到下,从下到上,从右到左滑动从左到右我该怎么做才能变得滑动?
在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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score" />
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
在Activity类中:
public class MainActivity extends Activity implements OnItemClickListener,
OnDrawerOpenListener {
GridView gridView;
SlidingDrawer sd;
static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
// sd = (SlidingDrawer) findViewById(R.id.sDrawer1);
// sd.setOnDrawerOpenListener(this);
gridView = (GridView) findViewById(R.id.grid_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerOpened() {
// TODO Auto-generated method stub
}
}