public class HistoryFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
ListView listView;
private OnFragmentInteractionListener mListener;
public HistoryFragment() {
// Required empty public constructor
}
public static HistoryFragment newInstance(String param1, String param2) {
HistoryFragment fragment = new HistoryFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_history, container, false);
String[] locations = {"广州市番禺区1", "广州市天河区2", "广州市海珠区3",
"广州市越秀区4", "广州市南沙区5", "广州市佛山区6", "广州市番禺区7", "广州市天河区8",
"广州市佛山区9", "广州市海珠区0", "广州市越秀区11", "广州市南沙区12",
"广州市佛山区13", "广州市番禺区14", "广州市天河区15", "广州市海珠区16",
"广州市越秀区17", "广州市南沙区18", "广州市中山大道19"};
List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
View view = inflater.inflate(R.layout.fragment_history,null);
listView = view.findViewById(R.id.history_listview);
Map<String, Object> item = new HashMap<String, Object>();
for(int i=0;i < locations.length;i++) {
item.put("location", locations[i]);
items.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(),items,
R.layout.history_list_item,new String[]{"locations"},new int[]{R.id.history_location});
listView.setAdapter(simpleAdapter);
// listView.setAdapter(new ArrayAdapter<>(view.getContext(), R.layout.history_list_item,locations));
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
----------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity implements MapFragment.OnFragmentInteractionListener,
HistoryFragment.OnFragmentInteractionListener,SettingsFragment.OnFragmentInteractionListener{
// private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment;
switch (item.getItemId()) {
case R.id.navigation_map:
// mTextMessage.setText(R.string.title_map);
fragment = MapFragment.newInstance("","");
fragmentTransaction.replace(R.id.content,fragment);
fragmentTransaction.commit();
return true;
case R.id.navigation_history:
fragment = HistoryFragment.newInstance("","");
fragmentTransaction.replace(R.id.content,fragment);
fragmentTransaction.commit();
// mTextMessage.setText(R.string.title_history);
return true;
case R.id.navigation_settings:
fragment = SettingsFragment.newInstance("","");
fragmentTransaction.replace(R.id.content,fragment);
fragmentTransaction.commit();
// mTextMessage.setText(R.string.title_settings);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment;
fragment = MapFragment.newInstance("","");
fragmentTransaction.replace(R.id.content,fragment);
fragmentTransaction.commit();
// mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setItemIconTintList(null);
// navigation.setSelectedItemId(R.id.navigation_map);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
Fragment 的layout文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.fragment.HistoryFragment">
<!-- TODO: Update blank fragment layout -->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:text="@string/hello_history_fragment" />-->
<ListView
android:id="@+id/history_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
list item layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/history_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
main activity layout 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--<TextView-->
<!--android:id="@+id/message"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="@dimen/activity_vertical_margin"-->
<!--android:layout_marginLeft="@dimen/activity_horizontal_margin"-->
<!--android:layout_marginRight="@dimen/activity_horizontal_margin"-->
<!--android:layout_marginTop="@dimen/activity_vertical_margin"-->
<!--android:text="@string/title_map" />-->
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>