通知下拉视图

Notification drop down view

我正在平板电脑上构建一个应用程序,我想实现一个通知功能,它的功能类似于显示在操作栏下方的固定大小的可滚动下拉视图,以显示通知列表用户。我已经在我的 activity 栏中添加了一个通知按钮并构建了通知系统。

我只是想到了更好的方法。您应该使用 ListView,然后用 TextViews 填充它:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:visibility="gone"
        android:background="#EEEEEE"
        android:id="@+id/notification_list_view"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/notification_list_view">
        <!-- Content here -->
    </LinearLayout>

</RelativeLayout>

可以通过将此代码放入 ActionBar 中按钮的 onClick 函数来显示通知栏。然后您可以使用 ArrayAdapter 动态添加项目:

private void onClick() {
    ListView notificationListView = (ListView) findViewById(R.id.notification_list_view);

    // if you don't need the notifications anymore use:
    // notificationListView.setVisibility(View.GONE);
    notificationListView.setVisibility(View.VISIBLE);

    final List<String> notificationList = new ArrayList<>();
    notificationList.add("Notification 1");
    notificationList.add("Notification 2");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, notificationList);
    notificationListView.setAdapter(adapter);
    notificationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String chosenNotification = notificationList.get(position);
        }
    });
}

如您所见,如果需要,您可以获得点击通知。