content_main.xml具有框架布局,此布局可以容纳片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ALRugaib"
tools:showIn="@layout/app_bar_alrugaib">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_alrugaib"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_alrugaib">
<ExpandableListView
android:layout_marginTop="175dp"
android:id="@+id/lvExp"
android:gravity="left"
android:layoutDirection="rtl"
android:dividerHeight="1dp"
android:background="#ffffff"
android:layout_height="match_parent"
android:layout_width="match_parent"/></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
sample_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Sample_Fragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
这是我的mainActivity,我无法进入片段,我在新片段的位置使用了亲和力,但其意图是用于新的Activity。
public class ALRugaib extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<StringlistDataHeader;
LinkedHashMap<String, List<String>listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alrugaib);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
String data =listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
Fragment fragment1 = null;
if(data=="Sofas"){
Fragment fragment=null;
Class fragmentClass=null;
fragmentClass=Sample_Fragment.class;
try {
fragment= (android.support.v4.app.Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
if (fragment!=null){
String backStateName = fragment.getClass().getName();
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new LinkedHashMap<String, List<String>>();
// Adding Parent data
listDataHeader.add("New Arrivals");
listDataHeader.add("Rooms");
listDataHeader.add("Lighting");
listDataHeader.add("Mattresses");
listDataHeader.add("Gift Cards");
listDataHeader.add("Offers");
// Adding child data
List<StringnewArrivals = new ArrayList<String>();
newArrivals.add("Sofas");
newArrivals.add("Chairs");
newArrivals.add("Tables");
newArrivals.add("Storage");
List<Stringrooms = new ArrayList<String>();
rooms.add("Beds");
rooms.add("Bedroom Sets");
rooms.add("Kids Bedroom");
rooms.add("Storage & Acessories");
List<Stringlighting = new ArrayList<String>();
lighting.add("Dining Tables");
lighting.add("Dining Storage");
lighting.add("Bar Furniture");
List<Stringmattresses = new ArrayList<String>();
mattresses.add("Mattresses & Bedding");
mattresses.add("Comforters");
mattresses.add("Bed Linen");
List<StringgiftCards = new ArrayList<String>();
giftCards.add("Study Tables");
giftCards.add("Study Chairs");
giftCards.add("Study Lamps");
List<Stringoffers = new ArrayList<String>();
offers.add("Carpets & Rugs");
offers.add("Curtains");
offers.add("Lighting");
offers.add("Decor Accessories");
listDataChild.put(listDataHeader.get(0), newArrivals); // Header, Child data
listDataChild.put(listDataHeader.get(1), rooms);
listDataChild.put(listDataHeader.get(2), lighting);
listDataChild.put(listDataHeader.get(3),mattresses);
listDataChild.put(listDataHeader.get(4),giftCards);
listDataChild.put(listDataHeader.get(5),offers);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alrugaib, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} }
这是示例片段,可以在我的frameLayout中看到
public class Sample_Fragment extends Fragment {
public Sample_Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sample_, container, false);
}
}