Skip to content

Instantly share code, notes, and snippets.

@goodev
Last active December 29, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodev/7597062 to your computer and use it in GitHub Desktop.
Save goodev/7597062 to your computer and use it in GitHub Desktop.
HolerView 模式示例
<?xml version="1.0" encoding="utf-8"?>
<org.goodev.NotificationLayoutItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/notif_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/notif_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/notif_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/notif_time"
android:layout_toRightOf="@id/notif_icon"
android:ellipsize="none"
android:maxLines="2" />
</org.goodev.NotificationLayoutItem>
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.text.format.DateUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class NotificationLayoutItem extends RelativeLayout {
public NotificationLayoutItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public NotificationLayoutItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NotificationLayoutItem(Context context) {
super(context);
}
private ImageView mIcon;
private TextView mText;
private TextView mTime;
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mIcon = (ImageView) findViewById(R.id.notif_icon);
mText = (TextView) findViewById(R.id.notif_text);
mTime = (TextView) findViewById(R.id.notif_time);
}
// 业务逻辑代码
public void setData(NotificationData data) {
mText.setText(data.tickerText);
mTime.setText(formatTime(data.time));
Drawable icon;
try {
icon = getContext().getPackageManager().getResourcesForApplication(data.packageName.toString()).getDrawable(data.icon);
mIcon.setImageDrawable(icon);
} catch (NotFoundException e) {
e.printStackTrace();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private CharSequence formatTime(long time) {
return DateUtils.formatDateTime(getContext(), time, DateUtils.FORMAT_SHOW_TIME);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment