如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23212162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move from one fragment to another fragment on click of an ImageView in Android?
提问by anu_r
I have an ImageView. I want to move from one fragment to another fragment on a click of an Imageview, the same way like we can move from one activity to another using
我有一个 ImageView。我想在单击 Imageview 时从一个片段移动到另一个片段,就像我们可以使用从一个活动移动到另一个活动一样
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
How can I do this? Can anyone explain to me step by step?
我怎样才能做到这一点?谁能一步一步向我解释?
My codes are as follows:
我的代码如下:
mycontacts.class
mycontacts.class
public class mycontacts extends Fragment {
public mycontacts() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = super.getView(position, convertView, parent);
ImageView purple=(ImageView)v.findViewById(R.id.imageView1);
purple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//how to go to tasks fragment from here???
}
});
return view;
}
}
tasks.class
任务类
public class tasks extends Fragment {
public tasks() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_one, container,
false);
return view;
}
}
回答by GvSharma
purple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new tasks();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
you write the above code...there we are replacing R.id.content_frame with our fragment. hope this helps you
你写了上面的代码......我们正在用我们的片段替换 R.id.content_frame 。希望这对你有帮助
回答by Ashok Singhal
you can move to another fragment by using the FragmentManager transactions. Fragment can not be called like activities,. Fragments exists on the existance of activities.
您可以使用 FragmentManager 事务移动到另一个片段。Fragment 不能称为活动之类的。片段存在就存在活动。
You can call another fragment by writing the code below:
您可以通过编写以下代码来调用另一个片段:
FragmentTransaction t = this.getFragmentManager().beginTransaction();
Fragment mFrag = new MyFragment();
t.replace(R.id.content_frame, mFrag);
t.commit();
here "R.id.content_frame" is the id of the layout on which you want to replace the fragment.
这里的“R.id.content_frame”是您要替换片段的布局的 ID。
you can also add the other fragment incase of replace.
您还可以添加其他片段以防替换。
回答by t_mo_t
inside your onClickListener.onClick, put
在你的 onClickListener.onClick 里面,把
getFragmentManager().beginTransaction().replace(R.id.container, new tasks()).commit();
In another word, in your mycontacts.class
换句话说,在你的 mycontacts.class 中
public class mycontacts extends Fragment {
public mycontacts() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = super.getView(position, convertView, parent);
ImageView purple = (ImageView) v.findViewById(R.id.imageView1);
purple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new tasks())
.commit();
}
});
return view;
}
}
now, remember R.id.container
is the container (FrameLayout or other layouts) for the activity that calls the fragment
现在,记住R.id.container
是调用片段的活动的容器(FrameLayout 或其他布局)
回答by Saqib Javed
Add this code where you want to click and load Fragment. I hope it's work for you.
将此代码添加到要单击并加载Fragment 的位置。我希望它对你有用。
Fragment fragment = new yourfragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();