0

我一直试图在我的主要 fragmentActivity 上显示一个包含 ListView 的 tabHost。但是我的 listView 根本没有出现。元素正在上传,但视图没有显示它们。

显示 tabHost 的片段

    public class GestionListeNote extends Fragment implements OnTabChangeListener {

private static final String TAG = "FragmentTabs";
public static final String TAB_ALL = "All";
public static final String TAB_TOPAY = "To Pay";
public static final String TAB_PAID = "Paid";
public static final String TAB_SEND = "Send";

private View mRoot;
private TabHost mTabHost;
private int mCurrentTab;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.tabs_fragment, null);
    mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
    setupTabs();
    return mRoot;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    mTabHost.setOnTabChangedListener(this);
    mTabHost.setCurrentTab(mCurrentTab);
    // manually start loading stuff in the first tab
    updateTab(TAB_ALL, R.id.tab_1);
}

private void setupTabs() {
    mTabHost.setup(); // important!
    mTabHost.addTab(newTab(TAB_ALL, R.string.allNotes, R.id.tab_1));
    mTabHost.addTab(newTab(TAB_TOPAY, R.string.notesToPay, R.id.tab_2));
    mTabHost.addTab(newTab(TAB_PAID, R.string.notesPaid, R.id.tab_3));
    mTabHost.addTab(newTab(TAB_SEND, R.string.notesSend, R.id.tab_4));
}

private TabSpec newTab(String tag, int labelId, int tabContentId) {
    Log.d(TAG, "buildTab(): tag=" + tag);

    View indicator = LayoutInflater.from(getActivity()).inflate(
            R.layout.tab,
            (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
    ((TextView) indicator.findViewById(R.id.text)).setText(labelId);

    TabSpec tabSpec = mTabHost.newTabSpec(tag);
    tabSpec.setIndicator(indicator);
    tabSpec.setContent(tabContentId);
    return tabSpec;
}

@Override
public void onTabChanged(String tabId) {
    Log.d(TAG, "onTabChanged(): tabId=" + tabId);
    if (TAB_ALL.equals(tabId)) {
        updateTab(tabId, R.id.tab_1);
        mCurrentTab = 0;
        Log.d(TAG, "current tab=" + mCurrentTab);
        return;
    }
    if (TAB_TOPAY.equals(tabId)) {
        updateTab(tabId, R.id.tab_2);
        mCurrentTab = 1;
        Log.d(TAG, "current tab=" + mCurrentTab);
        return;
    }
    if (TAB_PAID.equals(tabId)) {
        updateTab(tabId, R.id.tab_3);
        mCurrentTab = 2;
        Log.d(TAG, "current tab=" + mCurrentTab);
        return;
    }
    if (TAB_SEND.equals(tabId)) {
        updateTab(tabId, R.id.tab_4);
        mCurrentTab = 3;
        Log.d(TAG, "current tab=" + mCurrentTab);
        return;
    }
}

private void updateTab(String tabId, int placeholder) {
    FragmentManager fm = getFragmentManager();
    if (fm.findFragmentByTag(tabId) == null) {
        fm.beginTransaction()
        .replace(placeholder, new ListeNotesFragment(tabId), tabId)
        .commit();
    }
}

那是我的 ListFragment 展示我的元素

    public class ListeNotesFragment extends ListFragment implements 
    LoaderCallbacks<Void> {

private static final String TAG = "FragmentTabs";

private String mTag;
private MyAdapter mAdapter;
private ArrayList<String> mItems;
private LayoutInflater mInflater;
private int mTotal;
private int mPosition;

private static final String[] All = { "Lorem", "ipsum", "dolor", "sit",
    "amet", "consectetur", "adipiscing", "elit", "Fusce", "pharetra",
    "luctus", "sodales" };
private static final String[] ToPay = { "I", "II", "III", "IV", "V",
    "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV" };
private static final String[] Paid = { "Test" };
private static final String[] Send = { "Test" };

private static final int SLEEP = 1000;

private final int allBarColor = R.color.all_bar;
private final int topayBarColor = R.color.topay_bar;
private final int paidBarColor = R.color.paid_bar;
private final int sendBarColor = R.color.send_bar;

public ListeNotesFragment() {
}

public ListeNotesFragment(String tag) {
    mTag = tag;
    mTotal = GestionListeNote.TAB_ALL.equals(mTag) ? All.length 
            : (GestionListeNote.TAB_TOPAY.equals(mTag) ? ToPay.length 
                    : (GestionListeNote.TAB_PAID.equals(mTag) ? Paid.length 
                            : Send.length ) );
    Log.d(TAG, "mTotal =" + mTotal);
    Log.d(TAG, "Constructor: tag=" + tag);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // this is really important in order to save the state across screen
    // configuration changes for example
    setRetainInstance(true);

    mInflater = LayoutInflater.from(getActivity());

    // you only need to instantiate these the first time your fragment is
    // created; then, the method above will do the rest
    if (mAdapter == null) {
        mItems = new ArrayList<String>();
        mAdapter = new MyAdapter(getActivity(), mItems);
    }
    getListView().setAdapter(mAdapter);

    // initiate the loader to do the background work
    getLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<Void> onCreateLoader(int id, Bundle args) {
    AsyncTaskLoader<Void> loader = new AsyncTaskLoader<Void>(getActivity()) {

        @Override
        public Void loadInBackground() {
            try {
                // simulate some time consuming operation going on in the
                // background
                Thread.sleep(SLEEP);
            } catch (InterruptedException e) {
            }
            return null;
        }
    };
    // somehow the AsyncTaskLoader doesn't want to start its job without
    // calling this method
    loader.forceLoad();
    Log.d(TAG, "Stop sleep");
    return loader;
}

@Override
public void onLoadFinished(Loader<Void> loader, Void result) {

    // add the new item and let the adapter know in order to refresh the
    // views

    mItems.add(GestionListeNote.TAB_ALL.equals(mTag) ? All[mPosition]
            : (GestionListeNote.TAB_TOPAY.equals(mTag) ? ToPay[mPosition]
                    : (GestionListeNote.TAB_PAID.equals(mTag) ? Paid[mPosition] 
                            : Send[mPosition]) ) );
    mAdapter.notifyDataSetChanged();

    Log.d(TAG, "item =" + mItems.get(mPosition));

    // advance in your list with one step
    mPosition++;

    if (mPosition < mTotal - 1) {
        getLoaderManager().restartLoader(0, null, this);
        Log.d(TAG, "onLoadFinished(): loading next...");
    } else {
        Log.d(TAG, "onLoadFinished(): done loading!");
    }
}

@Override
public void onLoaderReset(Loader<Void> loader) {
}

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, List<String> objects) {
        super(context, R.layout.list_item, R.id.text, objects);
        Log.d(TAG, "affiche contenu");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        Wrapper wrapper;

        Log.d(TAG, "affiche vue");
        if (view == null) {
            view = mInflater.inflate(R.layout.list_item, null);
            wrapper = new Wrapper(view);
            view.setTag(wrapper);
        } else {
            wrapper = (Wrapper) view.getTag();
        }

        wrapper.getTextView().setText(getItem(position));
        wrapper.getBar().setBackgroundColor(                
                GestionListeNote.TAB_ALL.equals(mTag) ? getResources().getColor(allBarColor) 
                        : (GestionListeNote.TAB_TOPAY.equals(mTag) ? getResources().getColor(topayBarColor)
                                : (GestionListeNote.TAB_PAID.equals(mTag) ? getResources().getColor(paidBarColor) 
                                        :getResources().getColor(sendBarColor)) ) );        
        return view;
    }

}

// use an wrapper (or view holder) object to limit calling the
// findViewById() method, which parses the entire structure of your
// XML in search for the ID of your view
private class Wrapper {
    private final View mRoot;
    private TextView mText;
    private View mBar;

    public Wrapper(View root) {
        mRoot = root;
        Log.d(TAG, "initialise view");
    }

    public TextView getTextView() {
        if (mText == null) {
            mText = (TextView) mRoot.findViewById(R.id.text);
            Log.d(TAG, "initialise TextView");
        }
        Log.d(TAG, "initialise view");
        return mText;
    }

    public View getBar() {
        if (mBar == null) {
            mBar = mRoot.findViewById(R.id.bar);
            Log.d(TAG, "initialise view");
        }
        Log.d(TAG, "initialise view");
        return mBar;
    }
}

在xml下面

主要的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="org.newnote.android.AddNoteButton" />

<fragment
    android:id="@+id/tabs_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="org.newnote.android.GestionListeNote" />

liste_item

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="@dimen/list_item_height"
   android:gravity="left|center_vertical">
<View
    android:id="@+id/bar"
    android:layout_width="10dp"
    android:layout_height="fill_parent" />
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:gravity="center" />

tabs_fragment

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <FrameLayout
            android:id="@+id/tab_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <FrameLayout
            android:id="@+id/tab_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <FrameLayout
            android:id="@+id/tab_3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <FrameLayout
            android:id="@+id/tab_4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>
</LinearLayout>

标签

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="80dp"
    android:layout_height="@dimen/tab_height"
    android:gravity="center"
    android:padding="@dimen/tab_padding"
    android:background="@drawable/tab_selector">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="@dimen/text_small"
    android:textStyle="bold"
    android:textColor="@drawable/tab_text_selector" />

我不明白为什么我的片段不显示元素

希望你能帮助我。

4

1 回答 1

1

您似乎缺少onCreateViewListFragment

于 2012-07-10T22:14:35.063 回答