2014-09-11 10 views

回答

0

我會減少我的例子在一個按鈕,你應該能夠乘以它。我也切出unneccessarry東西像利潤率等等.​​.....如果你有問題,只是告訴我,我會很樂意幫助你:

MainActivity.java - openHome方法很有趣

public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener 
{ 
    FragmentManager fragmentManager = getFragmentManager(); 

    HomeFragment homeFragment; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     homeFragment = new HomeFragment(); 

     fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit(); 
    } 

    public void openHome(View view) 
    { 
     homeFragment = new HomeFragment(); 
     fragmentManager.beginTransaction().replace(R.id.mainFrame, homeFragment).commit(); 
    } 

    @Override 
    public void onFragmentInteractionHome(Uri uri) 
    { 
     Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();//only to check, can be removed (the content, the method must be implemented!!!!!!!!!!!!!!!) 
    } 
} 

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    tools:ignore="MergeRootFrame" > 

    <FrameLayout 
     android:id = "@+id/mainFrame" 
     android:layout_width = "match_parent" 
     android:layout_height = "match_parent" 
     android:layout_marginBottom = "@dimen/bottom_Main_Tabs"> 
     </FrameLayout> 

    <LinearLayout 
     android:layout_width = "match_parent" 
     android:layout_height = "@dimen/bottom_Main_Tabs" 
     android:layout_gravity = "bottom" 
     > 

     <ImageButton 
      android:id = "@+id/bottomButton_home" 
      android:layout_height = "match_parent" 
      android:layout_width = "0dp" 
      android:layout_weight = "1.0" 
      android:background = "@drawable/ic_home_white" 
      android:onClick = "openHome" 
      /> 
    </LinearLayout> 
</FrameLayout> 

HomeFragment.java - 如果你創建一個片段,這一切都將被自動生成加評論,這是我切出!!!有趣的是,只有在底部的內部接口!!!:

import android.app.Activity; 
import android.app.Fragment; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 


public class HomeFragment extends Fragment 
{ 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public static HomeFragment newInstance(String param1, String param2) 
    { 
     HomeFragment fragment = new HomeFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public HomeFragment() 
    { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) 
     { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_home, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) 
    { 
     if (mListener != null) 
     { 
      mListener.onFragmentInteractionHome(uri); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) 
    { 
     super.onAttach(activity); 
     try 
     { 
      mListener = (OnFragmentInteractionListener) activity; 
     } 
     catch (ClassCastException e) 
     { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() 
    { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener 
    { 
     // TODO: Update argument type and name 
     public void onFragmentInteractionHome(Uri uri); 
    } 

} 

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/home_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue" 
    tools:context="com.domain.app.HomeFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:textSize="100sp" 
     android:text="Home" /> 

</FrameLayout> 

我tthink僅此而已。如果不是,請告訴我:)

+0

是的,想打開片段,當我點擊標籤,但我認爲標籤的定義應該在佈局文件中,這意味着不要以編程方式創建按鈕。 – redrom 2014-09-11 11:08:10

+0

好的,我會編輯我的答案並用代碼填充它。這將是我做到這一點的方式。今天剛剛工作:D – JRsz 2014-09-11 12:00:14

+0

我編輯了答案,我希望這有助於。如果沒有,請告訴我,如果它確實如此,請注意它:D – JRsz 2014-09-11 12:22:13