Android 基本适配器,显示不同视图时如何使用convertView

Android 基本适配器,显示不同视图时如何使用convertView,android,Android,假设这是我的代码, 我想根据某些条件将两个不同的视图膨胀为一个listView public View getView(int position, View convertView, ViewGroup parent) { if (condition) { view = layoutInflater.inflate(R.layout.layout1, parent, false); } else { view = layoutInflater.in

假设这是我的代码, 我想根据某些条件将两个不同的视图膨胀为一个listView

public View getView(int position, View convertView, ViewGroup parent) {
    if (condition) {
        view = layoutInflater.inflate(R.layout.layout1, parent, false);
    } else {
        view = layoutInflater.inflate(R.layout.layout2, parent, false);
    }
    return view;
}

我想使用convertView来回收返回的视图,但是我的适配器如何知道要回收的是这两种类型中的哪一种?

您需要从接口实现另外两种方法:

/**
 * <p>
 * Returns the number of types of Views that will be created by
 * {@link #getView}. Each type represents a set of views that can be
 * converted in {@link #getView}. If the adapter always returns the same
 * type of View for all items, this method should return 1.
 * </p>
 * <p>
 * This method will only be called when when the adapter is set on the
 * the {@link AdapterView}.
 * </p>
 * 
 * @return The number of types of Views that will be created by this adapter
 */
int getViewTypeCount();
/**
 * Get the type of View that will be created by {@link #getView} for the specified item.
 * 
 * @param position The position of the item within the adapter's data set whose view type we
 *        want.
 * @return An integer representing the type of View. Two views should share the same type if one
 *         can be converted to the other in {@link #getView}. Note: Integers must be in the
 *         range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can
 *         also be returned.
 * @see #IGNORE_ITEM_VIEW_TYPE
 */
int getItemViewType(int position);
私有类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文、列表){
超级(上下文,0,列表);
}
@凌驾
public int getItemViewType(int位置){
返回位置%2;
}
@凌驾
public int getViewTypeCount(){
返回2;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
int type=getItemViewType(位置);
如果(类型==1){
视图=布局更平坦。充气(R.layout.layout1,父级,false);
}否则{
视图=布局更平坦。充气(R.layout.layout2,父级,false);
}
返回视图;
}
}
回答:

您应该将此方法添加到适配器中,这将导致适配器为当前索引提供正确的转换视图。 只要与正确的类型保持一致,返回的数字就没有关系

public int getItemViewType(int position) {
        int type;
        if (condition) {
            type = 1;
        } else {
            type = 2;
        }
        return type;
    }

您可以按以下方式解决此问题:

将两个视图添加为同一布局的子视图(item_example.xml):

 private class MyAdapter extends ArrayAdapter<String> {



    public MyAdapter(Context context, List<String> list) {
        super(context, 0, list);
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        int type = getItemViewType(position);
            if (type == 1){
                view = layoutInflater.inflate(R.layout.layout1, parent, false);
            } else {
                view = layoutInflater.inflate(R.layout.layout2, parent, false);
            }
        return view;
    }

}
public int getItemViewType(int position) {
        int type;
        if (condition) {
            type = 1;
        } else {
            type = 2;
        }
        return type;
    }
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:background="@color/white">
        <View
            android:id="@+id/v_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone">
        <View
            android:id="@+id/v_second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone">
</LinearLayout>
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View exampleView = convertView;

    if (exampleView == null) {
        LayoutInflater inflater = fragment.getActivity().getLayoutInflater();
        exampleView             = inflater.inflate(R.layout.item_example, parent, false);
    }

    View view1 = exampleView.findViewById(R.id.v_first);
    View view2 = exampleView.findViewById(R.id.v_second);

    if (/* ... show view1 ? ... */) {
        view1.setVisibility(View.VISIBLE);
        view2.setVisibility(View.GONE);
    } else {
        view2.setVisibility(View.VISIBLE);
        view1.setVisibility(View.GONE);
    }
    return exampleView;
}