ExpandableListView - 自动折叠组

时间:2015-08-24 19:03:35

标签: android expandablelistview collapse

我使用自定义适配器创建了一个ExpandableListView,所有功能都完美无缺,包括click,collapse和expand事件。如果有人需要有关可扩展列表视图的帮助,我按照教程:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

我现在想在我的setOnGroupExpandListener方法中编写代码,以折叠所有其他与当前焦点分开的组。 (内容意识 - 从某种意义上说)澄清一下,如果我有以下内容:

第1项

  • 子项目1
  • 子项目2

第2项

  • 子项目1

第3项

  • 子项目1
  • 子项目2

如果我点击第1项,它应该展开第1项(​​这已经有效),它应该为我折叠第2项和第3项组。这有可能吗?我怎样才能做到这一点?

这是我当前的setOnGroupExpandListener:

    gpsMenuListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(), "Expanded: " + gpsMenuListDataHeader.get(groupPosition).toString(), Toast.LENGTH_SHORT).show();
        }
    });

正如您所看到的,我目前只有Toast打印消息,但我想添加折叠功能。

2 个答案:

答案 0 :(得分:8)

listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                YourExpandableListAdapter customExpandAdapter = (YourExpandableListAdapter)listView.getExpandableListAdapter();
                if (customExpandAdapter == null) {return;}
                for (int i = 0; i < customExpandAdapter.getGroupCount(); i++) {
                    if (i != groupPosition) {
                        listView.collapseGroup(i);
                    }
                }
            }
        });

答案 1 :(得分:-1)

您需要运行整个列表并折叠除您当前所在群组之外的所有其他群组:

<?php
# Store the user input username
if (isset($_SESSION['email']) && strlen($_SESSION['email']) > 0) {
  $email = $_SESSION['email'];
} else {
  // Die the error
  printf('No email address available');
  exit;
}

# Store DB Credentials
$DBHost = 'localhost';
$DBUser = 'username';
$DBPass = 'password';
$DBName = 'database';

# Create a database connection for PHP to use
$link = mysqli_connect($DBHost, $DBUser, $DBPass);
// Preform from tasks to ensure the connection is active
if (!$link)
{
  // Die the error
  printf('Unable to connect to the database server.');
  exit;
}
// Sets encoding type to uft8
if (!mysqli_set_charset($link, 'utf8'))
{
  // Die the error
  printf('Unable to set database connection encoding.');
  exit;
}
// Set database that is in use (makes queries shorter to write)
if (!mysqli_select_db($link, $DBName))
{
  // Die the error
  printf('Unable to locate the database.');
  exit;
}

# Query the database
// Build the query
$query = 'SELECT `firstname`,`lastname`,`username` ';
$query .= 'FROM `table` ';
$query .= 'WHERE `email` = ? ';
$query .= 'LIMIT 1 ';
// Prepare it
$stmt = $link->prepare($query);
if (!$stmt)
{
  // Die the error
  printf($link->errno.'  :  '.$link->error);
  exit;
}
// Bind in the user input data so as to avoid SQL injection
if (!$stmt->bind_param('s', $email))
{
  // Die the error
  printf($link->errno.'  :  '.$link->error);
  exit;
}
// Execute the query
if (!$stmt->execute())
{
  // Die the error
  printf($link->errno.'  :  '.$link->error);
  exit;
}
// Bind the results to some variables
if (!$stmt->bind_result($firstname,$lastname,$username))
{
  // Die the error
  printf($link->errno.'  :  '.$link->error);
  exit;
}
// Fetch the data
$stmt->fetch();
// Close the query
$stmt->close();

# Build the html
$pageHtml = '
<p>First Name: '.$firstname.'</p>
<p>Last Name: '.$lastname.'</p>
<p>User Name: '.$username.'</p>
';

# Display the html
echo $html;
exit;
?>