我正在使用自定义适配器显示列表视图。列表视图的每个项目基本上都有一个照片(Imageview)和一个像(FlipImageView)按钮和一个textView来显示喜欢的数量。我已经在列表视图中填充了3个项目。奇怪的问题是,当我点击第二张照片的类似按钮时,没有响应(没有图像翻转,喜欢的数量没有变化)。相反,第一张照片按钮响应,第一张照片的喜欢数量增加。第二个奇怪的问题是,listview的第一项未完全填充(imageview位于空白,而其他列表项的imageview完全膨胀)。但是当我滚动并回到第一个项目时,它会完全膨胀。这是适配器代码,每个列表项的xml文件和Listview本身。如何解决这些问题?
class PostAdapter extends ArrayAdapter<eachpost>
{
ArrayList<eachpost> posts;
ViewHolder vholder;
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File (root + "/fishograph/.feed");
public PostAdapter(Context context, int resource, ArrayList<eachpost> list) {
super(context, R.layout.postcontent, list);
posts = list;
}
@Override
public View getView(int pos,View vu,ViewGroup vg)
{
final eachpost post = posts.get(pos);
final int finalpos = pos;
final String han = post.getHandle();
final String imgurl = post.getDpbase64();
if(vu == null)
{
vu = getActivity().getLayoutInflater().inflate(R.layout.postcontent,vg,false);
vholder = new ViewHolder();
vholder.handle = ((TextView) vu.findViewById(R.id.handle));
vholder.caption = ((TextView) vu.findViewById(R.id.caption));
vholder.likesnum = ((TextView) vu.findViewById(R.id.likesnum));
vholder.comnum = ((TextView) vu.findViewById(R.id.comnum));
vholder.uploadtime = ((TextView) vu.findViewById(R.id.uploadtime));
vholder.photo = (ImageView) vu.findViewById(R.id.imgpost);
vholder.feeddp = (ImageView) vu.findViewById(R.id.realdp);
vholder.like = (FlipImageView) vu.findViewById(R.id.like);
Drawable d =getResources().getDrawable(R.drawable.hook_like);
vholder.like.setFlippedDrawable(d);
LayoutParams lp = vholder.photo.getLayoutParams();
lp.width = width;
lp.height = width;
vholder.feeddp.requestLayout();
vholder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!likes.contains(imgurl))
{
RequestParams rp = new RequestParams();
rp.add("handle",handle);
rp.add("url",imgurl);
vholder.like.toggleFlip();
new AsyncHttpClient().post("Php script to increment like",rp,new AsyncHttpResponseHandler(){
@Override
public void onFailure(int code,Throwable t,String err)
{
Toast.makeText(getActivity(), "Something went wrong..Please try again", Toast.LENGTH_SHORT).show();
Log.d("onError like",err);
vholder.like.toggleFlip();
}
@Override
public void onSuccess(String response)
{
response = response.trim();
Log.d("like", response);
if(!response.equals("liked"))
{
Toast.makeText(getActivity(), "Something went wrong..Please try again", Toast.LENGTH_SHORT).show();
vholder.like.toggleFlip();
}
else
{ likes.add(imgurl);
post.incrementLikes();
vholder.likesnum.setText(post.getLikes()+"");
}
}
});
}
else
{
RequestParams rp = new RequestParams();
rp.add("handle",handle);
rp.add("url", imgurl);
vholder.like.toggleFlip();
new AsyncHttpClient().post("php script url to unlike",rp,new AsyncHttpResponseHandler(){
@Override
public void onFailure(int code,Throwable t,String err)
{
Toast.makeText(getActivity(), "Something went wrong..Please try again", Toast.LENGTH_SHORT).show();
Log.d("onError unlike", err);
vholder.like.toggleFlip();
}
@Override
public void onSuccess(String response)
{
response = response.trim();
Log.d("unlike", response);
if(!response.equals("unliked"))
{
Toast.makeText(getActivity(), "Something went wrong..Please try again", Toast.LENGTH_SHORT).show();
vholder.like.toggleFlip();
}
else
{
likes.remove(imgurl);
post.decrementLikes();
vholder.likesnum.setText(post.getLikes()+"");
}
}
});
}
}
});
vu.setTag(vholder);
}
else
vholder = (ViewHolder) vu.getTag();
vholder.handle.setText(posts.get(pos).getHandle());
vholder.caption.setText(posts.get(pos).getCaption());
vholder.likesnum.setText(posts.get(pos).getLikes()+"");
vholder.comnum.setText(posts.get(pos).getComments()+"");
vholder.uploadtime.setText(posts.get(pos).getUl());
Glide.with(getActivity()).load("photo to load").into(vholder.photo);
RequestParams rpdp = new RequestParams();
Log.d("FEED POSTADAPTER load dp handle", han);
rpdp.add("handle",han);
new AsyncHttpClient().post("url to get DP of user", rpdp, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String response)
{
response = response.trim();
Log.d("getdpurl",finalpos+response);
if(response.contains("http"))
Glide.with(getActivity()).load(response).into(vholder.feeddp);
else
Glide.with(getActivity()).load("url of the dp").into(vholder.feeddp);
}
@Override
public void onFailure(int err,Throwable error,String response)
{
Log.d("Feed POSTADAPTER getdpurl", response);
}
});
RequestParams rpliked = new RequestParams();
rpliked.add("handle", handle);
rpliked.add("url",imgurl);
new AsyncHttpClient().post("php script to check if a photo is likes", rpliked, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String response)
{
response = response.trim();
Log.d("FEED POSTADAPTER isliked", response);
if(response.equals("islikedyes"))
{ //TODO set the hook-like
likes.add(imgurl);
vholder.like.setFlipped(true, false);
}
}
@Override
public void onFailure(int err,Throwable error,String response)
{
Log.d("Feed POSTADAPTER isliked", response);
//Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
}
});
return vu;
}
@Override
public boolean isEnabled(int position)
{
return true;
}
}
ListView每个项目
<?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="wrap_content"
>
<FrameLayout android:id="@+id/dpsmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp">
<ImageView
android:id="@+id/realdp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/trans"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="@drawable/framedo" />
</FrameLayout>
<TextView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/dpsmall"
android:text="handle"
android:layout_marginLeft="3dp"
android:gravity="center_vertical"
android:layout_alignTop="@+id/dpsmall"
android:layout_alignBottom="@+id/dpsmall"
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"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
>
<ImageView
android:id="@+id/imgpost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="#ffffff"
android:layout_marginTop="7dp"
/>
</RelativeLayout>
<com.sivaram.fishograph.FlipImageView
xmlns:fiv="http://schemas.android.com/apk/res-auto"
android:id="@+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rlimg"
android:background="#00000000"
android:layout_marginLeft="40dp"
android:src="@drawable/hook_unlike"/>
<ImageButton
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rlimg"
android:background="#00000000"
android:layout_toRightOf="@+id/likesnum"
android:layout_marginLeft="5dp"
android:src="@drawable/comment" />
<ImageButton
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rlimg"
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="2dp"
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="2dp"
android:layout_toRightOf="@+id/comment"
android:gravity="center_vertical"
android:text="comments"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#440011" />
<TextView
android:id="@+id/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rlimg"
android:layout_below="@+id/like"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="Caption"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
ListView本身
<?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"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false" >
<ListView
android:id="@+id/lvposts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#55ea4f5f"
android:listSelector="#00000000"
android:clickable="false"
android:smoothScrollbar="true"
android:scrollingCache="false"
android:animationCache="false" >
</ListView>
</LinearLayout>
答案 0 :(得分:0)
我会删除所有这些最终声明。正如您将使用第一项的值填充它们:
final eachpost post = posts.get(pos);
final int finalpos = pos;
final String han = post.getHandle();
final String imgurl = post.getDpbase64()
要:
eachpost post = posts.get(pos);
int finalpos = pos;
String han = post.getHandle();
String imgurl = post.getDpbase64()
你应该看到Log.d("getdpurl",finalpos+response);
当你这样做时的变化。如果这不能解决您的问题,请告诉我。
网上有很多相关信息......这里有一个