0
我有一個listview和一個數組適配器。我想傳遞一個數組並創建一個列表。到現在爲止還挺好。現在我想要做的下一件事是在已經創建的列表下追加另一個數組資源。我不能將兩個陣列放在一起,因爲這兩個陣列應該有不同的佈局。有沒有辦法做到這一點?我可以在單個listview上添加不同的佈局嗎?
我有一個listview和一個數組適配器。我想傳遞一個數組並創建一個列表。到現在爲止還挺好。現在我想要做的下一件事是在已經創建的列表下追加另一個數組資源。我不能將兩個陣列放在一起,因爲這兩個陣列應該有不同的佈局。有沒有辦法做到這一點?我可以在單個listview上添加不同的佈局嗎?
是你可以
首先你需要擴展ArrayAdapter一個新的類,並重寫getview方法
class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context,ArrayList<MyClass> myarray) {
super(context,R.layout.yourlayout,myarray);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*this is also called when creating for the first time or once any view (row) is visible again */
View view=null;
MyClass object=(MyClass)getItem(position);
//if true than choose the first layout
if (object.layout1 == true) {
view=LayoutInflater.from(getContext()).inflate(R.layout.yourlayout1,
parent, false);
}else if(object.layout2==true){
view=LayoutInflater.from(getContext()).inflate(R.layout.yourlayout2,
parent, false);
}
}
,現在你應該有例如稱爲MyClass的一類含有
public class MyClass{
public boolean layout1;
public boolean layout2;
}
所以這樣,當你添加項目到適配器添加一個MyClass的對象,你所要做的就是將布爾值layout1和layout2設置爲真的還是假的知道你想選擇
,比加入新的項目到adpater要它來選擇佈局2的一個例子是佈局:
MyClass object=new MyClass();
object.layout1=false;
object.layout2=true;
//setting listview adpater
MyListView.setAdapter(new MyAdapter(this,new ArrayList<MyClass>()));
Myadapter.add((Object)object);