将所选用户标记为在FriendPicker中检查(Facebook SDK for Android)

时间:2013-01-26 21:52:25

标签: android facebook sdk picker

我已完成Facebook SDK for Android的教程(特别是"Show Friends"-tutorial)。

当我再次单击“显示朋友”按钮时,如何标记我之前在PickerActivity中选择的所选用户?

5 个答案:

答案 0 :(得分:3)

我查看了Facebook SDK的源代码,在我看来,PickerFragment不能让您重新选择以前选择的项目。由于Facebook SDK是在Apache License 2.0下发布的,并且您可以访问完整的源代码,我猜您可以尝试修改PickerFragment,使其具有必要的方法。

答案 1 :(得分:2)

您必须将包含选定的facebook用户ID的逗号分隔字符串传递给FriendPickerFragment。

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

在PickerFragment的onActivityCreated()中,它将解析列表中所选的id显示。您可以在FacebookSDK中的PickerFragment中看到以下代码。

selectionStrategy.readSelectionFromBundle(savedInstanceState, SELECTION_BUNDLE_KEY);

答案 2 :(得分:1)

这是一个对我有用的解决方案。我扩展了一个标准的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;
}

}

我使用FriendPickerFragment2作为普通的FriendPickerFragment。在OnCreate中,我执行以下操作:

        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);
    }

这里的pickedUsersString是一个逗号分隔的ID字符串。

最后一点是在OnStart中添加一行:

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

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

这个解决方案对我有用。

答案 3 :(得分:0)

好消息。在当前的SDK版本(3.6)中添加了以下功能:

  
      
  • 在FriendPickerFragment上添加了setSelection方法,以允许预先选择朋友。
  •   
  • 在Friend Picker Sample
  • 中添加了setSelection API的示例用法   

答案 4 :(得分:0)

FriendPickerFragment有这个方法:

/**
 * 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);
}

这个方法:

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

您应该在片段实例化后立即调用其中一种方法:

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