Android - 动态gridview(多列绑定行)

时间:2012-11-21 15:20:40

标签: java android data-binding gridview dynamic

我想在Android中实现gridview,它基本上由4列组成(它将与表格类似)。

这基本上是分布在4列(name,qty,p1,p2)中的项目列表。因此每行包含4列,对1个项目是唯一的。因此,我需要一种方法将这4列绑定在一起,这样当我选择这4列中的任何位置时,将选择整行,同时返回'name'列作为文本ID。

有没有一种简单的方法可以做到这一点?这是我当前的gridView代码:

XML布局:

<GridView
    android:id="@+id/gvShopCompleteList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/spnShopCharacters"
    android:layout_above="@+id/linerBuyBtns"
    android:numColumns="1"
    android:fastScrollEnabled="true"
    android:scrollbarStyle="insideOverlay">
</GridView>

动态内容的绑定:

GridView gridview;
static final String[] listItems = new String[] { "name","qty","p1","p2","itemname","5","100","1" };
gridview = (GridView)findViewById(R.id.gvShopCompleteList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
gridview.setAdapter(adapter);

gridview.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View v,int position, long id) 
    {
        Toast.makeText(getApplicationContext(),
        ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
    }
});

最后,我想添加的另一个功能是使标题只读,并突出显示与其他行不同的颜色。任何帮助将在这里非常感激,我不必使用GridView,但希望尽可能保持通用的android外观(在我的应用程序中一致)。感谢。

1 个答案:

答案 0 :(得分:2)

我做了一些样品。以下是行R.layout.adapters_tablelikelistview

的布局文件
<?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="horizontal" >

    <TextView
        android:id="@+id/data1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/data2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/data3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/data4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1" />

</LinearLayout>

以及使用上述布局的Activity和适配器:

public class TableLikeListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        String[] data1 = new String[] { "Header1", "data1", "data2" };
        String[] data2 = new String[] { "Header2", "data1", "data2" };
        String[] data3 = new String[] { "Header3", "data1", "data2" };
        String[] data4 = new String[] { "Header4", "data1", "data2" };
        setListAdapter(new MyAdapter(this, R.layout.adapters_tablelikelistview,
                R.id.data1, data1, data2, data3, data4));
    }

    private static class MyAdapter extends ArrayAdapter<String> {

        private String[] data1, data2, data3, data4;

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] data1, String[] data2, String[] data3, String[] data4) {
            super(context, resource, textViewResourceId, data1);
            this.data1 = data1;
            this.data2 = data2;
            this.data3 = data3;
            this.data4 = data4;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if (position == 0) {
                // header
                v.setBackgroundColor(Color.GREEN);
            } else {
                v.setBackgroundResource(android.R.drawable.list_selector_background);
            }
            TextView t1 = (TextView) v.findViewById(R.id.data1);
            t1.setText(data1[position]);
            TextView t2 = (TextView) v.findViewById(R.id.data2);
            t2.setText(data2[position]);
            TextView t3 = (TextView) v.findViewById(R.id.data3);
            t3.setText(data3[position]);
            TextView t4 = (TextView) v.findViewById(R.id.data4);
            t4.setText(data4[position]);
            return v;
        }

        @Override
        public boolean isEnabled(int position) {        
            return position == 0 ? false : true;
        }       

    }

}