【问题标题】:ListView Header implementationListView 标头实现
【发布时间】:2014-04-20 08:00:49
【问题描述】:

我有一个适配器类,它使用标题实现 ListView。

//Adapter Class
private class MyCustomAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private static final int TYPE_GRAY_SEPARATOR = 2;
private TreeSet<Integer> mGraySeparatorsSet = new TreeSet<Integer>();

private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter(Context context)
{
    mInflater = LayoutInflater.from(context); 
}

public void addItem(ContentWrapper value) {
    mData.add(value);
    notifyDataSetChanged();
}

public void addSeparatorItem(ContentWrapper value) {
    mData.add(value);
    // save separator position
    mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}


public ContentWrapper getItem(int position) {
    return mData.get(position);
}
@Override
public int getItemViewType(int position) {
    return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

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

public int getCount() {
    return mData.size();
}

public long getItemId(int position) {
    Log.v("getItemId Position", ""+position);
    return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    int type = getItemViewType(position);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
        case TYPE_ITEM:
            convertView = mInflater.inflate(R.layout.white, null);
            holder.textView = (TextView)convertView.findViewById(R.id.text);
            break;
        case TYPE_SEPARATOR:
            convertView = mInflater.inflate(R.layout.black, null);
            holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
            break;

        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    } holder.textView.setText(mData.get(position).getItem());

    if (type == TYPE_ITEM) {
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder x = new AlertDialog.Builder(getActivity());
                       x.setIcon(R.drawable.ic_launcher)

                        .setTitle(mData.get(position).getItem())

                        .setMessage(mData.get(position).getItemDescription())

                        .setCancelable(true)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg,
                                            int arg1) {
                                    }
                                });
                 AlertDialog a = x.create();
                 a.show();
             }
                return null;
                }


            });
    } else {
        holder.textView.setOnClickListener(null);
    }

return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}

在此我真正理解了getView方法(ViewHolder、convertView等)的实现。

上面的代码运行流畅,但我不明白我们为什么要使用这些方法

  • 添加项目
  • 添加SeparatorItem
  • getItemViewType
  • getViewTypeCount
  • getCount
  • getItemId

谁能给我解释清楚这个场景!

提前谢谢..

【问题讨论】:

    标签: android listview android-listview baseadapter


    【解决方案1】:

    这些都是回调方法。您可以在下面提到的链接中明确定义这些方法。

    http://developer.android.com/reference/android/widget/BaseAdapter.html

    让我给你一个 getItem() 的例子

    每当创建新的 listview 行时,adapter 都会从这个回调方法中获取项目的详细信息。

    类似地,每当创建列表时,都会调用 getCount()。

    假设您在列表视图中有 12 个项目。但是你在 getCount() 中传递了 10。它只会在列表视图中显示十个项目。

    【讨论】:

    • 我不明白他们的观点,他们也没有很好的解释
    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多