ListView与Facebook ProfilePictureView小部件导致滞后

时间:2013-11-18 16:11:29

标签: android facebook android-listview

我为包含来自facebook SDK 3.5的ProfilePictureView元素的列表视图完成了一个自定义适配器。这里的问题是:由于加载配置文件图像(虽然我删除它的次数要少得多)但滚动时存在很多延迟,尽管它是异步加载的。

我该如何解决这个问题?在我的自定义适配器下面。

public class UserAdapter extends ArrayAdapter<User> {

/** Contacts list */
private List<User> Users;

/** Context */
private Activity Ctx;

public UserAdapter(Context context, int textViewResourceId, List<User> users) {
    super(context, textViewResourceId, users);
    this.Ctx = (Activity) context;
    this.Users = users;
}

@Override
public View getView(int position, View v, ViewGroup parent) {
    // Keeps reference to avoid future findViewById()
    UsersViewHolder viewHolder;

    if (v == null) {
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.row_friend, parent, false);

        viewHolder = new UsersViewHolder();
        viewHolder.mUserName = (CustomTextView) v.findViewById(R.id.friendName);
        viewHolder.mUserDescription = (CustomTextView) v.findViewById(R.id.friendDescription);
        viewHolder.mProfilePicture = (ProfilePictureView) v.findViewById(R.id.userPicture); 
        v.setTag(viewHolder);
    } else {
        viewHolder = (UsersViewHolder) v.getTag();
    }  

    User mUser = Users.get(position);
    if (mUser != null) {
        viewHolder.mUserName.setText(""+mUser.getName()+" "+mUser.getLastName());
        viewHolder.mUserDescription.setText(mUser.getEmail());
        viewHolder.mProfilePicture.setProfileId(mUser.getIdFB()); //problem
        viewHolder.mProfilePicture.setCropped(true);              //problem
    }
    return v;
}

static class UsersViewHolder {
    LinearLayout mLayout;
    ProfilePictureView mProfilePicture;
    CustomTextView mUserName;
    CustomTextView mUserDescription;
}
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,所以我决定使用ImageView直接从facebook api下载图片。这是代码:

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {

private ImageView layoutimage;
private Bitmap bitmap;
private boolean malformedUrl;

public DownloadImage(ImageView Image) {
    this.layoutimage = Image;
}

public Bitmap getBitmap() {
    return bitmap;
}

@Override
protected void onPreExecute() {
}

@Override
protected Bitmap doInBackground(String... Image_URL) {

    String url = Image_URL[0];
    try {

        // Handle redirect
        URL obj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        int status = conn.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER)
                url = conn.getHeaderField("Location");
        }

        // Get bitmap from URL
        Bitmap bitmap = null;
        try {
            // Download Image from URL
            InputStream input = new java.net.URL(url)
                    .openStream();
            bitmap = BitmapFactory.decodeStream(input);
        } catch (Exception e) {
            // Error Log
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bitmap;
    } catch (MalformedURLException e) {
        malformedUrl = true;
        return null;
    } catch (IOException e) {
        malformedUrl = true;
        return null;
    }
}

@Override
protected void onPostExecute(Bitmap result) {
    // Set image into image.xml layout
    if (layoutimage != null)
        layoutimage.setImageBitmap(result);
    bitmap = result;
}
}

然后你只需使用facebook id调用该类,如下所示:

DownloadImage di = new DownloadImage(category.findViewById(R.id.your_image_view));
di.execute("http://graph.facebook.com/" + FACEBOOK_ID + "/picture?type=small);