added onclicklistener to notificationlist

This commit is contained in:
danijoo
2016-01-02 22:02:18 +01:00
parent a53acba581
commit 31c053b9c3
3 changed files with 40 additions and 4 deletions

View File

@@ -20,7 +20,7 @@ import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public class NotificationListAdapter extends CursorRecyclerViewAdapter<NotificationListAdapter.NotificationViewHolder> { public class NotificationListAdapter extends CursorRecyclerViewAdapter<NotificationListAdapter.NotificationViewHolder> implements View.OnClickListener {
class NotificationViewHolder extends RecyclerView.ViewHolder { class NotificationViewHolder extends RecyclerView.ViewHolder {
@@ -61,13 +61,25 @@ public class NotificationListAdapter extends CursorRecyclerViewAdapter<Notificat
Drawable smallIcon = PackageUtils.getApplicationDrawable(context, n.packageName, n.smallIconId); Drawable smallIcon = PackageUtils.getApplicationDrawable(context, n.packageName, n.smallIconId);
ivSmallIcon.setImageDrawable(smallIcon); ivSmallIcon.setImageDrawable(smallIcon);
itemView.setTag(n.id);
} }
} }
interface NotificationClickListener {
void onNotificationClick(long id);
}
private NotificationClickListener mNotificationClickListener;
public NotificationListAdapter(Context context, Cursor cursor) { public NotificationListAdapter(Context context, Cursor cursor) {
super(context, cursor); super(context, cursor);
} }
public void setNotificationClickListener(NotificationClickListener listener) {
mNotificationClickListener = listener;
}
@Override @Override
public void onBindViewHolder(NotificationViewHolder viewHolder, Cursor cursor) { public void onBindViewHolder(NotificationViewHolder viewHolder, Cursor cursor) {
LoggedNotification n = Logged_notificationTable.getRow(cursor, false); LoggedNotification n = Logged_notificationTable.getRow(cursor, false);
@@ -77,6 +89,16 @@ public class NotificationListAdapter extends CursorRecyclerViewAdapter<Notificat
@Override @Override
public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_list_item, parent, false); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.notification_list_item, parent, false);
v.setOnClickListener(this);
return new NotificationViewHolder(v); return new NotificationViewHolder(v);
} }
@Override
public void onClick(View v) {
if(mNotificationClickListener != null) {
long id = (long) v.getTag();
mNotificationClickListener.onNotificationClick(id);
}
}
} }

View File

@@ -14,13 +14,14 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import net.headlezz.notificationlogger.R; import net.headlezz.notificationlogger.R;
import net.headlezz.notificationlogger.logger.LoggedNotification;
import net.headlezz.notificationlogger.logger.Logged_notificationTable; import net.headlezz.notificationlogger.logger.Logged_notificationTable;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import timber.log.Timber; import timber.log.Timber;
public class NotificationListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { public class NotificationListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, NotificationListAdapter.NotificationClickListener {
final int LOADER_ID = 124; final int LOADER_ID = 124;
@@ -32,6 +33,9 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
private NotificationListAdapter mAdapter; private NotificationListAdapter mAdapter;
// TODO reload list if new notification arrives
// TODO notification list divider
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -46,6 +50,7 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mAdapter = new NotificationListAdapter(getContext(), null); mAdapter = new NotificationListAdapter(getContext(), null);
mAdapter.setNotificationClickListener(this);
mNotificationList.setAdapter(mAdapter); mNotificationList.setAdapter(mAdapter);
getLoaderManager().initLoader(LOADER_ID, Bundle.EMPTY, this); getLoaderManager().initLoader(LOADER_ID, Bundle.EMPTY, this);
} }
@@ -86,4 +91,13 @@ public class NotificationListFragment extends Fragment implements LoaderManager.
mNotificationList.setVisibility(View.VISIBLE); mNotificationList.setVisibility(View.VISIBLE);
} }
} }
@Override
public void onNotificationClick(long id) {
String qry = Logged_notificationTable.FIELD__ID + " = ? ";
String[] args = { String.valueOf(id) };
Cursor cursor = getContext().getContentResolver().query(Logged_notificationTable.CONTENT_URI, null, qry, args, null);
LoggedNotification notification = Logged_notificationTable.getRow(cursor, true);
Timber.e(notification.title);
}
} }

View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:andorid="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="8dp"> android:padding="8dp"
android:foreground="?attr/selectableItemBackground">
<ImageView <ImageView
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"