滚动列表视图到新添加项目的位置

huangapple go评论66阅读模式
英文:

Scrolling the listview to position of the newly added item

问题

我有一个带有ListView的自定义适配器。我从SQL Server查询项目,并使用我的ListView将它们显示为列表。我在ListView上有项目点击侦听器,以便获取项目位置。

public void getData() {
    String query = "SELECT * FROM Table";
    List<Map<String, String>> data = new ArrayList<>();
    data = filterData(query);
    adapter = new ContactListAdapter(this, data, R.layout.items, from, to);
    listView.setAdapter(adapter);
    listView.invalidateViews();

    listView.setOnItemClickListener((adapterView, view, i, l) -> {
        HashMap<String, String> retrieve = (HashMap<String, String>) adapterView.getAdapter().getItem(i);
        callerID = retrieve.get("CallerDetailID");
        .
        .
    });
}

我使用HashMap字符串将值存储在列表中,如下所示。

// 从SQL Server筛选联系人详细信息
List<Map<String, String>> filterData(String query) {
    List<Map<String, String>> contact_details = new ArrayList<>();
    try {
        connect = connectionClass.CONN();  // 连接到数据库
        Statement stmt = connect.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            Map<String, String> datanum = new HashMap<>();
            datanum.put("CallerDetailID", rs.getString("CallerID"));
            .
            .
            .
            contact_details.add(datanum);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contact_details;
}

我知道我可以通过单击项目来将其定位到位置(因为我已经使用了setOnItemClickListener)。但我这里的问题是,如何在不点击项目的情况下滚动到新添加的项目位置?

一旦我获得了项目的位置,我可以在ListView中使用以下其中一种方法进行滚动。

listView.smoothScrollToPosition();
listView.smoothScrollToPosition();
listView.smoothScrollToPositionFromTop();

是否可能在ListView中不点击项目的情况下获取项目的位置?有什么想法吗?

英文:

I have a custom adapter with a listview. I query the items from SQL Server and display them as a list using my listview. I have item click listener on my list view in order to get the item position.

  public void getData() {
    String query = &quot;SELECT * FROM Table&quot;;
    List&lt;Map&lt;String, String&gt;&gt; data = new ArrayList&lt;&gt;();
    data = filterData(query);
    adapter = new ContactListAdapter(this, data, R.layout.items, from, to);
    listView.setAdapter(adapter);
    listView.invalidateViews();


    listView.setOnItemClickListener((adapterView, view, i, l) -&gt; {
        HashMap&lt;String, String&gt; retreive = (HashMap&lt;String, String&gt;) adapterView.getAdapter().getItem(i);
        callerID = retreive.get(&quot;CallerDetailID&quot;);
        .
        .
    });
}

I store the values using hashmap strings in a list as follows.

// filter the contact details from sql server
List&lt;Map&lt;String, String&gt;&gt; filterData(String query) {
    List&lt;Map&lt;String, String&gt;&gt; contact_details = new ArrayList&lt;&gt;();
    try {
        connect = connectionClass.CONN();  // Connect to database
        Statement stmt = connect.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            Map&lt;String, String&gt; datanum = new HashMap&lt;&gt;();
            datanum.put(&quot;CallerDetailID&quot;, rs.getString(&quot;CallerID&quot;));
            .
            .
            .
            contact_details.add(datanum);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contact_details;
}

I am aware that I can get the items to the position by clicking on it (since I already use setOnItemClickListener). But my question here is how can I scroll to the newly added item's position in my list view, without clicking the item?

Once I get the position of the item, I can use one of the following methods in the listview to scroll.

    listView.smoothScrollToPosition();
    listView.smoothScrollToPosition();
    listView.smoothScrollToPositionFromTop();

Is it possible to get the item's to position without clicking on the item in the listview? Any ideas folks.

答案1

得分: 2

要实现所需的行为,您需要向listview添加一个属性以实现自动滚动。

android:transcriptMode="alwaysScroll"

您只需要调用notifyDataSetChanged(),您的列表将会自动滚动。

英文:

To achieve the behaviour you need to add a property to scroll listview automatically.

android:transcriptMode=&quot;alwaysScroll&quot;

You just have to call notifyDataSetChanged() and your list will be auto scrolled.

huangapple
  • 本文由 发表于 2020年10月26日 20:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64537310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定