0

我在我的应用程序中实现了 FragmentPagerAdapter,但每当我滑动时,它只为每个片段显示相同的项目列表。我正在使用 SherlockFragmentActivity 并且每当我在片段寻呼机中滑动到下一个或上一个片段时,我想为每个片段显示不同的非静态页面。

我该怎么做?

我是否列出了一个没有任何当前片段标题的联系人列表,以便我可以知道这是我使用此代码在我的片段中的联系人片段

public class ContactsFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor>{

     // This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;



    public static Fragment newInstance(Context context){
        ContactsFragment contactFragment = new ContactsFragment();
        return contactFragment;
    }


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

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
       // setEmptyText("No phone numbers");


        // Create an empty adapter we will use to display the loaded data.

        mAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1, null,
                new String[] {ContactsContract.Contacts.DISPLAY_NAME},
                new int[] { android.R.id.text1}, 0);


        setListAdapter(mAdapter);

        // Start out with a progress indicator.
        setListShown(true);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);

    }



    @Override public void onListItemClick(ListView l, View v, int position, long id) {
        // Insert desired behavior here.
        Log.i("FragmentComplexList", "Item clicked: " + id);
    }

    // These are the Contacts rows that we will retrieve.
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
    };

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
         // This is called when a new Loader needs to be created.  This
        // sample only has one Loader, so we don't care about the ID.
        // First, pick the base URI to use depending on whether we are
        // currently filtering.
        Uri baseUri;
        if (mCurFilter != null) {
            baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
                      Uri.encode(mCurFilter));
        } else {
            baseUri = ContactsContract.Contacts.CONTENT_URI;
        }

        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                + ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
        return new CursorLoader(getActivity(), baseUri,
                CONTACTS_SUMMARY_PROJECTION, select, null,
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);

        // The list should now be shown.
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }


}

但我想在联系人列表上方显示磁贴。为此,我在下面列出的片段类中添加了两个方法

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Contacts");
        return v;
    }

但是当我运行我的应用程序“你的应用程序意外停止”时,这个弹出消息就来了。

如果没有这些方法,我的应用程序可以正常工作,但没有任何片段标题。它出什么问题了?

4

1 回答 1

0

嗨,您可以在此处使用视图寻呼机来实现此示例

您可以在 one.xml 布局中使用它

<ListView android:id="@+id/List" android:background="#00000000"
    android:scrollbars="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollingCache="false"
    android:fastScrollEnabled="true"
    android:cacheColorHint="@color/white"
     />

于 2012-06-07T07:09:39.503 回答