2014-02-19 62 views
5

如何自定義的Facebook應用Android的共享意圖。當我使用共享意圖時,我得到以下對話框。如何自定義共享意向onclick事件中的Android

Share Intent Dialog

不過我使用後的圖像和文字的Facebook SDK。以及如何自定義,當我們在上面的對話框中,單擊在Facebook上的圖標就會轉到我的自定義的Facebook對話......

+0

嘗試此鏈接http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android?rq=1 – Hariharan

+0

是否有可能處理共享意圖的onclick事件@ HariharanTamilan –

回答

11

通過使用下面的代碼ü可以得到其安裝在社交媒體網絡的應用程序列表的列表你的手機。

Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); 
sendIntent.setType("text/plain"); 
List activities = ShareList.this.getPackageManager().queryIntentActivities(sendIntent, 0); 

發送此列表適配器類:

ListView lv=(ListView)findViewById(R.id.listView1); 
final ShareAdapter adapter=new ShareAdapter(ShareList.this,activities.toArray()); 
lv.setAdapter(adapter); 

下面是Adapter類代碼:

public class ShareAdapter extends BaseAdapter { 
Object[] items; 
private LayoutInflater mInflater; 
Context context; 

public ShareAdapter(Context context, Object[] items) { 
    this.mInflater = LayoutInflater.from(context); 
    this.items = items; 
    this.context = context; 
} 

public int getCount() { 
    return items.length; 
} 

public Object getItem(int position) { 
    return items[position]; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.singleitem, null); 
     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.textView1); 
     holder.logo = (ImageView) convertView.findViewById(R.id.imageView1); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.name 
      .setText(((ResolveInfo) items[position]).activityInfo.applicationInfo 
        .loadLabel(context.getPackageManager()).toString()); 

    holder.logo 
      .setImageDrawable(((ResolveInfo) items[position]).activityInfo.applicationInfo 
        .loadIcon(context.getPackageManager())); 

    return convertView; 
} 

static class ViewHolder { 

    TextView name; 
    ImageView logo; 
}} 

使用下面的代碼處理在ListView特定的社會媒體網絡:

lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      ResolveInfo info = (ResolveInfo) adapter.getItem(arg2); 
      if(info.activityInfo.packageName.contains("facebook")) { 
       new PostToFacebookDialog(context, body).show(); 
     //here u can write your own code to handle the particular social media networking apps.  
       Toast.makeText(getApplicationContext(), "FaceBook", Toast.LENGTH_LONG).show(); 
      } else { 
       Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
       intent.setClassName(info.activityInfo.packageName, info.activityInfo.name); 
       intent.setType("text/plain"); 
       intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
       intent.putExtra(Intent.EXTRA_TEXT, "body"); 
       ((Activity)ShareList.this).startActivity(intent); 
      } 

     } 
    }); 
+1

很好的答覆。但是,你能告訴我什麼是'R.id.listView1'嗎? –

+0

非常感謝。這有點棘手,但你的建議讓我工作。明天我會展示我的解決方案,作爲一個額外的「如何」 - 我只是一個初學者,但如果我向別人解釋我的代碼,這個東西會變得更清晰。非常感謝! –

1

我使用Venu的解決方案來獲得「定製的共享意圖」。我剛剛創建xml時遇到了一些麻煩..所以在這裏我想向其他Android初學者展示如何添加custom_share_list_white .xml。也許它會幫助其他人也使它工作。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal"> 

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/listView" 
      android:footerDividersEnabled="false"/> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center_vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="Large Text" 
       android:id="@+id/tv_share_adapter" 
       android:gravity="center_vertical" 
       android:layout_gravity="center_vertical"/> 
     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
在ShareAdapter.java

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.custom_share_list_white, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.tv_share_adapter); 
      holder.logo = (ImageView) convertView.findViewById(R.id.imageView); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.name.setText(((ResolveInfo) items[position]).activityInfo 
            .applicationInfo.loadLabel(context.getPackageManager()).toString()); 

     holder.logo.setImageDrawable(((ResolveInfo) items[position]).activityInfo 
              .applicationInfo.loadIcon(context.getPackageManager())); 

     return convertView; 
    }