1

我正在尝试为我的应用程序构建一个自定义适配器,它的主屏幕是一个ListActivityListView中心的。我对如何创建适配器一无所知,所以我想知道是否有人知道任何好的教程来引导我完成它。

4

2 回答 2

2

我记得学习 ArrayAdapters 并不容易。网上有很多教程。

扩展 ArrayAdapter 您可能想要覆盖方法。

getCount() - 列表中的项目数。

public int getViewTypeCount() - ArrayList 呈现数据的方式数。通常这是 1。但如果您的列表中有多种类型的数据,并且有多个视图,那么您将不得不更改此数字。一个例子是联系人列表。它有 2 种类型。1 代表联系人本身,另一个是您看到的 A、B 等字母类别。

getItemViewType(position) - 对于职位,它应该得到什么类型?通常, 1. 除非您有多种类型。

public long getItemId(int position) - 您要展示的项目类型。

真正重要的一个! public View getView(int position, View convertView, ViewGroup parent)

  • 位置显然是列表中的当前项目。
  • convertView 用于视图回收。这意味着如果一个项目滚动离开屏幕,它的视图将被保留并等待另一个将出现在屏幕上的项目重用。这有助于提高性能,因为您最多只能在屏幕上看到大约 8 个相同的视图。最初 convertView 将为空,因为它不能重用任何东西,但当用户在屏幕上滚动时,你会得到它们。ViewGroup parent 除了在视图膨胀中使用它之外,我不太确定。

下面是一个使用 ViewHolder 模式的 getView 示例。ViewHolder 有助于使您的滚动流畅,因为您不必一遍又一遍地重做所有 findViewById 的东西。每次使用新信息更新视图时,请使用 holder.WidgetName 来执行此操作。

/* Hypotetical list of names. */
@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        ListItemWithSideButton view;

        if(convertView == null) {
            view = (ListItemWithSideButton)_inflater.inflate(R.layout.some_line_item, parent, false);
            holder = new ViewHolder();
            view.setTag(holder);

            holder.SomeButton = view.findViewById(R.id.some_button);
            /* Other views. */
        }
        else {
            view = (ListItemWithSideButton) convertView;
            holder = (ViewHolder) convertView.getTag();
        }

            /* Get the item. */
        final String name = getItem(position);

            /* Update the data of the view with the new information. */
        holder.SomeButton.setText(name);

        return view;
    }

    static class ViewHolder {
    Button SomeButton;
    /* Other views. */
  }

}

于 2010-12-06T06:14:23.677 回答
1

现在我没有任何参考可以给你,但这是你可能正在做的事情以获得你想要的东西:

您可能在 XML 中有 ListView,因此在您的代码中实例化一个 ListView 对象:

ListView myList = (ListView)findViewById(R.id.myListView)

获得对它的引用后,立即创建一个扩展 BaseAdapter 的自定义类。

一个好主意是将这个类放在您的 Activity 类中,以便它可以访问您的 Activity 保存的所有数据。

在扩展 BaseAdapter 时,您需要做一些事情才能使事情正常进行。

我在下面解释所有这些,但实现 BaseAdapter 的 getView() 方法是最重要的。每次 ListView 绘制一行时,运行时系统都会调用此方法。

因此,您应该在此方法中完成所有操作,您希望为单行完成所有操作。

找到下面的代码:

Class myListActivity extends Activity{
... some code here...
public void onCreate(Bundle savedInstanceState){
.....
myList.setAdapter(new myCustomAdapter());
.....
}

/**
*Custom Adapter class for the ListView containing data
*/
class myCustomAdapter extends BaseAdapter{

TextView userName;
/**
 * returns the count of elements in the Array that is used to draw the text in rows 
   * @see android.widget.Adapter#getCount()
 */
@Override
public int getCount() {
    //return the length of the data array, so that the List View knows how much rows it has to draw   
return DataArr.length;
}

/**
 * @param position The position of the row that was clicked (0-n)
 * @see android.widget.Adapter#getItem(int)
                     */
@Override
public String getItem(int position) {
    return null;
}

/**
 * @param position The position of the row that was clicked (0-n)
 * @see android.widget.Adapter#getItemId(int)
 */
@Override
public long getItemId(int position) {
    return position;
}

/**
 * Returns the complete row that the System draws.
 * It is called every time the System needs to draw a new row;
 * You can control the appearance of each row inside this function.
 * @param position The position of the row that was clicked (0-n)
 * @param convertView The View object of the row that was last created. null if its the first row
 * @param parent The ViewGroup object of the parent view
 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final int pos = position;
    if(row == null){
                    //getting custom layout to the row
        LayoutInflater inflater=getLayoutInflater();
        row=inflater.inflate(R.layout.list_row, parent, false);
    }
            //get the reference to the textview of your row. find the item with row.findViewById()
    userName= (TextView)row.findViewById(R.id.user_name);
    userName.setText(DataArr[position]);
return row; //the row that ListView draws
}
}
}

希望对您有所帮助。

请记住在单独的布局文件中创建行的布局。

如果您想更深入,请尝试CommonsGuy 网站上的此链接。这是他很棒的书的摘录,专门处理 ListView 自定义适配器

编辑:这是我关于它的博客文章和一个示例项目:http ://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html

于 2010-12-06T06:14:41.740 回答