4

我整天都被困在这个问题上,我似乎无法在这里找到适合我的情况的答案,足以让我使用。

这是我的用户界面,选项卡和列表视图功能很好:https ://i.stack.imgur.com/84kRY.png

当我单击一个列表项(如蓝色)时,我希望它以纵向显示一个新片段,即蓝屏,而在横向模式下,它会在一半屏幕上显示该片段,而在另一个屏幕上显示列表视图。我正在使用 ActionBarSherlock 和片段来完成此操作。

我从内到外构建它,所以我从一个列表开始,它按我想要的方式工作。然后我添加了选项卡、寻呼机功能和其他列表。在那个过程中的某个地方,显示的片段停止工作,每次我点击任何东西都会崩溃。

这是我的 ListView 活动之一的可点击部分

public class TitlesFragment extends SherlockListFragment{

boolean mDualPane;
int mCurCheckPosition = 0;

    .
    .
    .

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    showDetails(position);
}

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;
}
}

这是蓝色班

public class Blue extends SherlockFragment{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.blue, container, false);
}

}

蓝色布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000FF"
android:orientation="vertical" >

</LinearLayout>

我希望它以横向替换的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:id="@+id/fragment_layout_support_land" >

<fragment
    android:id="@+id/titles_land"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="***.TitlesFragment" />

<fragment
    android:id="@+id/details"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="2" />

</LinearLayout>

以及纵向替换的布局

<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" >

<fragment
    android:id="@+id/titles"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="***.TitlesFragment" />

</FrameLayout>

最后是我的 LogCat

LogCat 截图在这里

我使用了一个教程来了解之前工作的片段。我无法发布更多链接,但您可以在 youtube 上找到它,名为 ListFragment Tutorial Part 2。

在这一点上,我对任何事情都持开放态度,包括从头开始和以不同的方式编写代码,但我更想了解这里发生了什么以及为什么,所以我可以从中学习。谢谢!

编辑 尝试了一些新的东西并得到了一个新的错误

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    showDetails(position);
}

void showDetails(int position){
    SherlockFragment newContent = new SherlockFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_layout_support, newContent);
    transaction.commit();
    
    
    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;
            
    
    } transaction.add(R.id.fragment_layout_support_land, 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;
        
        
    }transaction.replace(R.id.fragment_layout_support, newContent);
    }
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.addToBackStack(null);
    transaction.commit();
    return;
}
}

新的 LogCat

    FATAL EXCEPTION: main java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at [package].TitlesFragment.showDetails(TitlesFragment.java:97)
at [package].TitlesFragment.onListItemClick(TitlesFragment.java:44)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
at android.widget.AbsListView$1.run(AbsListView.java:3463)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

7

您应该将您的肖像 xml 更改为:

<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" >

    <FrameLayout
        android:id="@+id/titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</FrameLayout>

在你MainActivityonCreate方法中,输入:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    TitlesFragment title = new TitlesFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.titles, title).commit();
}

请改回代码TitlesFragment 看我的showDetails下面。

注意:对于风景,我希望你能自己弄清楚。您当前的处理方式land-portrait,IMO 不是最好的方法。阅读布局别名了解更多详细信息。

额外:您不必创建Blue, Green, Red,Yellow类。您可以创建一个Rainbow类。

public class Rainbow extends SherlockFragment {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.rainbow, container, false);

        // default color
        int color = Color.WHITE;

        // get the color if provided through setArguments(Bundle args)
        if (getArguments() != null) {
            color = getArguments().getInt("color");
        }

        view.findViewById(R.id.rainbow).setBackgroundColor(color);

        return view;
    }
}

将您更改showDetails为:

 void showDetails(int position) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    int color;
    switch (position) {
    case 0:
        color = Color.BLUE;
        break;
    case 1:
        color = Color.RED;
        break;
    default:
        color = Color.WHITE;
    }

    Rainbow rainbow = new Rainbow();
    Bundle arguments = new Bundle();
    arguments.putInt("color", color);
    rainbow.setArguments(arguments);

    ft.replace(R.id.titles, rainbow);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(null);
    ft.commit();
    return;
}
于 2013-09-16T03:48:32.303 回答