1

我正在阅读开发人员网站上有关 Fragment 的文档。我创建了一个针对 api 8:android 2.2 的新项目。我安装了 Android 支持库,并将 android 支持库添加到项目中。我开始实现辅助方法 showDetails,我得到红色波浪线表示“a_item 无法解析”。我不记得说创建 a_item 变量或添加带有 a_item id 的布局的指令。此外,在详细活动中,我也收到一条错误消息。(getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();) 它说“FragmentTransaction 类型中的方法 add(int, Fragment) 不适用于参数 (int ,详细信息片段)”。我是否遗漏了说明中的某些内容?

这是我的示例代码:

package com.example.fragmentlayout;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;

public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getResources().getConfiguration().ORIENTATION_LANDSCAPE == Configuration.ORIENTATION_LANDSCAPE )
        {
            finish();
            return;
        } 
        if(savedInstanceState == null)
        {
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }

}

这是标题片段:

package com.example.fragmentlayout;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TitlesFragment extends ListFragment {
   boolean mDualPane;
   int mCurCheckPosition =0;
    String[] titles = {"Title 1", "Title 2", "Title 3"};
    String[] Details = {"Details for one", "Details for two", "Details for Three"};

    @SuppressLint("InlinedApi")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, titles));

        View detailsFrame = getActivity().findViewById(R.id.fragment2);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        if(savedInstanceState != null)
        {
            mCurCheckPosition = savedInstanceState.getInt("curChoise", 0);
        }

        if(mDualPane)
        {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(mCurCheckPosition);
        }
    }

   private void showDetails(int index) {

        mCurCheckPosition = index;
        if(mDualPane)
        {
            getListView().setItemChecked(index, true);
            DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.fragment2);
            if(details == null || details.getShownIndex() != index)
            {
                details = DetailsFragment.newInstance(index);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if(index ==0)
                {
                    ft.replace(R.id.fragment2, details);
                } else
                {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }

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



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


}

这是详细信息片段:

package com.example.fragmentlayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class DetailsFragment extends Fragment {
    String[] Details = {"Details for one", "Details for two", "Details for Three"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if(container == null)
        {
            return null;
        }
        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Details[getShownIndex()]);
        return scroller;

    //  return super.onCreateView(inflater, container, savedInstanceState);
    }

    public static DetailsFragment newInstance(int index)
    {
        DetailsFragment f = new DetailsFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex()
    {
        return getArguments().getInt("index", 0);
    }
}

这是陆地和纵向布局

<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:orientation="horizontal">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentlayout.TitlesFragment"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentlayout.DetailsFragment"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>


<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"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentlayout.TitlesFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
4

3 回答 3

1

也许,这是一个错误。答案在这里 https://code.google.com/p/android/issues/detail?id=54837

链接消息的作者建议更换代码片段

if (index == 0) {
    ft.replace(R.id.details, details);
} else {
    ft.replace(R.id.a_item, details);
}

带有代码片段

if (index == 0) {
    ft.add(R.id.details, details);
} else {
    ft.replace(R.id.details, details);
}

或者也许只是

ft.replace(R.id.details, details);
于 2017-01-04T20:47:36.830 回答
0
  1. 您的 DetailsActivity 应该扩展 FragmentActivity

  2. 您应该使用 getSupportFragmentManager()

  3. 如果您将片段嵌套到其他片段中,请考虑使用 getChildFragmentManager()

    (好的,我没有查看所有代码,所以不确定 3 是否适用)

于 2013-06-06T04:00:58.413 回答
-1

您需要使用 getSupportFragmentManager 而不是 getFragmentManager,因为您使用的是支持片段。

R.id.a_item 引用布局文件中布局的 id。而不是 android:id="@+id/fragment1",让它成为 android:id="@+id/a_item"

于 2013-06-06T03:29:49.943 回答