天天看點

友盟自定義分享面闆(GridView)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="?color_more_background" >

    <TextView

        android:id="@+id/share_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="6dp"

        android:layout_marginTop="16dp"

        android:text="分享"

        android:textSize="14sp" />

    <View

        android:id="@+id/divider_line"

        android:layout_width="match_parent"

        android:layout_height="1dp"

        android:layout_below="@id/share_text"

        android:background="@drawable/divider" />

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/divider_line"

        android:layout_marginTop="15dp"

        android:orientation="vertical" >

        <include layout="@layout/share_board" />

    </LinearLayout>

</RelativeLayout>

share_board.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <GridView

        android:id="@+id/gridView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:horizontalSpacing="1dp"

        android:numColumns="4"

        android:verticalSpacing="1dp" >

    </GridView>

</LinearLayout>

activity_classify_up_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="?color_white" >

    <RelativeLayout

        android:id="@+id/person_entrys"

        android:layout_width="wrap_content"

        android:layout_height="93dp"

        android:background="@drawable/classify_up_background" >

        <ImageView

            android:id="@+id/imageView1"

            android:layout_width="47dp"

            android:layout_height="47dp"

            android:layout_centerHorizontal="true"

            android:layout_centerVertical="true"

            android:contentDescription="@null"

            android:src="@drawable/android_person_entry" />

        <TextView

            android:id="@+id/textView1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_below="@id/imageView1"

            android:layout_marginTop="5dp"

            android:gravity="center"

            android:text="@string/person_entry"

            android:textColor="?color_set_textcolor"

            android:textSize="12sp" />

    </RelativeLayout>

</RelativeLayout>

CustomShareBoard .java

public class CustomShareBoard extends PopupWindow  {

private List<HashMap<String, Object>> item;

private GridView mTopGridView;

private SimpleAdapter mSimpleAdapter;

private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

private Activity mActivity;

public CustomShareBoard(Activity activity) {

super(activity);

this.mActivity = activity;

initView(activity);

}

private void initTopGridViewAdapter() {

item = new ArrayList<HashMap<String, Object>>();

int data[] = { R.string.umeng_socialize_text_weixin_key, R.string.sina, R.string.umeng_socialize_text_qq_key, R.string.umeng_socialize_text_qq_zone_key };

int imgId[] = { R.drawable.umeng_socialize_wechat, R.drawable.umeng_socialize_wxcircle, R.drawable.umeng_socialize_qq_on, R.drawable.umeng_socialize_qzone_on };

for (int i = 0; i < data.length; i++) {

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("itemImage", imgId[i]);

map.put("itemName", mActivity.getResources().getString(data[i]));

item.add(map);

}

mSimpleAdapter = new SimpleAdapter(mActivity, item, R.layout.activity_classify_up_item, new String[] { "itemImage", "itemName" }, new int[] { R.id.imageView1, R.id.textView1 });

}

@SuppressWarnings("deprecation")

private void initView(Context context) {

View rootView = LayoutInflater.from(context).inflate(R.layout.custom_board, null);

mTopGridView = (GridView) rootView.findViewById(R.id.gridView);

initTopGridViewAdapter();

mTopGridView.setAdapter(mSimpleAdapter);

mTopGridView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

if(arg2==0){

performShare(SHARE_MEDIA.WEIXIN);

}else if(arg2==1){

performShare(SHARE_MEDIA.SINA);

}else if(arg2==2){

performShare(SHARE_MEDIA.QQ);

}else if(arg2==3){

performShare(SHARE_MEDIA.QZONE);

}

}

});

setContentView(rootView);

setWidth(LayoutParams.MATCH_PARENT);

setHeight(LayoutParams.WRAP_CONTENT);

setFocusable(true);

setBackgroundDrawable(new BitmapDrawable());

setTouchable(true);

}

private void performShare(SHARE_MEDIA platform) {

mController.postShare(mActivity, platform, new SnsPostListener() {

@Override

public void onStart() {

}

@Override

public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {

String showText = platform.toString();

if (eCode == StatusCode.ST_CODE_SUCCESSED) {

showText += "平台分享成功";

} else {

showText += "平台分享失敗";

}

Toast.makeText(mActivity, showText, Toast.LENGTH_SHORT).show();

dismiss();

}

});

}

}

打開分享面闆的觸發事件

button.setOnClickListener(new View.onClickListener){

postShare();

}

private void postShare() {

        CustomShareBoard shareBoard = new CustomShareBoard(this);

        shareBoard.showAtLocation(this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);

    }