Last active
August 29, 2015 13:56
-
-
Save Yosuke-Kawakami/8861216 to your computer and use it in GitHub Desktop.
Fragment の宿題消化。縦画面時は詳細が出力されて、一定以上の解像度かつ横画面時には 2 ペインにする(縦画面時にリストを表示するわけではないのがシャレオツ)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string-array name="lists"> | |
<item>ふう</item> | |
<item>たあ</item> | |
<item>ばあ</item> | |
</string-array> | |
<string-array name="list_values"> | |
<item>FOO</item> | |
<item>TAR</item> | |
<item>BAA</item> | |
</string-array> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout 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:baselineAligned="false" | |
android:orientation="horizontal" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" > | |
<!-- | |
fragment にするとリスト選択後の画面回転時に落ちる。 | |
FrameLayout にしても FragmentManager 経由でアクセスできるから代替可能らしいが、腑に落ちない | |
--> | |
<FrameLayout | |
android:id="@+id/f_detail" | |
android:name="org.example.fortesting2.MainFragment" | |
android:layout_width="0dip" | |
android:layout_height="match_parent" | |
android:layout_weight="1" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/body" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerInParent="true" | |
android:gravity="center" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.fortesting2; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.res.Configuration; | |
import android.util.DisplayMetrics; | |
import android.view.Menu; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
if(!isTowPane()){ | |
// do something. | |
// アクションバーとか? | |
} | |
} | |
/** | |
* 解像度が一定値以上かつディスプレイの状態が縦の場合に真を返す関数 | |
* @return | |
*/ | |
private boolean isTowPane(){ | |
int DISP_WIDTH = 800; | |
DisplayMetrics displayMetrics = new DisplayMetrics(); | |
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); | |
Configuration configuration = getResources().getConfiguration(); | |
return (displayMetrics.widthPixels >= DISP_WIDTH) && (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); | |
} | |
/** | |
* | |
* | |
* | |
* | |
*/ | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.fortesting2; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class MainFragment extends Fragment{ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.main_fragment, container, false); | |
TextView textView = (TextView)view.findViewById(R.id.body); | |
if(getArguments() != null){ | |
String[] tmp = getResources().getStringArray(R.array.list_values); | |
textView.setText(tmp[getArguments().getInt("key")]); | |
} | |
return view; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example.fortesting2; | |
import android.app.FragmentManager; | |
import android.app.FragmentTransaction; | |
import android.app.ListFragment; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
public class MyListFragment extends ListFragment{ | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.lists, android.R.layout.simple_list_item_1); | |
setListAdapter(arrayAdapter); | |
} | |
@Override | |
public void onListItemClick(ListView listView, View view, int position, long id) { | |
super.onListItemClick(listView, view, position, id); | |
MainFragment mainFragment = new MainFragment(); | |
Bundle bundle = new Bundle(); | |
bundle.putInt("key", position); | |
mainFragment.setArguments(bundle); | |
FragmentManager fragmentManager = getFragmentManager(); | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.replace(R.id.f_detail, mainFragment); | |
fragmentTransaction.commit(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout 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:baselineAligned="false" | |
android:orientation="horizontal" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" > | |
<!-- | |
ここは fragment じゃないと駄目みたい。layout ファイルを経由してないからかな? | |
--> | |
<fragment | |
android:id="@+id/f_list" | |
android:name="org.example.fortesting2.MyListFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="3" /> | |
<FrameLayout | |
android:id="@+id/f_detail" | |
android:name="org.example.fortesting2.MainFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="1" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment