Listview对点击的奇怪反应
Posted
技术标签:
【中文标题】Listview对点击的奇怪反应【英文标题】:Listview weird reaction on Clicking 【发布时间】:2015-12-31 10:05:52 【问题描述】:我正在显示一个带有自定义适配器的列表视图。列表视图的每个项目基本上都有一个照片(Imageview)和一个喜欢(FlipImageView)按钮和一个 textView 来显示喜欢的数量。我已经用 3 个项目填充了列表视图。奇怪的问题是,当我点击第二张照片的点赞按钮时,没有任何反应(没有翻转图像,点赞数也没有变化)。相反,第一张照片喜欢按钮会响应,并且第一张照片的喜欢次数会增加。第二个奇怪的问题是,列表视图的第一项没有完全填充(一个图像视图处于空白状态,而其他列表项的图像视图完全膨胀)。但是当我滚动并回到第一个项目时,它会完全膨胀。这是适配器代码、每个列表项的 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_
android:layout_
>
<FrameLayout android:id="@+id/dpsmall"
android:layout_
android:layout_
android:layout_marginLeft="2dp">
<ImageView
android:id="@+id/realdp"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/trans"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_below="@+id/handle"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
>
<ImageView
android:id="@+id/imgpost"
android:layout_
android:layout_
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_
android:layout_
android:layout_below="@+id/rlimg"
android:background="#00000000"
android:layout_marginLeft="40dp"
android:src="@drawable/hook_unlike"/>
<ImageButton
android:id="@+id/comment"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:orientation="vertical"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false" >
<ListView
android:id="@+id/lvposts"
android:layout_
android:layout_
android:divider="#55ea4f5f"
android:listSelector="#00000000"
android:clickable="false"
android:smoothScrollbar="true"
android:scrollingCache="false"
android:animationCache="false" >
</ListView>
</LinearLayout>
【问题讨论】:
是的,上一个索引处的图片之类的会增加 好的,我将删除final,反正它是一个未使用的变量。它始终是第一个索引:/ 【参考方案1】:我会删除所有这些最终声明。因为您将使用第一项的值填充它们:
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);
的变化。如果这不能解决您的问题,请告诉我。
网上有很多关于这方面的信息..这是一个
Can i change value of final int
【讨论】:
我确实像你说的那样。但是现在我在 new AsyncHttpClient() 中遇到错误,说必须将修饰符更改为 final。 哦,是的,我可以等。嗯..是的,我想,也许我会把喜欢的部分作为一个函数。 我读到了 final 关键字,我明白了你所说的。我有一个问题,虽然它们是最终变量,但它们具有本地范围对吗?当每次为每个列表项调用 getView() 时,它不应该与其他项目混淆。对吗? 哦,祝考试顺利。我被困在这个..你的帮助将不胜感激。谢谢以上是关于Listview对点击的奇怪反应的主要内容,如果未能解决你的问题,请参考以下文章
为什么QML ListView委托不立即对选择更改做出反应?
我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法