投影后,圆形图像视图在列表视图中消失

时间:2015-10-18 08:43:45

标签: android listview android-listview

使用Glide生成一个充满图像(类似于Instagram帖子)的Feed,用于加载图像和用户的个人资料图片。从服务器获取数据后,我在listitem中加载了Url的图像。最初所有项目都正确加载。

问题在于,当我快速滚动列表视图时,用户个人资料图片会消失,并且该视图不响应onClick事件。请解释为什么会发生这种情况,我该如何解决?

每个列表项的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:layout_marginBottom="20dp"
android:orientation="vertical" >

<RelativeLayout android:id="@+id/userheader"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="1">

 <com.mikhaellopez.circularimageview.CircularImageView
    android:id="@+id/realdp"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/nodp"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"/>

    <TextView
    android:id="@+id/handle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/realdp"
    android:text="handle"
    android:layout_marginLeft="3dp"
    android:gravity="center_vertical"
    android:layout_alignTop="@+id/realdp"
    android:layout_alignBottom="@+id/realdp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

    <TextView
    android:id="@+id/uploadtime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/handle"
    android:layout_marginRight="5dp"
    android:layout_alignParentRight="true"
    android:text="time"
    android:textAppearance="?android:attr/textAppearanceSmall" />

  <RelativeLayout android:id="@+id/rlimg"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/handle">

  <ImageView
    android:id="@+id/imgpost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:background="#ffffff"/>
  </RelativeLayout> 

 <RelativeLayout android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rlimg"
    android:layout_marginTop="5dp">

   <com.sivaram.fishograph.FlipImageView
    android:id="@+id/like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:layout_marginLeft="20dp"
    android:src="@drawable/hook_unlike"/>

    <ImageButton
    android:id="@+id/comment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:layout_toRightOf="@+id/likesnum"
    android:layout_marginLeft="20dp"
    android:src="@drawable/comment" />

    <ImageButton
    android:id="@+id/more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    android:layout_alignParentRight="true"
    android:background="#00000000"
    android:src="@drawable/more" />

    <TextView
    android:id="@+id/likesnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/like"
    android:layout_alignTop="@+id/like"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/like"
    android:text="likes"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#440011" />

    <TextView
    android:id="@+id/comnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/comment"
    android:layout_alignTop="@+id/comment"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/comment"
    android:gravity="center_vertical"
    android:text="comments"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#440011" />

   </RelativeLayout>

  </RelativeLayout>  

 <TextView
    android:id="@+id/caption"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:layout_below="@+id/userheader"
    android:gravity="center_horizontal"
    android:layout_weight="1"
    android:text="Caption"
    android:textAppearance="?android:attr/textAppearanceMedium" />
 <TextView
    android:id="@+id/dummy"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:layout_below="@+id/caption"
    android:gravity="center_horizontal"
    android:layout_weight="1"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium" />            
 </LinearLayout>

用于生成Feed的Java代码:

public class Feed extends Fragment implements OnScrollListener
{ 
    String handle;
    ListView lvposts;
    Jsparser jp;
    int width,height;
    int maxMemory;
    int currentFirstVisibleItem ;
    int currentVisibleItemCount;
    PostAdapter pa;
    ArrayList<eachpost> posts;
    int value = 1;
    boolean isLoading = false;
    int photoid;
    private List<String> myData;
    Boolean tapped = false, Loading= false;
    SharedPreferences spf;
    ArrayList<String> likes;
    public Feed()
    {
      super();
    }

    Feed(String handle)
    {
      super();
      photoid = 99999999;
      this.handle = handle; 
    }

    @Override
    public void onCreate(Bundle b)
    {
     super.onCreate(b);
     maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
     final int cacheSize = maxMemory / 8;
     spf = getActivity().getSharedPreferences("prefs",Context.MODE_PRIVATE);
        likes = new ArrayList<String>();
    }
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup vg,  Bundle b)
    { 
      View v = inflater.inflate(R.layout.allposts, vg, false);
      ActionBar ab = ((ActionBarActivity) getActivity()).getSupportActionBar();
      ab.setBackgroundDrawable(new ColorDrawable(Color.RED));
      ab.hide();
      lvposts = (ListView) v.findViewById(R.id.lvposts);
      jp = new Jsparser();
      Display d = getActivity().getWindowManager().getDefaultDisplay();
      width = d.getWidth();
      height = d.getHeight();
      lvposts.setOnScrollListener(this);
      posts = new ArrayList<eachpost>();
      pa = new     PostAdapter(getActivity(),R.layout.postcontent,posts,inflater);
      Loading = true;
      lvposts.setAdapter(pa);
      new GetData(photoid).execute(); 
      return v;
     }

class GetData extends AsyncTask<String,String,String>
  {
    String msg;
    Integer limit,success=0;
    ProgressDialog pd;
    Bitmap dpbm;
    GetData(int l)
    {
        limit = l;
    }
    @Override
    public void onPreExecute()
    {
    }
    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> lp = new ArrayList<NameValuePair>();
        lp.add(new BasicNameValuePair("handle",handle));
        lp.add(new BasicNameValuePair("photoid",limit.toString()));
        JSONObject job = jp.makeHttpRequest("server/getfeed.php", "POST", lp);

        try
        {
          Log.d("json", job.toString());
          success = job.getInt("success");
          msg = job.getString("message");
          if(success==1)
          {
            JSONArray ja = job.getJSONArray("posts");
            for(int c = 0; c<ja.length(); c++)
            {
              JSONObject jb = ja.getJSONObject(c);
              posts.add(new eachpost(jb.getString("handle"),jb.getString("url"), jb.getString("caption"),
              jb.getString("uldatetime"), jb.getInt("likescount"), jb.getInt("comcount"), jb.getString("dpurl"), jb.getInt("isliked"),jb.getInt("photoid") ));
            }
          }
          else
          {
          }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        return msg;
     }

    @Override
    public void onPostExecute(String url)
    {
     Loading = false;
     if(success==1)
      {    
        photoid = posts.get(posts.size()-1).getPhotoid();
        Log.d("last id",photoid+"");
        Log.d("Length of posts",""+posts.size());
        pa.notifyDataSetChanged();
      }
    }

  }

class PostAdapter extends ArrayAdapter<eachpost> 
{
  ViewHolder vholder;
  String root = Environment.getExternalStorageDirectory().toString();
  File dir = new File (root + ".feed");
  Map<Integer,View> myViews;
    public PostAdapter(Context context, int resource, ArrayList<eachpost> list, LayoutInflater li) {
        super(context, R.layout.postcontent, list); 
        myViews = new HashMap<Integer,View>();
    }

    @Override
    public View getView(final int pos,View v,ViewGroup vg)
    {   
      final eachpost post = posts.get(pos);
      final String imgurl = post.getPhotoUrl();
      String dpurl = post.getDpurl();
      int isliked = post.getIsliked();
      View row = myViews.get(pos);
      if(row == null)
      { 
        row = getActivity().getLayoutInflater().inflate(R.layout.postcontent,vg,false); 
        row.setMinimumHeight(height);
        vholder = new ViewHolder();
        vholder.handle = ((TextView) row.findViewById(R.id.handle));
        vholder.caption = ((TextView) row.findViewById(R.id.caption));
        vholder.likesnum = ((TextView) row.findViewById(R.id.likesnum));
        vholder.comnum = ((TextView) row.findViewById(R.id.comnum));
        vholder.uploadtime = ((TextView) row.findViewById(R.id.uploadtime));
        vholder.photo = (ImageView) row.findViewById(R.id.imgpost);
        vholder.feeddp = (CircularImageView) row.findViewById(R.id.realdp);
        vholder.like = (FlipImageView) row.findViewById(R.id.like);
            LayoutParams lp = vholder.photo.getLayoutParams();
        lp.width = width;
        lp.height = width;

        vholder.handle.setText(post.getHandle());
        vholder.caption.setText(post.getCaption());
        vholder.likesnum.setText(post.getLikes()+"");
        vholder.comnum.setText(post.getComments()+"");
        vholder.uploadtime.setText(post.getUl());
        Drawable d = getResources().getDrawable(R.drawable.hook_like);
        vholder.like.setFlippedDrawable(d);
        Glide.with(getActivity()).load("server/"+imgurl).into(vholder.photo);

        if(dpurl.contains("http"))
            Glide.with(getActivity()).load(dpurl).into(vholder.feeddp);
        else
            Glide.with(getActivity()).load("server/"+dpurl).into(vholder.feeddp);
        Log.d("image loading", dpurl + "-" + imgurl);
        if(isliked==1)
             {
               vholder.like.setFlipped(true,false);
               likes.add(imgurl);
             }

        vholder.like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View temp = myViews.get(pos);
                final FlipImageView like = (FlipImageView) temp.findViewById(R.id.like);
                final TextView likesnum = (TextView) temp.findViewById(R.id.likesnum);
                like.toggleFlip();

                if(!likes.contains(imgurl))
                   {
                    posts.get(pos).incrementLikes(handle);
                    likes.add(posts.get(pos).getPhotoUrl());
                    likesnum.setText(posts.get(pos).getLikes()+"");
                  }
                else
                  {
                    likes.remove(posts.get(pos).getPhotoUrl());
                    posts.get(pos).decrementLikes(handle);
                    likesnum.setText(posts.get(pos).getLikes()+"");
                  }

            }
        });

        row.setTag(vholder);
        myViews.put(pos, row);
      }

       return row;
    }

    @Override
    public boolean isEnabled(int position)
    {
        return true;
    }

}  //end of adapter class

static class ViewHolder {
        TextView handle;
        TextView caption;
        TextView likesnum;
        TextView comnum;
        TextView uploadtime;
        ImageView photo;
        CircularImageView feeddp;
        FlipImageView like;
   }

  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
   if (this.currentVisibleItemCount > 0 && scrollState == SCROLL_STATE_FLING) {
    /*** In this way I detect if there's been a scroll which has completed ***/
    /*** do the work for load more date! ***/

        if(currentFirstVisibleItem > (currentVisibleItemCount - 2) && Loading!=true)
        {
            new GetData(photoid).execute();
        }       
   }
  }

 @Override
 public void onScroll(AbsListView view, int firstVisibleItem,
    int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;

 }  
}

2 个答案:

答案 0 :(得分:0)

加载多个图像时,使用异步任务时始终存在缓存问题。我推荐一个开源库Picasso。它将加载图像并缓存它。在那之后很可能,你的快速滚动问题将解决。 原因也可以是正在接收的图像的大小。 Picasso有一些方法可以在将图像放到图像视图之前调整图像大小

答案 1 :(得分:0)

尝试使用此修改后的getView

@Override
public View getView(final int pos,View v,ViewGroup vg)
{   
  final eachpost post = posts.get(pos);
  final String imgurl = post.getPhotoUrl();
  String dpurl = post.getDpurl();
  int isliked = post.getIsliked();
  View row = myViews.get(pos);
  if(row == null)
  { 
   row = getActivity().getLayoutInflater().inflate(R.layout.postcontent,vg,false); 
   row.setMinimumHeight(height);
   vholder = new ViewHolder();
   vholder.handle = ((TextView) row.findViewById(R.id.handle));
   vholder.caption = ((TextView) row.findViewById(R.id.caption));
   vholder.likesnum = ((TextView) row.findViewById(R.id.likesnum));
   vholder.comnum = ((TextView) row.findViewById(R.id.comnum));
   vholder.uploadtime = ((TextView) row.findViewById(R.id.uploadtime));
   vholder.photo = (ImageView) row.findViewById(R.id.imgpost);
   vholder.feeddp = (CircularImageView) row.findViewById(R.id.realdp);
   vholder.like = (FlipImageView) row.findViewById(R.id.like);
       LayoutParams lp = vholder.photo.getLayoutParams();
   lp.width = width;
   lp.height = width;

   row.setTag(vholder); //changed here setTag()
 }else{
     vholder=(ViewHolder)row.getTag(); //changed here getTag()
 }

 vholder.handle.setText(post.getHandle());
 vholder.caption.setText(post.getCaption());
 vholder.likesnum.setText(post.getLikes()+"");
 vholder.comnum.setText(post.getComments()+"");
 vholder.uploadtime.setText(post.getUl());
 Drawable d = getResources().getDrawable(R.drawable.hook_like);
 vholder.like.setFlippedDrawable(d);
 Glide.with(getActivity()).load("server/"+imgurl).into(vholder.photo);

 if(dpurl.contains("http"))
     Glide.with(getActivity()).load(dpurl).into(vholder.feeddp);
 else
     Glide.with(getActivity()).load("server/"+dpurl).into(vholder.feeddp);
 Log.d("image loading", dpurl + "-" + imgurl);
 if(isliked==1)
      {
        vholder.like.setFlipped(true,false);
        likes.add(imgurl);
      }

 vholder.like.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         View temp = myViews.get(pos);
         final FlipImageView like = (FlipImageView) temp.findViewById(R.id.like);
         final TextView likesnum = (TextView) temp.findViewById(R.id.likesnum);
         like.toggleFlip();

         if(!likes.contains(imgurl))
            {
             posts.get(pos).incrementLikes(handle);
             likes.add(posts.get(pos).getPhotoUrl());
             likesnum.setText(posts.get(pos).getLikes()+"");
           }
         else
           {
             likes.remove(posts.get(pos).getPhotoUrl());
             posts.get(pos).decrementLikes(handle);
             likesnum.setText(posts.get(pos).getLikes()+"");
           }

     }
 });
 myViews.put(pos, row);
  }
  return row;
  }