所以在NameList中,它表示“fragmentTransaction.add(android.R.id.content,fragment2,”login“)。commit();”当我尝试R.id.container时,我需要找到NameList的容器,出现一个空白的白色屏幕。在这种情况下,我应该把什么代替android.R.id.content?
NameList.java
package com.drdc1.medic;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* A simple {@link Fragment} subclass.
*/
public class NameList extends Fragment implements DataObserver {
private SoldierListAdapter adapter;
private HomeActivity homeActivity;
private ArrayList<Soldier> soldiers = new ArrayList<>();
public NameList() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_name_list, container, false);
homeActivity = (HomeActivity) getActivity();
homeActivity.registerFragment(this);
final ListView listView = (ListView) rootView.findViewById(R.id.soldierList);
addListHeader(listView);
DataManager dbManager = ((AppContext) getActivity().getApplication()).getDataManager();
soldiers = dbManager.getActiveSoldier();
adapter = new SoldierListAdapter(getContext(), soldiers);
listView.setAdapter(adapter);
TextView nameHeader = (TextView) rootView.findViewById(R.id.name_header);
nameHeader.setOnClickListener(new ClickToSort());
TextView hrHeader = (TextView) rootView.findViewById(R.id.hr_header);
hrHeader.setOnClickListener(new ClickToSort());
TextView brHeader = (TextView) rootView.findViewById(R.id.br_header);
brHeader.setOnClickListener(new ClickToSort());
TextView skinTmpHeader = (TextView) rootView.findViewById(R.id.skin_tmp_header);
skinTmpHeader.setOnClickListener(new ClickToSort());
TextView core_tmp_header = (TextView) rootView.findViewById(R.id.core_tmp_header);
core_tmp_header.setOnClickListener(new ClickToSort());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO When item clicked, write code here
String idsol = null;
if (((Soldier) listView.getAdapter().getItem(position)) != null) {
idsol = ((Soldier) listView.getAdapter().getItem(position)).getId();
}
android.support.v4.app.FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
if (idsol != null){
bundle.putString("id", idsol); // Put anything what you want
}
IndividualSoldierTab fragment2 = new IndividualSoldierTab();
fragment2.setArguments(bundle);
fragmentTransaction.add(android.R.id.content, fragment2, "login").commit();
// getFragmentManager()
// .beginTransaction()
// .replace(R.id.container, fragment2)
// .commit();
}
});
return rootView;
}
@Override
public void onPause() {
super.onPause();
homeActivity.registerFragment(this);
}
@Override
public void onResume() {
super.onResume();
homeActivity.registerFragment(this);
}
@Override
public void onDestroyView() {
homeActivity.unregisterFragment(this);
super.onDestroyView();
}
@Override
public void update(Map data) {
Boolean soldierInTheList = false;
for (Soldier s : soldiers) {
if (data.get("ID").equals(s.getId())) {
s.setPhysioData(data);
soldierInTheList = true;
}
}
if (!soldierInTheList) {
Soldier newSol = new Soldier((String) data.get("name"), (String) data.get("ID"));
newSol.setPhysioData(data);
soldiers.add(newSol);
}
adapter.notifyDataSetChanged();
}
private void addListHeader(ListView lv) {
View headerView =
LayoutInflater.from(getContext()).inflate(R.layout.list_header, lv, false);
lv.addHeaderView(headerView);
}
private class SoldierListAdapter extends ArrayAdapter<Soldier> {
private final ArrayList<Soldier> soldiers;
private String sortedBy = "";
// the soldier list is only sorted by 1 attribute at a time. don't overthink about stable sorting
public SoldierListAdapter(Context context, ArrayList<Soldier> soldiers) {
super(context, R.layout.list_item, soldiers);
this.soldiers = soldiers;
}
public String getSortedBy() {
return sortedBy;
}
/***
* When the user click the header when soldiers are already sorted, reverse the order
*/
public void reverse() {
Collections.reverse(soldiers);
}
/***
* @param x "Sort by x" Sortby is an enum
*/
public void sort(SortBy x) {
switch (x) {
case NAME:
this.sort(new OrderByName());
sortedBy = "NAME";
break;
case HR:
this.sort(new OrderByHR());
sortedBy = "HR";
break;
case BR:
this.sort(new OrderByBR());
sortedBy = "BR";
break;
case SKINTMP:
this.sort(new OrderBySkinTmp());
sortedBy = "SKINTMP";
break;
case CORETMP:
this.sort(new OrderByCoreTmp());
sortedBy = "CORETMP";
break;
default:
}
}
@NonNull
@Override
public View getView(int position, View v, ViewGroup parent) {
// Get the data item for this position
Soldier soldier = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
v = LayoutInflater.from(getContext())
.inflate(R.layout.list_item, parent, false);
}
TextView name = (TextView) v.findViewById(R.id.name);
// name.setOnClickListener(new setFragmentTransition());
name.setText(soldier.getName());
ImageView overall = (ImageView) v.findViewById(R.id.overal_status);
switch (soldier.getOverallStatus()) {
case "RED":
overall.setImageResource(R.drawable.red);
break;
case "GREEN":
overall.setImageResource(R.drawable.green);
break;
case "YELLOW":
overall.setImageResource(R.drawable.yellow);
break;
default:
break;
}
if (soldier.getBodyOrientation() != null && !soldier.getBodyOrientation().isEmpty()) {
ImageView bodyPos = (ImageView) v.findViewById(R.id.body_position);
bodyPos.setImageResource(R.drawable.body);
switch (soldier.getBodyOrientation()) {
case "UP":
break;
case "DOWN":
bodyPos.setRotation(180);
break;
case "LEFT":
bodyPos.setRotation(270);
break;
case "RIGHT":
bodyPos.setRotation(90);
break;
default:
break;
}
}
String currentHR = soldier.getHeartRate();
String lastHR = soldier.getLastHeartRate();
if (!currentHR.equals("")) {
TextView heartRate = (TextView) v.findViewById(R.id.hr_text);
ImageView arrow = (ImageView) v.findViewById(R.id.hr_img);
if (!lastHR.equals("")) {
if (Integer.parseInt(currentHR) > Integer.parseInt(lastHR)) {
arrow.setImageResource(R.drawable.up_arrow);
heartRate.setTextColor(Color.RED);
} else if (Integer.parseInt(currentHR) < Integer.parseInt(lastHR)) {
arrow.setImageResource(R.drawable.down_arrow);
heartRate.setTextColor(Color.YELLOW);
} else {
arrow.setImageResource(0);
heartRate.setTextColor(Color.BLACK);
}
}
heartRate.setText(currentHR);
}
//TODO replace image for down_arrow color not right
String currentBR = soldier.getBreathingRate();
String lastBR = soldier.getLastBreathingRate();
if (!currentBR.equals("")) {
TextView breathingRate = (TextView) v.findViewById(R.id.br_text);
ImageView arrow = (ImageView) v.findViewById(R.id.br_img);
if (!lastBR.equals("")) {
if (Integer.parseInt(currentBR) > Integer.parseInt(lastBR)) {
arrow.setImageResource(R.drawable.up_arrow);
breathingRate.setTextColor(Color.RED);
} else if (Integer.parseInt(currentBR) < Integer.parseInt(lastBR)) {
arrow.setImageResource(R.drawable.down_arrow);
breathingRate.setTextColor(Color.YELLOW);
} else {
arrow.setImageResource(0);
breathingRate.setTextColor(Color.BLACK);
}
}
breathingRate.setText(currentBR);
}
String currentCoreTmp = soldier.getCoreTmp();
String lastCoreTmp = soldier.getLastCoreTmp();
if (!currentCoreTmp.equals("")) {
TextView coreTmp = (TextView) v.findViewById(R.id.core_tmp_text);
ImageView arrow = (ImageView) v.findViewById(R.id.core_tmp_img);
if (!lastCoreTmp.equals("")) {
if (Integer.parseInt(currentCoreTmp) > Integer.parseInt(lastCoreTmp)) {
arrow.setImageResource(R.drawable.up_arrow);
coreTmp.setTextColor(Color.RED);
} else if (Integer.parseInt(currentCoreTmp) < Integer.parseInt(lastCoreTmp)) {
arrow.setImageResource(R.drawable.down_arrow);
coreTmp.setTextColor(Color.YELLOW);
} else {
arrow.setImageResource(0);
coreTmp.setTextColor(Color.BLACK);
}
}
coreTmp.setText(currentCoreTmp);
}
String currentSkinTmp = soldier.getSkinTmp();
String lastSkinTmp = soldier.getLastSkinTmp();
if (!currentSkinTmp.equals("")) {
TextView skinTmp = (TextView) v.findViewById(R.id.skin_tmp_text);
ImageView arrow = (ImageView) v.findViewById(R.id.skin_tmp_img);
if (!lastSkinTmp.equals("")) {
if (Integer.parseInt(currentSkinTmp) > Integer.parseInt(lastSkinTmp)) {
arrow.setImageResource(R.drawable.up_arrow);
skinTmp.setTextColor(Color.RED);
} else if (Integer.parseInt(currentSkinTmp) < Integer.parseInt(lastSkinTmp)) {
arrow.setImageResource(R.drawable.down_arrow);
skinTmp.setTextColor(Color.YELLOW);
} else {
arrow.setImageResource(0);
skinTmp.setTextColor(Color.BLACK);
}
}
skinTmp.setText(currentSkinTmp);
}
//TextView tvHome = (TextView) v.findViewById(R.id.tvHome);
// Populate the data into the template view using the data object
// Return the completed view to render on screen
return v;
}
}
private class ClickToSort implements View.OnClickListener {
@Override
public void onClick(View v) {
String sortBy = (String) ((TextView) v).getText();
switch (sortBy.toLowerCase()) {
case "name":
if (adapter.getSortedBy().equals("NAME")) {
adapter.reverse();
} else {
adapter.sort(SortBy.NAME);
}
break;
case "hr":
if (adapter.getSortedBy().equals("HR")) {
adapter.reverse();
} else {
adapter.sort(SortBy.HR);
}
break;
case "br":
if (adapter.getSortedBy().equals("BR")) {
adapter.reverse();
} else {
adapter.sort(SortBy.BR);
}
break;
case "skintmp":
if (adapter.getSortedBy().equals("SKINTMP")) {
adapter.reverse();
} else {
adapter.sort(SortBy.SKINTMP);
}
break;
case "coretmp":
if (adapter.getSortedBy().equals("CORETMP")) {
adapter.reverse();
} else {
adapter.sort(SortBy.CORETMP);
}
break;
default:
}
adapter.notifyDataSetChanged();
}
}
/************************************************************************
* Comparator classes to support different ordering of soldiers
*************************************************************************/
private enum SortBy {
NAME, HR, BR, SKINTMP, CORETMP
}
private static class OrderByName implements Comparator<Soldier> {
@Override
public int compare(Soldier s1, Soldier s2) {
return s1.getName().compareTo(s2.getName());
}
}
private static class OrderByHR implements Comparator<Soldier> {
@Override
public int compare(Soldier s1, Soldier s2) {
return Integer.parseInt(s1.getHeartRate().equals("") ? "0" : s1.getHeartRate())
- Integer.parseInt(s2.getHeartRate().equals("") ? "0" : s2.getHeartRate());
}
}
private static class OrderByBR implements Comparator<Soldier> {
@Override
public int compare(Soldier s1, Soldier s2) {
return Integer.valueOf(s1.getBreathingRate().equals("") ? "0" : s1.getBreathingRate())
.compareTo(Integer.valueOf(
s2.getBreathingRate().equals("") ? "0" : s2.getBreathingRate()));
}
}
private static class OrderBySkinTmp implements Comparator<Soldier> {
@Override
public int compare(Soldier s1, Soldier s2) {
return Integer.valueOf(s1.getSkinTmp().equals("") ? "0" : s1.getSkinTmp())
.compareTo(Integer.valueOf(s2.getSkinTmp().equals("") ? "0" : s2.getSkinTmp()));
}
}
private static class OrderByCoreTmp implements Comparator<Soldier> {
@Override
public int compare(Soldier s1, Soldier s2) {
return Integer.valueOf(s1.getCoreTmp().equals("") ? "0" : s1.getCoreTmp())
.compareTo(Integer.valueOf(s2.getCoreTmp().equals("") ? "0" : s2.getCoreTmp()));
}
}
}
HomeActivity.java
package com.drdc1.medic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
// This is the home page of the tab application.
public class HomeActivity extends AppCompatActivity implements FragmentDataManager {
private SectionsPagerAdapter mSectionsPagerAdapter;
private DataReceiver dataReceiver;
private ArrayList<DataObserver> fragmentlist;
static private HashMap data; //one data shared by all fragments
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startBackgroundServices();
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
//register intent filter for LocalBroadbast Manager
IntentFilter PDAMESSAGE = new IntentFilter("PDAMessage");
dataReceiver = new DataReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(dataReceiver, PDAMESSAGE);
data = new HashMap();
fragmentlist = new ArrayList<>();
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void registerFragment(DataObserver o) {
if (!fragmentlist.contains(o))
fragmentlist.add(o);
}
@Override
public void unregisterFragment(DataObserver o) {
if (fragmentlist.contains(o)) {
int observerIndex = fragmentlist.indexOf(o);
fragmentlist.remove(observerIndex);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void notifyObserver(Map data) {
for (DataObserver observer : fragmentlist) {
observer.update(data);
}
}
class DataReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceive(Context context, Intent intent) {
data.put("bodypos", intent.getStringExtra("bodypos"));
data.put("overall", intent.getStringExtra("overall"));
data.put("coreTemp", intent.getStringExtra("coreTmp"));
data.put("skinTemp", intent.getStringExtra("skinTmp"));
data.put("br", intent.getStringExtra("br"));
data.put("hr", intent.getStringExtra("hr"));
data.put("ID",intent.getStringExtra("ID"));
data.put("name",intent.getStringExtra("name"));
notifyObserver(data);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(
getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
SquadStatus tab1 = new SquadStatus();
return tab1;
case 1:
NameList tab2 = new NameList();
return tab2;
case 2:
IndividualSoldierTab tab3 = new IndividualSoldierTab();
return tab3;
case 3:
TreatmentScreenTab tab4 = new TreatmentScreenTab();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
// Show 4 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Squad Status";
case 1:
return "Name List";
case 2:
return "Individual Soldier";
case 3:
return "Treatment Screen";
}
return null;
}
}
private void startBackgroundServices() {
Intent Serviceintent = new Intent();
Serviceintent.setClass(this, BackgroundServer.class);
startService(Serviceintent);
Intent sleepAlgo = new Intent();
sleepAlgo.setClass(this, BackgroundSleepAlgo.class);
startService(sleepAlgo);
Intent wellnessAlgo = new Intent();
wellnessAlgo.setClass(this, BackgroundWellnessAlgo.class);
startService(wellnessAlgo);
}
public void callHelp(View v) {
Intent callhelp = new Intent(HomeActivity.this, HelpActivity.class);
startActivity(callhelp);
}
}
答案 0 :(得分:0)
容器应该是什么?
您可以简单地使用FrameLayout作为容器布局并动态添加片段。