单击listview项目中的按钮时,其他列表视图项也会发生更改

时间:2017-06-15 18:30:42

标签: android listview onclicklistener

我正在创建一个应用程序,其中包含评论和说明的帖子。每个帖子上有三个按钮。 1个描述2个评论,第三个是按钮。我在自定义适配器的getview方法中设置了一个按钮单击侦听器。当我点击描述按钮时,该帖子的描述应该显示在按钮下面。但是当我单击描述按钮时,还会显示其他一些listview项目的描述。我只想显示点击描述按钮的帖子的描述。这是我的代码:

getview代码:

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代码:

<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>

2 个答案:

答案 0 :(得分:0)

当你在更新ListView的项目之前将列表项的位置的int标志用作条件语句时。

因此,在您的情况下,Adapter类看起来像这样:

...

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;
    }
}

如果有效,请告诉我。

答案 1 :(得分:0)

最好的方法是在适配器中实现on click侦听器并在按钮上设置标记。之后在onclick方法中通过获取按钮的标签ID来执行所需的任务,这肯定会有效。