0

我正在开发一个使用 Facebook SDK 的应用程序。我在我的应用程序中集成了 Facebook SDK。我正在尝试为登录用户显示 Facebook 朋友。我不想向用户显示他所有的 Facebook 朋友,而只显示那些符合特定条件的人。我设法选择了这些 Facebook 朋友,但无法显示它们。有没有办法自定义显示 Facebook 好友?在我的代码中,为了获取所有 Facebook 用户,我使用了 picker://friend uri。我也尝试将某些用户 ID 传递给该 Uri,但没有成功。是否有将 Facebook Ids 传递给 picker://friend 以便我可以显示某些朋友,或者这个方向是错误的?

提前感谢您的帮助。亚历克斯

4

1 回答 1

0

这是一个老问题,但我找不到其他试图这样做的人。通过覆盖图形请求及其处理程序,我能够继承 FriendPickerFragment 以指定用户 ID。这有点hacky,但它似乎工作。

// Has to be in this package so we can access package-private members on the superclass.
package com.facebook.widget;

import android.os.Bundle;
import android.text.TextUtils;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Derek on 1/11/2015.
 */
public class MyFriendPickerFragment extends FriendPickerFragment {
    private List<String> userIds;

    public void setUserIds(List<String> userIds) {
        this.userIds = userIds;
    }

    @Override
    Request getRequestForLoadData(Session session) {
        if (userIds == null) {
            return super.getRequestForLoadData(session);
        } else {
            return createRequest(extraFields, session);
        }
    }

    private Request createRequest(Set<String> extraFields, Session session) {
        Request request = Request.newGraphPathRequest(session, "", null);

        Set<String> fields = new HashSet<String>(extraFields);
        String[] requiredFields = new String[]{
                "id",
                "name"
        };
        fields.addAll(Arrays.asList(requiredFields));

        String pictureField = adapter.getPictureFieldSpecifier();
        if (pictureField != null) {
            fields.add(pictureField);
        }

        Bundle parameters = request.getParameters();
        parameters.putString("fields", TextUtils.join(",", fields));
        parameters.putString("ids", TextUtils.join(",", userIds));
        request.setParameters(parameters);

        return request;
    }

    @Override
    public void loadData(boolean forceReload) {
        if(userIds == null) {
            super.loadData(forceReload);
        } else {
            Request request = getRequestForLoadData(getSession());
            request.setCallback(
                    new Request.Callback() {
                        @Override
                        public void onCompleted(Response response) {
                            if (response.getError() == null) {
                                Map<String, Object> map = response.getGraphObject().asMap();
                                final List<GraphUser> users = new ArrayList<>();
                                for (Map.Entry<String, Object> entry : map.entrySet()) {
                                    JSONObject jsonObject = (JSONObject)entry.getValue();
                                    GraphUser user = GraphObject.Factory.create(jsonObject, GraphUser.class);
                                    users.add(user);
                                }

                                adapter.changeCursor(new GraphObjectCursor<GraphUser>() {
                                    private int position = 0;

                                    @Override
                                    public boolean isFromCache() {
                                        return false;
                                    }

                                    @Override
                                    public boolean areMoreObjectsAvailable() {
                                        return position < getCount() - 1;
                                    }

                                    @Override
                                    public int getCount() {
                                        return users.size();
                                    }

                                    @Override
                                    public int getPosition() {
                                        return position;
                                    }

                                    @Override
                                    public boolean move(int offset) {
                                        return moveToPosition(position + offset);
                                    }

                                    @Override
                                    public boolean moveToPosition(int newPosition) {
                                        if (newPosition >= 0 && newPosition < getCount()) {
                                            position = newPosition;
                                            return true;
                                        }
                                        return false;
                                    }

                                    @Override
                                    public boolean moveToFirst() {
                                        position = 0;
                                        return true;
                                    }

                                    @Override
                                    public boolean moveToLast() {
                                        position = getCount() - 1;
                                        return true;
                                    }

                                    @Override
                                    public boolean moveToNext() {
                                        return move(1);
                                    }

                                    @Override
                                    public boolean moveToPrevious() {
                                        return move(-1);
                                    }

                                    @Override
                                    public boolean isFirst() {
                                        return position == 0;
                                    }

                                    @Override
                                    public boolean isLast() {
                                        return position == getCount() - 1;
                                    }

                                    @Override
                                    public boolean isBeforeFirst() {
                                        return position < 0;
                                    }

                                    @Override
                                    public boolean isAfterLast() {
                                        return position >= getCount();
                                    }

                                    @Override
                                    public GraphUser getGraphObject() {
                                        return users.get(position);
                                    }

                                    @Override
                                    public void close() {
                                    }

                                    @Override
                                    public boolean isClosed() {
                                        return false;
                                    }
                                });
                            }
                        }
                    }
            );
            request.executeAsync();
        }
    }
}
于 2015-01-13T03:11:59.883 回答