如何从Activity页面移动到android中的片段

时间:2017-04-17 10:42:48

标签: android android-fragments

我正在尝试从“活动”页面移动到片段。 单击“活动”页面上的按钮如何移动到片段我尝试使用Intent但是它不起作用可以有人帮助我...

Payment.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by reema on 27-Mar-17.
 */
public class Payment extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment);
        Button b1;
        b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                Intent i = new Intent(Payment.this, Student_mycourse.class);
//                startActivity(i);
                Intent intent = new Intent();
                intent.setClass(Payment.this, Student_mycourse.class);
                startActivity(intent);
            }
        });

    }
}

Student_mycourse.java

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;

/**
 * Created by reema on 27-Mar-17.
 */
public class Student_mycourse extends Fragment {
    GridView gv;
    Context context;
    ArrayList prgmName;
    public static String[] prgmNameList = {"Let Us C", "c++"};
    public static int[] prgmImages = {R.drawable.angular, R.drawable.cloud};

    public static String[] values = new String[]{"Course", "Activtiy2"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.my_course_student,container,false);

        custom_student adapter = new custom_student(getActivity(), prgmNameList, prgmImages, values);

        gv = (GridView) v.findViewById(R.id.gridView1);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), StudentAfterPay.class);
position], Toast.LENGTH_SHORT).show();

            }
        });


        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        getActivity().setTitle("My Course");

    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

 b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           Fragment fragment = new Student_mycourse ();
    FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack(null);
        fragmentTransaction.commit();
        }
    });

答案 1 :(得分:0)

 Fragment fragment = null;


 fragment=new Setup_Activity_General();

if ((fragment != null)) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.content_main, fragment);
            fragmentTransaction.commit();

            // set the toolbar title
            getSupportActionBar().setTitle(title);
        }