使用CustomListViewAdapter时以编程方式滚动到ListView中的项目

时间:2018-08-12 16:52:45

标签: android listview listview-adapter

我试图在使用CustomListViewAdapter时以编程方式滚动到列表视图中的某个位置。

我使用customlistviewadapter,因为只要单击列表视图中的项目,它就会“打开”以显示一些文本。正是在这一点上,我想以编程方式滚动到刚刚显示的文本的顶部。

当前,一切正常,除了我不知道如何调用该函数:

listview.setSelection(currentPosition);

我似乎无法在CustomListViewAdapter中找到它,这是我侦听listview项上的click事件的地方。

我尝试致电:

parent.setSelected(true);

只需单击该项目并单击功能

notifyDataSetChanged();

被调用,但不执行任何操作。

我怎么打电话

listview.setSelection(currentPosition);

甚至更好:

listview.smoothScrollToPositionFromTop(position,offset,duration);

在CustomListViewAdapter类中?

下面是CustomListViewAdapter的代码。

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;
ArrayList<RowItem> _rowItems;
RowItem item;
View row;
TextView ReferenceGospel;

  public CustomListViewAdapter(Context context, int resourceId,
                             ArrayList<RowItem> rowItems) {


    super(context, resourceId, rowItems);
    this.context = context;
    _rowItems = rowItems;
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
row = convertView;
item = _rowItems.get(position);

 if (row == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = mInflater.inflate(R.layout.book_list, parent, false);
        row.setTag(thisPosition);

    }

ReferenceGospel = (TextView) row.findViewById(R.id.gospelRef);

// ... some other code here

ReferenceGospel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            item = _rowItems.get(position);
            CustomListViewAdapter.this.notifyDataSetChanged();
  }
     });
    return row;
}

我看了几篇文章,但没有帮助。 谢谢。

1 个答案:

答案 0 :(得分:2)

在适配器的构造函数中传递listview:

ListView listView;

  public CustomListViewAdapter(Context context, ListView listView,
                         ArrayList<RowItem> rowItems) {


 super(context, resourceId, rowItems);
 this.context = context;
 this.listView = listView;
 _rowItems = rowItems;

}

然后您可以致电listview.smoothScrollToPositionFromTop(position,offset,duration); 通过getView方法。

希望有帮助。

相关问题