public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show image
return ImageFragment.init(position);
case 1: // Fragment # 1 - This will show image
return ImageFragment.init(position);
default:// Fragment # 2-9 - Will show list
return ArrayListFragment.init(position);
}
}
}
}
public class ImageFragment extends Fragment {
int fragVal;
static ImageFragment init(int val) {
ImageFragment truitonFrag = new ImageFragment();
// Supply val input as an argument.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
View tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText("Truiton Fragment #" + fragVal);
return layoutView;
}
}
public class ArrayListFragment extends ListFragment {
int fragNum;
String arr[] = {"1", "2", "2", "3", "4", "5"};
static ArrayListFragment init(int val) {
ArrayListFragment truitonList = new ArrayListFragment();
// Supply val input as an argument.
Bundle args = new Bundle();
args.putInt("val", val);
truitonList.setArguments(args);
return truitonList;
}
/** * Retrieving this instance's number from its arguments. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
}
/** * The Fragment's UI is a simple text view showing its instance number and
* an associated list. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_pager_list,
container, false);
View tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText("Truiton Fragment #" + fragNum);
return layoutView;
}
@Override
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
private static final int REQUEST_CODE = 2323;
private TextView mNumberOfNotifications;
private PendingIntent mDeletePendingIntent;
protected static final String ACTION_NOTIFICATION_DELETE
= "com.example.android.activenotifications.delete";
private static final String NOTIFICATION_GROUP =
"com.example.android.activenotifications.notification_type";
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_GROUP_SUMMARY_ID = 1;
private static int sNotificationId = NOTIFICATION_GROUP_SUMMARY_ID + 1;
@Override
protected void onResume() {
super.onResume();
updateNumberOfNotifications();
}
@SuppressLint("StringFormatMatches")
protected void updateNumberOfNotifications() {
final StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();
final int numberOfNotifications = activeNotifications.length;
mNumberOfNotifications.setText(getString(R.string.active_notifications,
numberOfNotifications));
Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications));
}
protected void updateNotificationSummary() {
final StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();
int numberOfNotifications = activeNotifications.length;
for (StatusBarNotification notification : activeNotifications) {
if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) {
numberOfNotifications--;
break;
}
}
if (numberOfNotifications > 1) {
// There are %s ActiveNotifications
String notificationContent = getString(R.string.sample_notification_summary_content,
"" + numberOfNotifications);
final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.setSummaryText(notificationContent))
.setGroup(NOTIFICATION_GROUP)
.setGroupSummary(true);
final Notification notification = builder.build();
mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification);
} else {
mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID);
}
}
private void addNotificationAndUpdateSummaries() {
final NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("This is a sample notification")
.setAutoCancel(true)
.setDeleteIntent(mDeletePendingIntent)
.setGroup(NOTIFICATION_GROUP);
final Notification notification = builder.build();
mNotificationManager.notify(getNewNotificationId(), notification);
Log.d(TAG, "Add a notification");
updateNotificationSummary();
updateNumberOfNotifications();
}
public int getNewNotificationId() {
int notificationId = sNotificationId++;
if (notificationId == NOTIFICATION_GROUP_SUMMARY_ID) {
notificationId = sNotificationId++;
}
return notificationId;
}
void init() {
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
mNumberOfNotifications = (TextView) findViewById(R.id.number_of_notifications);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onBindChildViewHolder(MyChildViewHolder childViewHolder, int position, Object childListItem) {
SubcategoryParentListItem subcategoryChildListItem = (SubcategoryParentListItem) childListItem;
// handler for row child
childViewHolder.txtListChild.setText(subcategoryChildListItem.mTitle);
}
public class MyParentViewHolder extends ParentViewHolder {
public TextView lblListHeader;
public MyParentViewHolder(View itemView) {
super(itemView);
// init view parent
lblListHeader = (TextView) itemView.findViewById(R.id.lblListHeader);
}
}
public class MyChildViewHolder extends ChildViewHolder {
public TextView txtListChild;
public MyChildViewHolder(View itemView) {
super(itemView);
// init view child list
public class SubcategoryParentListItemimplements ParentListItem {
// objectpublic String mTitle;
public List<SubcategoryParentListItem> mChildItemList;
public SubcategoryParentListItem() {
mTitle = "SubcategoryParentListItem";
}
@Override