如何从图像视图onclicklistener调用其他片段

时间:2016-01-11 10:04:01

标签: java android android-fragments onclicklistener

我想从图像视图点击监听器调用类FragmentDiagnosis。我对如何从点击监听器调用其他类感到困惑?我尝试过使用意图,但是有错误。对于任何了解此问题的人,请帮助我。我将不胜感激,非常感谢你。

package com.pakarayam;
import android.app.Fragment;

public class FragmentHome extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater
            .inflate(R.layout.activity_fragment_home, container, false);
    configureImageView(view);
    return view;
}
private void configureImageView(View view) {
// TODO Auto-generated method stub
ImageView mulai = (ImageView) view.findViewById(R.id.mulai);
mulai.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent iMulai = new Intent(getApplicationContext(), FragmentDiagnosis.class);
        startActivity(iMulai);
}});
}}

2 个答案:

答案 0 :(得分:0)

试试这个:

public void onClick(View v)
{
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    YourFragment yourFragment= new YourFragment();
    fragmentTransaction.add(R.id.your_activity_id, yourFragment, "FRAGMENT");
    fragmentTransaction.commit();
}

答案 1 :(得分:0)

 @SuppressLint("NewApi")
    public void addFragment(Fragment fragment, Bundle bundle) {
        try {
            if (bundle != null) {
                fragment.setArguments(bundle);
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_left,
                    R.anim.slide_right, R.anim.slide_left, R.anim.slide_right);
            fragmentTransaction.add(R.id.frame1, fragment).addToBackStack(null)
                    .commit();

        } catch (Exception e) {
            Log.e("MenuChangeActivity", "Add fragment error");
        }
    }