Java onListItem单击打开新片段
我正在使用“动作栏夏洛克”和“滑动菜单”库。 我有一个5个项目的列表,我想在单击列表中的项目时更改片段。 这是到目前为止我的代码,我试图使用OnListItemClick,但我真的不知道如何使用它Java onListItem单击打开新片段,java,android,android-fragments,Java,Android,Android Fragments,我正在使用“动作栏夏洛克”和“滑动菜单”库。 我有一个5个项目的列表,我想在单击列表中的项目时更改片段。 这是到目前为止我的代码,我试图使用OnListItemClick,但我真的不知道如何使用它 public class RandomList extends SherlockListFragment { String[] list_contents = {"Page 1", "Page 2", "Page 3", "Page 4", "Page 5" }; @Overrid
public class RandomList extends SherlockListFragment {
String[] list_contents = {"Page 1", "Page 2", "Page 3", "Page 4", "Page 5" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list_contents));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// **Open different fragment after click**
}
}
public类RandomList扩展了SherlockListFragment{
字符串[]列表内容={“第1页”、“第2页”、“第3页”、“第4页”、“第5页”};
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//TODO自动生成的方法存根
//返回super.onCreateView(充气机、容器、savedInstanceState);
返回充气机。充气(右布局列表,容器,假);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onActivityCreated(savedInstanceState);
setListAdapter(新的ArrayAdapter(getActivity()),
android.R.layout.simple_list_item_1,list_contents));
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
//**单击后打开不同的片段**
}
}
如果其中一个(我们称之为MyFragment)与滑动菜单中的第一项相关:
public class MyFragment extends Fragment {
public static Fragment newInstance(...) {
MyFragment f = new MyFragment();
// Since fragments require that you have a
// public constructor with zero arguments, then
// we use this pattern to initialize the fragment.
...
return f;
}
...
}
您可以使用switch语句:
void callFragmentFromDrawer(int position) {
Fragment f = null;
switch (position) {
case 0:
f = MyFragment.newInstance();
break;
case 1:
...
}
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, f)
.addToBackStack(list_contents[position])
.commit();
}
}
那么
如果其中一项(我们称之为MyFragment)与滑动菜单中的第一项相关:
public class MyFragment extends Fragment {
public static Fragment newInstance(...) {
MyFragment f = new MyFragment();
// Since fragments require that you have a
// public constructor with zero arguments, then
// we use this pattern to initialize the fragment.
...
return f;
}
...
}
您可以使用switch语句:
void callFragmentFromDrawer(int position) {
Fragment f = null;
switch (position) {
case 0:
f = MyFragment.newInstance();
break;
case 1:
...
}
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, f)
.addToBackStack(list_contents[position])
.commit();
}
}
那么
如果其中一项(我们称之为MyFragment)与滑动菜单中的第一项相关:
public class MyFragment extends Fragment {
public static Fragment newInstance(...) {
MyFragment f = new MyFragment();
// Since fragments require that you have a
// public constructor with zero arguments, then
// we use this pattern to initialize the fragment.
...
return f;
}
...
}
您可以使用switch语句:
void callFragmentFromDrawer(int position) {
Fragment f = null;
switch (position) {
case 0:
f = MyFragment.newInstance();
break;
case 1:
...
}
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, f)
.addToBackStack(list_contents[position])
.commit();
}
}
那么
如果其中一项(我们称之为MyFragment)与滑动菜单中的第一项相关:
public class MyFragment extends Fragment {
public static Fragment newInstance(...) {
MyFragment f = new MyFragment();
// Since fragments require that you have a
// public constructor with zero arguments, then
// we use this pattern to initialize the fragment.
...
return f;
}
...
}
您可以使用switch语句:
void callFragmentFromDrawer(int position) {
Fragment f = null;
switch (position) {
case 0:
f = MyFragment.newInstance();
break;
case 1:
...
}
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, f)
.addToBackStack(list_contents[position])
.commit();
}
}
那么
这与SlidingMenu的作者在演示应用程序中所做的差不多,尽管您的解决方案实际上有点简洁。我认为它需要一个getActivity()和一个cast来获得FM。哦,对了。我忘了我们在一个ListFragment中:-P更新以反映这一事实,方法是在getSupportFragmentManager()之前调用getActivity()。对不起,MyFragment从哪来,在这一行f=MyFragment.newInstance();谢谢该模式在书中被称为“静态工厂方法”。我已经更新了答案,加入了它的使用示例。你们也可以在下面的例子中找到,这和SlidingMenu的作者在演示应用程序中所做的差不多,尽管你们的解决方案实际上有点简洁。我认为它需要一个getActivity()和一个cast来获得FM。哦,对了。我忘了我们在一个ListFragment中:-P更新以反映这一事实,方法是在getSupportFragmentManager()之前调用getActivity()。对不起,MyFragment从哪来,在这一行f=MyFragment.newInstance();谢谢该模式在书中被称为“静态工厂方法”。我已经更新了答案,加入了它的使用示例。你们也可以在下面的例子中找到,这和SlidingMenu的作者在演示应用程序中所做的差不多,尽管你们的解决方案实际上有点简洁。我认为它需要一个getActivity()和一个cast来获得FM。哦,对了。我忘了我们在一个ListFragment中:-P更新以反映这一事实,方法是在getSupportFragmentManager()之前调用getActivity()。对不起,MyFragment从哪来,在这一行f=MyFragment.newInstance();谢谢该模式在书中被称为“静态工厂方法”。我已经更新了答案,加入了它的使用示例。你们也可以在下面的例子中找到,这和SlidingMenu的作者在演示应用程序中所做的差不多,尽管你们的解决方案实际上有点简洁。我认为它需要一个getActivity()和一个cast来获得FM。哦,对了。我忘了我们在一个ListFragment中:-P更新以反映这一事实,方法是在getSupportFragmentManager()之前调用getActivity()。对不起,MyFragment从哪来,在这一行f=MyFragment.newInstance();谢谢该模式在书中被称为“静态工厂方法”。我已经更新了答案,加入了它的使用示例。您还可以在下面的