在Android中单击ImageView时如何从一个片段移动到另一个片段? [英] How to move from one fragment to another fragment on click of an ImageView in Android?
本文介绍了在Android中单击ImageView时如何从一个片段移动到另一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ImageView.我想通过单击Imageview从一个片段移动到另一个片段,就像我们可以使用
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?
我的代码如下:
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();
}
});
您编写了上面的代码...我们正在用片段替换R.id.content_frame. 希望这对您有帮助
you write the above code...there we are replacing R.id.content_frame with our fragment. hope this helps you
这篇关于在Android中单击ImageView时如何从一个片段移动到另一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文