2017-08-16 31 views
-2

我正在製作一個應用程序,其中需要監聽片段小部件上的單擊事件。 在我的應用程序中: 我將我的活動的默認片段設置爲fragmentOne,並且我想在有人單擊fragmentOne中的某個小部件時更改活動的片段,我想將片段的活動從fragmentOne更改爲fragmentTwo。 這裏是我的主要活動的XML代碼:如何在android中的活動中偵聽片段中的點擊?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.wordpress.tbkj.app_name.LoginSignupActivity"> 


    <fragment 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/login_signup_fragment" 
     android:name="com.wordpress.tbkj.app_name.LoginFragment" 
     tools:layout="@layout/fragment_login" /> 
</RelativeLayout> 

這裏是我的活動代碼:

public class LoginSignupActivity extends AppCompatActivity { 

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

當我啓動這一活動fragmenOne是可見的。 如果有人想要其他代碼,請發表評論。

+0

你能分享一些代碼嗎?在片段中,您還可以在視圖上使用setOnClickListener方法。 – AppPhil

回答

0

您應該定義一個活動實現的接口。例如:

public interface CoolFragmentListener { 
    void someCoolWidgetActionClicked(); 
} 

public class LoginSignupActivity extends AppCompatActivity implements CoolFragmentListener { 

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

    public void someCoolWidgetActionClicked() { 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

     // Use the new instance pattern instead of directly instantiating your fragment. 
     YourNewCoolFragment fragment = YourNewCoolFragment.newInstance(); 
     fragmentTransaction.add(R.id.container, fragment); 
     fragmentTransaction.commit(); 
    } 

} 

在片段中,您將擁有一個實現「CoolFragmentListener」接口的對象。這通常在onAttach生命週期中完成。

​​

一兩件事,而不是把你共享代碼的活動的佈局裏面的片段,你應該有一個容器認爲FragmentManager可以添加新的片段插入。例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.wordpress.tbkj.app_name.LoginSignupActivity"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     /> 

</RelativeLayout> 

這意味着您將不得不以編程方式添加第一個片段。

0

有一個很好的描述在https://developer.android.com/guide/components/fragments.html。在我看來,片段的轉換很好地描述如下:

在您的活動正在運行的任何時候,您都可以爲活動佈局添加片段。您只需指定放置片段的V​​iewGroup即可。 要在活動中創建片段事務(例如添加,移除或替換片段),您必須使用FragmentTransaction中的API。您可以從您的活動得到FragmentTransaction的一個實例是這樣的:

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction 
=fragmentManager.beginTransaction(); 

然後你可以使用add()方法中添加一個片段,指定要添加的片段,並在其中插入它的視圖。例如:

ExampleFragment fragment = new ExampleFragment(); 
fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.commit(); 

傳遞到添加的第一個參數()是的ViewGroup其中所述片段應放置,由資源ID指定的,並且所述第二參數是添加片段。

相關問題