在listView中确定运行时的布局

时间:2013-04-20 14:11:48

标签: android

public AdapterClass(Context a, int resourceID, List < Info > entries) {
    super(a, resourceID, entries);
    this.layoutResource = resourceID;
}

@
Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Object custom = getItem(position);
    LinearLayout rowView = null;
    if (convertView == null) {
        rowView = new LinearLayout(getContext());
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(layoutResource, rowView);
    } else
        rowView = (LinearLayout) convertView;

    if (custom != null) {

    }
    return rowView;
}

我在运行时传递两个不同的布局,如何识别我在getView()方法中传递的布局。我正在使用ArrayAdapter

2 个答案:

答案 0 :(得分:1)

您可以使用布局ID来检查传递的布局

if(passedresourceID == R.layout.layout1)
{
    //do something
}
else if(passedresourceID == R.layout.layout2)
{
    //do something
}

答案 1 :(得分:1)

您应该能够检查传递给getView()

的父ID
public View getView(int position, View convertView, ViewGroup parent) {

switch (parent.getId()){   //check your ids here
case (R.layout.layoutToCheck):
break;
case (R.layout.otherLayoutToCheck):
...

final Object custom = getItem(position);
LinearLayout rowView = null;
if (convertView == null) {
    rowView = new LinearLayout(getContext());
    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    vi.inflate(layoutResource, rowView);