如何在Android中点击ImageView从一个片段移动到另一个片段? [英] How to move from one fragment to another fragment on click of a ImageView in Android?
本文介绍了如何在Android中点击ImageView从一个片段移动到另一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ImageView。我想从一个片段移动到另一个片段,点击一个Imageview,就像我们可以从一个活动移动到另一个活动一样
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
我该怎么做?任何人都可以向我解释一步一步吗?
我的代码如下:
mycontacts。 class
public class mycontacts extends Fragment {
public mycontacts(){
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundled savedInstanceState){
final View v = super.getView(position,convertView,parent);
ImageView purple =(ImageView)v.findViewById(R.id.imageView1);
purple.setOnClickListener(new OnClickListener(){
$ b $ @Override
public void onClick(View v){
// TODO自动生成的方法存根
//如何从这里去任务片段???
}
});
返回视图;
$ b code
$ b tasks.class
public class tasks extends Fragment {
public tasks(){
}
@Override
public view onCreateView(LayoutInflater inflater,ViewGroup container,
Bundled savedInstanceState){
View view = inflater.inflate(R.layout.fragment_layout_one,容器,
false);
返回视图;
$ / code $ / pre
解决方案
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();
}
});
你写上面的代码...在那里,我们用我们的片段替换R.id.content_frame。
希望这可以帮助你
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
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
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;
}
}
解决方案 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
这篇关于如何在Android中点击ImageView从一个片段移动到另一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文