2015-09-27 129 views
-1

我想構建一個可滾動的android列表視圖,其中每個行項目將分爲兩部分,即每個行項目將有兩個單元格/列。每列將顯示一個數據元素。Android列表視圖

我也想給用戶,設施添加數據元素到列表視圖中下一個可用的單元格/列。

因此,假設第一行的兩個單元格被填充,那麼在列表視圖的第二行的第一個單元格中將會有一個+符號。

單擊+時,列表視圖應向該單元格添加一個元素,並且+符號將切換到下一個單元格或下一行項目。

你能否建議如何構建這樣的視圖。我是否需要爲行項目編寫自定義視圖?

如何確定每個行項目的高度?

回答

0

當然你需要寫一個自定義視圖

而且在自定義視圖

你有三種可能性

View1 = One Column Data View and other Plus Button // wrap in LinearLayout 

View2 = One Column Data View and Other DataView 

View3 = Only Plus Button at Left 

基礎上,即使項目顯示1或3

if(items.getCount%2==0) 
{ 
//then plus button will be in first column 
set third Linear Layout Visible and other gone 
} 
else 
{ 
View1 Visible and other s gone 
} 
0

讓我們打破這一點。 首先你聲明你想建立一個Grid視圖。爲了簡單起見,網格視圖應該是activity.xml文件的根佈局。在這裏,您將使用android:numColumns屬性設置列數。這裏是什麼看起來像一個示例代碼:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/list" 
    android:numColumns="2" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.android.app_name.activity"/> 

接下來,您將需要一個.java文件打造的「源」,這將在此列表視圖。您可以通過使用ArrayList的<>在java文件來實現這個目的,找到該GridView的視圖組用下面的代碼

GridView gridview = (GridView) findViewById(R.id.list); 

現在你可以使用建立一個ArrayList源。

ArrayList<YourElement> array = new ArrayList<YourElement>(); 

將項目添加到這個數組列表中的使用。新增方法

array.add(); 

我們數組列表連接到網格視圖您將使用.setAdapter方法並傳入的名稱數組,在這種情況下是數組。

 gridview.setAdapter(array); 

這是盡我所能帶你。希望這可以幫助其他人