2017-02-17 80 views
0

正如標題所示,我正試圖在列表視圖中添加一個額外的自定義行以允許更多的自定義。我目前有一個自定義行設置爲列表視圖,正如我們所知,它將不斷重複,取決於輸入的信息量。現在我想擴展列表視圖來顯示多個自定義行,我已經陷入調整我的代碼,並希望如果有人可以看看。如何添加一個額外的自定義行到列表視圖?

我提供了我當前設置的解釋。

首先,我們擁有視圖持有者本身,它可以在中間設置信息。

public class ViewHolderWine { 

public ImageView wineImage; 
public TextView wineName; 
public TextView wineDes; 
public TextView winePrice; 


public void setDataIntoViewHolder (List_String listString){ 

    wineImage.setImageResource(listString.getIcon_wine()); 
    wineName.setText(listString.getWineName()); 
    wineDes.setText(listString.getWinedes()); 
    winePrice.setText(listString.getWinePrices()); 

} 

其次,我們有我的List_String,在其中獲取getters和setter。

public class List_String { 

int icon_wine; 
String wineName; 
String winedes; 
String winePrices; 


public List_String(int icon_wine, String wineName, String winedes, String winePrices) { 
    this.icon_wine = icon_wine; 
    this.wineName = wineName; 
    this.winedes = winedes; 
    this.winePrices = winePrices; 

} 

public String getWinePrices() { 
    return winePrices; 
} 

public void setWinePrices(String winePrices) { 
    this.winePrices = winePrices; 
} 

etc...... 

接下來我們有ListFragment

public class List extends ListFragment { 

java.util.List<List_String> lst; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    listData(); 
    // Adapter custom; 


    Adapter myAdapter = new Adapter(getActivity(), R.layout.custom_list, lst); 

    setListAdapter(myAdapter); 

    return super.onCreateView(inflater, container, savedInstanceState); 

} 


public void listData() { 

    lst = new ArrayList<List_String>(); 

    lst.add(new List_String(R.drawable.thumb, etc.... 

最後,我再把所有的拼在一起使用適配器。

public class Adapter extends ArrayAdapter<List_String> { 

List<List_String> lstWine; 
LayoutInflater inflater; 

public Adapter(Context context, int resource, java.util.List<List_String> objects) { 
    super(context, resource, objects); 


    lstWine = objects; 
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 

    ViewHolderWine viewholder; 


    if (view == null){ 

     viewholder = new ViewHolderWine(); 


       view = inflater.inflate(R.layout.custom_list, null); 

       viewholder.wineImage = (ImageView) view.findViewById(R.id.wineImage); 
       viewholder.wineName = (TextView) view.findViewById(R.id.wineName); 
       viewholder.wineDes = (TextView) view.findViewById(R.id.wineDes); 
       viewholder.winePrice = (TextView) view.findViewById(R.id.winePrice); 



     view.setTag(viewholder); 

    }else{ 

     viewholder = (ViewHolderWine) view.getTag(); 

    } 

    List_String list = lstWine.get(position); 
    viewholder.setDataIntoViewHolder(list); 


    return view; 

} 
+0

嗎?或者你只是想要一些底部的行有不同的外觀? – kampangala

+0

基本上有些行有不同的外觀,以便我可以混合搭配。 – Steven

+0

它們是否包含相同類型的數據? – kampangala

回答

0

爲承載列表視圖活性的佈局是這樣的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="1"> 

    <ListView 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </ListView> 
    <ListView 
      android:id="@+id/list2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </ListView> 
</LinearLayout> 

在您的活動類,你想使可擴展列表

//populate your first list 
    myList.add(0, new Model("Full name 1", "Good")); 
    myList.add(1, new Model("Full name 2", "Bad")); 
    myList.add(2, new Model("Full name 3", "Worse")); 
    myList.add(3, new Model("Full name 4", "Mediocre")); 

//populate your second list 
    myList2.add(0, new Model2("Full name 5", true)); 
    myList2.add(1, new Model2("Full name 6", true)); 
    myList2.add(2, new Model2("Full name 7", false)); 

//initialize adapters 
    adapter = new mAdapter(StartActivity.this,myList); 
    adapter2 = new mAdapter2(StartActivity.this,myList2); 

//retrieve the list views 
    lv= (ListView) findViewById(R.id.list); 
    lv2= (ListView) findViewById(R.id.list2); 

//set the adapters 
    lv.setAdapter(adapter); 
    lv2.setAdapter(adapter2); 
相關問題