2017-04-17 82 views
0

我想從活動頁面移動到片段。 在從活動頁面點擊按鈕,我如何可以移動到片段我嘗試使用意圖,但它不工作有人能幫助我,請...如何從活動頁面移動到Android中的片段

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"); 

    } 
} 
+0

在開始處理片段之前,您應該閱讀本文(https://developer.android.com/guide/components/fragments.html) –

回答

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(); 
     } 
    }); 
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); 
     }