单击列表视图项中的按钮时,其他一些列表视图项也会发生更改 [英] On clicking the button in listview item, change occurs in some others listview items too
本文介绍了单击列表视图项中的按钮时,其他一些列表视图项也会发生更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个应用程序,其中包含带有评论和描述的帖子.每个帖子上都有三个按钮. 1个说明2个注释,第三个类似按钮.我在自定义适配器的getview方法中设置了一个按钮单击侦听器.当我单击描述按钮时,该帖子的描述应显示在按钮下方.但是,当我单击描述"按钮时,也会显示其他一些listview项目的描述.我只想显示单击其描述按钮的帖子的描述.这是我的代码:
获取视图代码:
getview code:
public View getView(int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try
{
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
}
else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring).into(v.imgView);
v.desc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
}
});
return convertView;
}catch (Exception e)
{
return null;
}
}
XML代码:
XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="35dp">
<ImageView
android:id="@+id/iv_list_image"
android:layout_width="match_parent"
android:layout_height="300dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="6">
<Button
android:id="@+id/btn_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Description"
/>
<Button
android:id="@+id/btn_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Comments"
/>
<Button
android:id="@+id/btn_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Likes"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:background="#ffffff"
android:visibility="invisible"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description:"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Default Desc"
android:textColor="#333"
/>
</LinearLayout>
</LinearLayout>
推荐答案
在这种情况下,在更新ListView的项目之前,将int标志用作列表项的位置作为条件语句.
That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.
因此,在您的情况下,Adapter类如下所示:
So in your case, the Adapter class would look something like this:
...
private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set
...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try {
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
.into(v.imgView);
// Runs the following functionality if the positions match. Otherwise, hides the layout.
if (mPosition == position) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
} else {
v.ll.setVisibility(View.INVISIBLE);
}
v.desc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
mPosition = position; // Sets the flag to the position that was clicked
notifyDataSetChanged(); // Updates the data instantly
}
});
return convertView;
} catch (Exception e) {
return null;
}
}
让我知道是否可行.
这篇关于单击列表视图项中的按钮时,其他一些列表视图项也会发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文