使用片段的正确方法是什么?我应该每个接口只使用一个FragmentActivity还是对所有接口使用独特的FragmentActivity?
我的应用程序非常简单:它有4个不同的界面,顶部有固定的选项菜单,我只在名为"actions"的界面中显示上下文菜单.
我想只使用一个FragmentActivity,我可以在片段之间切换.这是一个好习惯还是我应该为每个接口保留一个FragmentActivity?
到现在为止,我每个界面都使用一个FragmentActivity,但是当我更改(启动)FragmentActivity时,看起来这就是创建一个新对象,我的应用程序在打开新活动之前会闪烁.
预先感谢.
编辑:
这是我的应用程序的绘图:http://postimg.org/image/mvxyk2527/
我想检查Map中是否存在键,如果存在,我想在值上增加1.如果不存在,我想在Map上放一个新项目,其值等于1.
什么是'功能方式'来做到这一点?我用find和fold写了它,但它看起来有点奇怪.
val updatedScore = currentScores
.find(s => s._1.equals(score))
.fold(score -> 1)(s => s._1 -> (s._2 + 1))
val newScores = currentScores + updatedScore
Run Code Online (Sandbox Code Playgroud)
任何人都有更好的解决方案来做同样的事情?
我需要获得一个应该在某些输入和之间填充日期时间间隔的集合LocalDatetime.now.
例:
如果我要建立一个基于每30分钟的间隔,它应该是这样的2017-06-12 00:30, 2017-06-12 01:00, 2017-06-12 01:30, 2017-06-12 02:00..and go on.
我编写了一个符合我要求的代码,但它看起来像是使用Scala语法的Java - 而不是一种功能更清晰的方式.
val now = LocalDateTime.now
val dates = if (interval.toInt > 0) {
var tempDate = LocalDate.now.atStartOfDay()
val allDates = scala.collection.mutable.ListBuffer[String]()
while (tempDate.isBefore(now)) {
allDates += tempDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
tempDate = tempDate.plusMinutes(interval.toInt)
}
allDates
} else {
var initialDay = LocalDate.now.minusDays(30)
val allDates = scala.collection.mutable.ListBuffer[String]()
while (initialDay.isBefore(LocalDate.now)) {
allDates += initialDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
initialDay = initialDay.plusDays(1)
}
allDates
}
Run Code Online (Sandbox Code Playgroud)
如何重构此代码看起来像功能样式或至少删除该可变变量?
我开发了一个Android应用程序(对我来说)它太丑了,我很确定我的方法是非常错误的.
我有一堆片段活动和很多类,如异步任务,业务规则等.特别是,我有一个名为PropertiesReader的类,我用它来读取属性文件.我在很多地方使用这个类,比如片段和业务规则.
public class PropertyReader {
private Properties properties;
public PropertyReader(Context context){
super();
try {
properties = new Properties();
properties.load(context.getResources().getAssets().open("badass.properties"));
} catch (IOException e){
Log.e("Error", "Error opening properties file", e);
}
}
public String getValue(String key){
return properties.getProperty(key);
}
}
Run Code Online (Sandbox Code Playgroud)
在我使用这个课程的每个地方,我做的事情如下:
PropertyReader bla = new PropertyReader(this); //or new PropertyReader(context);
Run Code Online (Sandbox Code Playgroud)
我想知道什么是使用需要构造Context的类的最佳方法.在我看来,每个构造函数都有一个Context参数非常难看.
有任何想法吗?
预先感谢.
我正在尝试做一个尾递归方法,但我正在使用Map,我不知道如何使用模式匹配来检查Map是否为空/ null并得到head/tail:
def aa(a:Map[String, Seq[Operation]]): Map[String, (Seq[Operation], Double)] = {
def aaRec(xx:Map[String, Seq[Operation]],
res:Map[String, (Seq[Operation], Double)],
acc:Double = 0): Map[String, (Seq[Operation], Double)] = xx match {
case ? =>
res
case _ =>
val head = xx.head
val balance = head._2.foldLeft(acc)(_ + _.amount)
aaRec(xx.tail, res + (head._1 -> (head._2, balance)), balance)
}
aaRec(a, Map[String, (Seq[Operation], Double)]())
}
}
Run Code Online (Sandbox Code Playgroud)
案例空地图和案例h :: t的正确语法是什么?
提前致谢
我有以下对象结构:
case class Node(id:Int,children:List[Node])
Run Code Online (Sandbox Code Playgroud)
例:
NodeA
id: 1
children:[
NodeA1:
id: 2
children:
NodeA11
...
]
NodeB1:
id: 3
children:[]
NodeC1:
id: 17
children: [
NodeC11:
id:11
children:[
NodeC111:
id: 19
children: []
]
]
...
Run Code Online (Sandbox Code Playgroud)
我想创建一个递归循环来获取具有特定Id的Node,但是如果找不到iD且对象在子列表上有任何对象,我就会陷入如何继续运行功能的问题.我的函数只能用于获取第一个节点(例如:Id = 1).
这是我正在尝试做的事情:
def getNode(id:Int, node:Node) : Node = {
var result:Node = null
if(node.id == id){
return node
} else if(node.children.size > 0 ){
for(children <- node.children){
result = getNode(id, children)
if(result.id == id){
return result
}
}
}
return result
}
Run Code Online (Sandbox Code Playgroud)