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

0
  1. DetailsActivity は FragmentActivity を拡張する必要があります

  2. getSupportFragmentManager() を使用する必要があります

  3. フラグメントを他のフラグメントにネストする場合は、getChildFragmentManager() の使用を検討してください

    (わかりました、すべてのコードを調べたわけではないので、3が該当するかどうかはわかりません)

于 2013-06-06T04:00:58.413 に答える