正如标题所示,我试图在列表视图中添加其他自定义行以允许更多自定义。我目前有一个自定义行设置到列表视图,据我们所知,将根据输入的信息量不断重复。既然我想扩展列表视图以显示多个自定义行,我就会陷入调整代码的行列,如果有人可以看看,我会很感激。
我已解释了我目前的设置。
首先,我们有视图持有者本身作为设置信息的中间人。
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,其中已经创建了getter和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 :(得分: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);