Facebook使用Fragments共享

时间:2015-12-03 06:31:33

标签: android facebook listview facebook-share

我在我的应用中使用Facebbok分享,我有一个listview,在我的列表项中,我有一个Button,点击Button我正在尝试分享我的东西,但问题是当我通过活动扩展我的课程时我的代码工作正常,但它不适用于Fragment,以下是我的代码段,任何帮助

TestFrag.java

public class TestFrag extends Fragment{

    static CallbackManager callbackManager;
    private static LoginManager manager;
    ListView list;
    Button btnPost;
    Context context;
    static Activity activity;

    ArrayList prgmName;
    public static int[] prgmImages = {R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher};
    public static String[] prgmNameList = {"Let Us C", "c++", "JAVA", "Jsp",
            "Microsoft .Net", "Android", "PHP", "Jquery", "JavaScript"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getActivity());
        View rootView = inflater.inflate(R.layout.tester, container, false);


        callbackManager = CallbackManager.Factory.create();
        context = getActivity();
        activity = getActivity();
        list = (ListView)rootView.findViewById(R.id.listView);
        list.setAdapter(new CustomListAdapter(getActivity(), prgmNameList, prgmImages));

        return rootView;
    }

    public static void publishImage() {
        Bitmap image = BitmapFactory.decodeResource(activity.getResources(),
                R.drawable.ic_launcher);

        SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
                .setCaption("Welcome To Facebook Photo Sharing on steroids!")
                .build();

        SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
                photo).build();

        ShareApi.share(content, null);
        Toast.makeText(activity, "Succsesfully posted on your wall",
                Toast.LENGTH_LONG).show();

    }

    @Override
    public void onActivityResult(int requestCode, int responseCode,
                                 Intent data) {
        super.onActivityResult(requestCode, responseCode, data);
        callbackManager.onActivityResult(requestCode, responseCode, data);
    }

    public static void FaceBookSharing() {
        // title = ed_title.getText().toString();
        // des = ed_dec.getText().toString();
        //
        // callbackManager = CallbackManager.Factory.create();
        // shareDialog = new ShareDialog(this);
        // shareDialog.registerCallback(callbackManager,
        // new FacebookCallback<Sharer.Result>() {
        //
        // @Override
        // public void onCancel() {
        // System.out.println("Cancel");
        // }
        //
        // @Override
        // public void onError(FacebookException e) {
        // e.printStackTrace();
        // }
        //
        // @Override
        // public void onSuccess(Sharer.Result result) {
        // System.out.println("Success");
        //
        // }
        // });
        //
        // if (ShareDialog.canShow(ShareLinkContent.class)) {
        //
        // ShareLinkContent linkContent = new ShareLinkContent.Builder()
        // .setContentTitle(title).setContentDescription(des)
        // .setContentUrl(Uri.parse(image))
        // .setImageUrl(Uri.parse(image)).build();
        //
        // shareDialog.show(linkContent);
        //
        // }

        List<String> permissionNeeds = Arrays.asList("publish_actions");

        manager = LoginManager.getInstance();

        manager.logInWithPublishPermissions(activity, permissionNeeds);

        manager.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        publishImage();

                    }

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onError(FacebookException error) {
                        // TODO Auto-generated method stub

                    }

                });
    }

    public void onClick(View v) {

        if (v == btnPost) {
            FaceBookSharing();
        }

    }



    public class CustomListAdapter extends BaseAdapter {
        String[] result;
        Context context;
        int[] imageId;
        private LayoutInflater inflater = null;

        public CustomListAdapter(Context context, String[] prgmNameList,
                                 int[] prgmImages) {
            // TODO Auto-generated constructor stub
            result = prgmNameList;
            this.context = context;
            imageId = prgmImages;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return result.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class Holder {
            Button tv;
            ImageView img;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Holder holder = new Holder();
            View rowView;
            rowView = inflater.inflate(R.layout.custom, null);
            holder.tv = (Button) rowView.findViewById(R.id.btnPost);
            holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
            // holder.tv.setText(result[position]);
            holder.img.setImageResource(imageId[position]);
            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "You Clicked " + result[position],
                            Toast.LENGTH_LONG).show();
                    TestFrag.FaceBookSharing();
                }
            });
            return rowView;
        }

    }
}

1 个答案:

答案 0 :(得分:5)

在您的主要活动

上覆盖此方法
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    TestFrag demoFragment = (TestFrag) getSupportFragmentManager().findFragmentByTag("test");
    demoFragment.onActivityResult(requestCode, resultCode, data);
}
片段活动结果调用后