我正在尝试制作一个滑动菜单,就像 Youtube 或 Google+ 应用程序一样。
我正在关注这个项目代码,我想要做的是当我点击左侧栏上的一个项目时,活动会随着新内容滑回左侧。
就像您在 Google+主屏幕上时一样,单击左侧菜单上的配置文件项目,它会在加载配置文件屏幕时滑回左侧。
所以这是我的代码。
我有这个名为ExampleActivity.java的主要活动:
public class ExampleActivity extends SlidingFragmentActivity
{
//public PagerAdapter adapter;
public FragmentManager mFragmentManager;
private List<Fragment> mFragments = new ArrayList<Fragment>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mFragmentManager = getSupportFragmentManager();
// set the Behind View
setBehindContentView(R.layout.frame);
getSupportFragmentManager().beginTransaction().add(R.id.frame, new SampleListFragment()).commit();
// customize the SlidingMenu
this.setSlidingActionBarEnabled(true);
getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
getSlidingMenu().setShadowDrawable(R.drawable.shadow);
getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
getSlidingMenu().setBehindScrollScale(0.25f);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
// customize the ActionBar
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.main);
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我有处理左侧菜单点击的SampleListFragment.java 。
public class SampleListFragment extends ListFragment
{
private ExampleActivity mActivity;
@Override
public void onResume()
{
super.onResume();
mActivity = (ExampleActivity)getActivity();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
Fragment frag = null;
switch(position)
{
case 0:
frag = new FirstFragment();
break;
case 1:
frag = new SecondFragment();
break;
}
if (frag != null)
replaceFragment(frag);
}
protected void replaceFragment(Fragment frag)
{
FragmentTransaction fragmentTransaction = mActivity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main, frag);
fragmentTransaction.commit();
mActivity.showAbove();
}
/**
* A callback function, executed when this fragment is attached to an
* activity
*/
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.list, null);
return v;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++)
{
adapter.add(new SampleItem(new Fragment(), "Sample List",
android.R.drawable.btn_star));
}
setListAdapter(adapter);
}
private class SampleItem
{
public Fragment frag;
public String tag;
public int iconRes;
public SampleItem(Fragment fram, String tag, int iconRes)
{
this.frag = frag;
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem>
{
public SampleAdapter(Context context)
{
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.row, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.row_icon);
TextView title = (TextView) convertView
.findViewById(R.id.row_title);
convertView.setTag(title.getText().toString());
return convertView;
}
}
}
那么会发生什么,当我单击左侧菜单上的第一项时,调用 FirstFragment(),显示 toast 但布局未添加到活动中。
这是 FirstFragment 类的代码:
public class FirstFragment extends Fragment
{
private ExampleActivity mActivity;
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View fragmentView = inflater.inflate(R.layout.firstfragment, container, false);
TextView textView = (TextView) fragmentView.findViewById(R.id.textview);
textView.setText("Lorem ipsum");
return fragmentView;
}
@Override
public void onResume()
{
super.onResume();
mActivity = (ExampleActivity)getActivity();
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "First Fragment", Toast.LENGTH_SHORT).show();
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
}
}
这是main.xml的ExampleActivity.java的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<Button
android:text="Random Button!"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
问题
如何将第一个片段布局添加到活动中。它一直显示初始布局,但片段的布局没有加载到活动中。
谢谢你。