Mark selected user as checked in FriendPicker (Facebook SDK for Android)

旧街凉风 提交于 2019-12-01 03:15:01

问题


I've done the tutorials for the Facebook SDK for Android (Especially the "Show Friends"-tutorial).

How can I mark the selected users which i've selected before at the PickerActivity when i click on the "Show Friends"-button again?


回答1:


You have to pass comma separated string with selected facebook user ids in the bundle to the FriendPickerFragment.

Bundle args = new Bundle();
args.putString("com.facebook.android.PickerFragment.Selection", "11111, 2222, 333, already selected facebook ids....");
friendPickerFragment = new FriendPickerFragment(args);

In PickerFragment's onActivityCreated() it will parse the selected ids shows as selected in list. You can see the following code in PickerFragment in FacebookSDK.

selectionStrategy.readSelectionFromBundle(savedInstanceState, SELECTION_BUNDLE_KEY);



回答2:


I've looked at Facebook SDK's source code and it seems to me that PickerFragment does not give you possibility to reselect previously selected items. Since Facebook SDK is published under Apache License 2.0 and you have access to full source code I guess you could try to modify PickerFragment in such a way that it will have necessary methods.




回答3:


Here is a solution which worked for me. I extend a standard FriendPickerFragment.

public class FriendPickerFragment2 extends FriendPickerFragment {

SelectionStrategy selectionStrategy;
String mPreSelectedIDs;

public FriendPickerFragment2(Bundle args)
{
    super(args);
}

@Override
SelectionStrategy createSelectionStrategy() {
    selectionStrategy = getMultiSelect() ? new MultiSelectionStrategy() : new SingleSelectionStrategy();
    return selectionStrategy;
}

public void showInitialSelection()
{
    Bundle bundle = new Bundle();
    bundle.putString(this.MULTI_SELECT_BUNDLE_KEY, mPreSelectedIDs);
    selectionStrategy.readSelectionFromBundle(bundle, this.MULTI_SELECT_BUNDLE_KEY);
    adapter.notifyDataSetChanged();
}

public void setInitialSelection(String IDs)
{
    mPreSelectedIDs = IDs;
}

}

I use FriendPickerFragment2 as a normal FriendPickerFragment. In OnCreate I do the following:

        FragmentManager fm = getSupportFragmentManager();

    if (savedInstanceState == null) {
        final Bundle args = getIntent().getExtras();
        friendPickerFragment = new FriendPickerFragment2(args);
        friendPickerFragment.setInitialSelection(pickedUsersString());

        fm.beginTransaction()
                .add(R.id.friend_picker_fragment, friendPickerFragment)
                .commit();
    } else {
        friendPickerFragment = (FriendPickerFragment2) fm.findFragmentById(R.id.friend_picker_fragment);
    }

Here pickedUsersString is a coma separated string of IDs.

The last point is to add one row in the OnStart:

protected void onStart() {
    super.onStart();
    try {

        friendPickerFragment.loadData(false);
        friendPickerFragment.showInitialSelection();
    } catch (Exception ex) {
        onError(ex);
    }
}

This solution worked for me.




回答4:


Good news. In the current SDK version (3.6) the following feature was added:

  • Added setSelection methods on FriendPickerFragment to allow pre-selection of friends.
  • Added example use of setSelection API in Friend Picker Sample



回答5:


FriendPickerFragment has this method:

/**
 * Sets the list of friends for pre selection. These friends will be selected by default.
 * @param userIds list of friends as ids
 */
public void setSelectionByIds(List<String> userIds) {
    preSelectedFriendIds.addAll(userIds);
}

And this method:

public void setSelection(List<GraphUser> graphUsers) {
    List<String> userIds = new ArrayList<String>();
    for(GraphUser graphUser: graphUsers) {
        userIds.add(graphUser.getId());
    }
    setSelectionByIds(userIds);
}

You should call one of these methods as soon as fragment instantiation:

    friendPickerFragment = new FriendPickerFragment(args);
    friendPickerFragment.setSelectionByIds(fbIDs);


来源:https://stackoverflow.com/questions/14541945/mark-selected-user-as-checked-in-friendpicker-facebook-sdk-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!