ラベル Fragment の投稿を表示しています。 すべての投稿を表示
ラベル Fragment の投稿を表示しています。 すべての投稿を表示

2015/02/19

DialogFragmentの二重起動を防止する

if (getSupportFragmentManager().findFragmentByTag("dialog") == null) { 
 DialogFragment fragment = new CustomDialog();
 Bundle bundle = new Bundle();
 bundle.putInt("data", data);
 fragment.setArguments(bundle);
 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
 fragment.show(ft, "dialog");
}
support.v4を使わないときはgetSupportFragmentManager()をgetFragmentManager()にする。

2015/02/04

FragmentDialogでカスタムレイアウトを使用した場合、タブレットのlandscapeで表示すると通常の表示の上に回転されたレイアウトが重なって表示される症状

この症状は親レイアウトのwidthとheightが同じ値の場合で、子レイアウトにScrollViewを使用してlayout_heightがmatch_parentのときに発生した。
親レイアウトのwidthをheightより大きくすると症状はでなくなった。

スマホではこの症状にならなかったが念のため横幅を大きくしておいた方がいいかもしれない。

タブレットはNexus7(2012) kitkat 4.4.4
lollipopにはまだアプデしてないので試していない。

2015/02/01

PreferenceFragmentでDialogFragmentを使う

始めはDialogFragmentを別ファイルにしたが、showのところでエラーがでてしまいうまくいかなかっので PreferenceFragmentクラスの中にDialogFragmentクラスを書いたらうまくいった。

DialogFragmentはstaticにする必要がある。

public class SampleFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  addPreferencesFromResource(R.xml.pref);


  PreferenceScreen pref1 = (PreferenceScreen) findPreference("prefkey");
  pref1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
   @Override
   public boolean onPreferenceClick(Preference arg0) {  

    //ダイアログを表示
    DialogFragment fragment = new SampleDialogFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("type", 0);
    fragment.setArguments(bundle);
    FragmentTransaction ft = getFragmentManager().beginTransaction();      
    fragment.show(ft, "dialog");

    return false;
   }
  });

 }


 public static class SampleDialogFragment extends DialogFragment {

  public static DBdialogFragment newInstance(String title, String message) {
   SampleDialogFragmentt frag = new SampleDialogFragment();
   Bundle bundle = new Bundle();
   bundle.putInt("type", 0);
   frag.setArguments(bundle);
   return frag;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
   final int type = getArguments().getInt("type");
   //処理
  }
 }
}



2014/07/25

fragmentを使った場合、android:onClickで呼び出すメソッドはMainActivityの方へ書く。

ボタンをFragment.classのレイアウトで使用している
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton1"
        android:text="click" />
</LinearLayout>

Fragment.classではなくMainActivity.classに

public void onClickButton1(View view){
//処理
}

を書く。
"The specified child already has a parent. You must call removeView() on the child's parent first." というエラーが出た時の対処

View view = inflater.inflate(R.layout.fragment1, container);



View view = inflater.inflate(R.layout.fragment1, container, false);