[英]Android Studio Fragment with custom fragment class object
我剛剛在我的應用程序中創建了一個片段,其中包含一些控件。 原則上,我希望這些控件通過 tcp 客戶端發送它們的當前數據,該客戶端具有與 tcp 套接字相關的方法TcpClient::sendMessage
。 片段本身包含一個接口,用於創建對保存片段的MainActivity
的回調。 這就是我將片段附加到我的MainActivity
的方式:
FragmentControls fragmentControls = new FragmentControls((command, message) -> tcp_client.sendMessage(command, message));
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.fragment_pi_connected, FragmentControls.class, null)
.commit();
我如何設法在第一行使用初始化的 object fragmentControls
創建片段,以實現接口 function?
為了完整起見,我的片段 class:
public class FragmentControls extends Fragment {
public CommandSendInterface commandSendInterface;
public FragmentControls(CommandSendInterface sendCommandInterface) {
super(R.layout.maunal_pi_controls);
commandSendInterface = sendCommandInterface;
}
// Some stuff that handles the View Objects in the Fragment and treats their events...
public interface CommandSendInterface{
void onCommandSend(ServerCommands command, int message);
}
}
應該剛剛傳遞 object 作為參數而不是 .class 值,現在它完美地工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.