如何使GridView可滑动?

时间:2014-05-01 09:49:30

标签: android gridview

我正在制作项目,其中我希望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

    }

}

1 个答案:

答案 0 :(得分:0)

我认为您希望GridView像滚动水平/垂直一样。那么在这种情况下,双向视图将对您有所帮助。哪个是Android GridView,可以配置为水平或垂直滚动​​。

查看Link 1Link 2,它以两种方式演示GridView。