android中的一个scrollview中的多个gridView

时间:2015-04-21 05:49:39

标签: android gridview scrollview

我尝试在scrollview中实现多个gridView,以便所有girdview只有一个滚动条。 我用了两个gridView。 我的布局是这样的:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

<com.example.ExpandableHeightGridView
    android:id="@+id/gridView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />


    <com.example.ExpandableHeightGridView
    android:id="@+id/gridView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="1"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

 </LinearLayout>

</ScrollView>

我将This like用于“ExpandableHeightGridView”

在主要活动中我使用了这个:

    final ExpandableHeightGridView gView1 = new ExpandableHeightGridView(this);
gView1.setNumColumns(2);


ArrayList<HashMap<String, String>> list_of_wordmeanings = new ArrayList<HashMap<String, String>>();

Cursor mCursor = mDbHelper.getWord(value_of_category_id);
for (int i = 0; i < mCursor.getCount(); i++)
{
    mCursor.moveToPosition(i);

    HashMap<String, String> hm = new HashMap<String, String>();   
        String column1data = mCursor.getString(0).toString();
        String column2data = mCursor.getString(1).toString();
        String column3data = mCursor.getString(2).toString();
             hm.put("key_word",column1data);
             hm.put("key_meaning",column2data);  
             hm.put("key_translitaration",column3data); 
        list_of_wordmeanings.add(hm);
}

   // Keys used in Hashmap
        String[] from = { "key_word","key_meaning","key_translitaration" };
        int[] to = { R.id.txt1,R.id.txt2,R.id.txt3};     // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_wordmeanings, R.layout.grid_layout, from, to);
     // Setting the adapter to the listView
        gView1.setAdapter(adapter);
        gView1.setExpanded(true);


        gView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // TODO Auto-generated method stub


                HashMap<String, String> hash = new HashMap<String, String>();
                hash=(HashMap<String, String>) gView1.getItemAtPosition(position);

                String english_word=hash.get("key_word");
                String native_word=hash.get("key_meaning");

                 // Show Alert 
                Toast.makeText(getApplicationContext(),
                 "  gridViewValue : "+native_word , Toast.LENGTH_LONG)
                  .show();

            }
        });

它无法正常工作。

1 个答案:

答案 0 :(得分:0)

GridView2中,您将列数限制为1。检查一下。

GridView的目的是实现多个列。