うまくいけば、これは私の鼻のすぐ下に答えがあるばかげた質問ではありません。なぜなら、私はそれを修正するためにさまざまなことを試すのに長い時間を費やしてきたからです。寝る。
後で実際のアプリに追加できるように、UI のねじれを解決するためのサンプル アプリを作成しています。アプリはリストフラグメントを含むタブになり、リストを選択すると、テキストまたは画像を含む新しいフラグメントが開きます。
縦向きではすべてがうまく機能しますが、任意のタブまたはフラグメントから横向きにしようとすると、クラッシュが発生し続けます。私はタブを非難しています.1つのリストの場合、向きの入れ替えはうまくいきました.タブを追加すると、このエラーが表示され始めました.
フラグメントを表示する方法は、xml のフラグメントを置き換えることです。これは、タブが表示される前に正常に機能していたと述べたからです。ただし、向きを切り替えると、横向きの xml が必要になります。問題は、向きが変わったときにタブが新しいレイアウトで適切にリロードされないことにあると思います。私はまだ超初心者なので、onSaveInstanceState を使用していることは知っていますが、それを正しく行う方法や、解決策が異なるかどうかはわかりません。
これがコードです。
ポートレート XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_layout_support"
android:layout_width="match_parent"
android:layout_height="match_parent" >
***id.titles includes the listview, which needs to be replaced in portrait***
<fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent"
class=".TitlesFragment" />
</FrameLayout>
ランドスケープ XML
<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:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:orientation="horizontal"
tools:context=".TitlesFragment" >
***listview on the left***
<fragment
android:id="@+id/titles"
android:name=".TitlesFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
***Details frame for fragments to inflate into on the right***
<FrameLayout
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
ListFragment クラス(両方のリストは類似しています)
public class TitlesFragment extends SherlockListFragment implements ActionBar.TabListener {
boolean mDualPane;
int mCurCheckPosition = 0;
private Fragment mFragment;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter (new ArrayAdapter<String> (getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, ColorList.COLORS));
/* Checking if landscape or not. id.details is unique to landscape */
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null){
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane){
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
/****** This SaveInstanceState did well at changing the orientation before the tabs were added.
* Is there a way to refresh the tabs on the new layout when the orientation changes? */
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
showDetails(position);
}
/* If Landscape, replace details. If not, replace titles, which includes the listview */
void showDetails(int position){
SherlockFragment newContent = new SherlockFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if(mDualPane){
switch(position){
case 0:
newContent = new Blue();
break;
case 1:
newContent = new Red();
break;
case 2:
newContent = new Green();
break;
case 3:
newContent = new Yellow();
break;
case 4:
newContent = new Purple();
break;
} ft.replace(R.id.details, newContent);
} else {
switch(position){
case 0:
newContent = new Blue();
break;
case 1:
newContent = new Red();
break;
case 2:
newContent = new Green();
break;
case 3:
newContent = new Yellow();
break;
case 4:
newContent = new Purple();
break;
}ft.replace(R.id.titles, newContent);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
return;
}
/* Tab functionality */
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = new TitlesFragment();
ft.replace(R.id.titles, mFragment);
//ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
//ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
助けてくれてありがとう!これを投稿した後、立ち去らなければならないことをお詫びしますが、数時間寝てから戻ります.