自定义列表视图以显示通知

时间:2018-02-19 08:24:47

标签: java android

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    super.onNotificationRemoved(sbn);
}

public List<String> getText(Notification notification) {
    if (null == notification) {
        return null;
    }
    RemoteViews views = notification.bigContentView;
    if (views == null) {
        views = notification.contentView;
    }
    if (views == null) {
        return null;
    }

如何将此代码放在android列表视图中 在列表中显示通知。 还为它提供了xml布局。

1 个答案:

答案 0 :(得分:0)

首先为您的简单ListView创建一个XML文件 my_list_view.xml

<ListView
        android:id="@+id/simpleListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp"/>

然后创建另一个xml文件 row_view.xml ,它将是ListView中每行的布局

 <RelativeLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="horizontal"
         android:padding="10dp">
         <TextView
                 android:id="@+id/title"
                 android:textSize="25sp"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"/>
          <TextView
                 android:id="@+id/subTitle"
                 android:textSize="20sp"
                 android:layout_below="@+id/title"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"/>
 </RelativeLayout>

下一步是创建一个CustomAdapter,以通过通知正确地扩充ListView

public class CustomAdapter extends BaseAdapter {
Context context;
String titles[];
String subTitles[];
LayoutInflater inflter;

public CustomAdapter(Context applicationContext, String[] titles, String[] subTitles)
{
    this.context = context;
    this.titles = titles;
    this.subTitles=subTitles;
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    return titles.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.row_view, null);
    Textview tv1=(TextView)view.findViewById(R.id.title);
    Textview tv2=(TextView)view.findViewById(R.id.subTitle);
    tv1.setText(titles[i]);
    tv2.setText(subTitles[i]);
    return view;
}

}

最后在您的 MainActivity.java 中执行以下操作

    String notTitles[] = YOUR_NOTIFICATION_TITLES;
    String notSubTitles[]=YOUR_NOTIFICATION_SUBTITLES;
    ListView simpleList= (ListView) findViewById(R.id.simpleListView);
    CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), notTitles, notSubTitles);
    simpleList.setAdapter(customAdapter);

是的。 :)