Scala中的Programming中有以下示例:
<FrameLayout 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.example.nirvan.fragment_example.fragmentOne">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_gravity="left|top" />
</FrameLayout>
只打印苹果和橙色,因为无与某些(f)不匹配。
按照这个例子,我希望这次打印“无”:
import android.support.v4.app.Fragment;
import android.app.ListFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void FragmentOneClick(View v)
{
ListFragment myFragment=new fragmentOne();
FragmentManager fm= getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_switch, (android.app.Fragment) myFragment );
ft.commit();
}
public void FragmentTwoClick(View v)
{
Fragment myFragment=new fragmentTwo();
FragmentManager fm= getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.fragment_switch, myFragment);
ft.commit();
}
}
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.fragment_example.MainActivity">
<Button
android:onClick="FragmentOneClick"
android:text="Fragment1"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:layout_weight="2"
>
</Button>
<Button
android:text="Fragment2"
android:id="@+id/button2"
android:onClick="FragmentTwoClick"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:layout_alignTop="@+id/button1"
android:layout_alignRight="@+id/fragment_switch"
android:layout_alignEnd="@+id/fragment_switch">
android:layout_weight="2"
</Button>
<fragment
android:id="@+id/fragment_switch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/button1"
android:name="com.example.nirvan.fragment_example.fragmentOne"
tools:layout="@layout/fragment_fragment_one" />
</RelativeLayout>
但事实证明它打印出来了:
val fruits = List(Some("apple"), None, Some("orange"))
for(Some(f) <- fruits) println(f)
有谁可以解释我在这里缺少的东西?