滚动ListView时丢失的按钮

时间:2014-04-10 12:00:30

标签: android listview

当我点击listview项目时,该特定项目按钮是可见的。当我滚动列表视图时,按钮消失了。如何实现此功能。这有什么不对请帮帮我。感谢

这是我的代码:

MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    SampleAdapter sa;
    ArrayList<String> as=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++)
        {
            as.add(String.valueOf(i));
        }
        lv=(ListView)findViewById(R.id.listView1);
        sa=new SampleAdapter(MainActivity.this);
        lv.setAdapter(sa);

        }
    class SampleAdapter extends BaseAdapter
    {

        Context ctx;
        Button b;
        TextView tv;
        LayoutInflater lin;
        public SampleAdapter(Context ct) {
            this.ctx=ct;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return as.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            lin=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
            arg1=lin.inflate(R.layout.li, null);
            tv=(TextView)arg1.findViewById(R.id.textView1);
            b=(Button)arg1.findViewById(R.id.button1);
            tv.setText(as.get(pos));
            b.setText(as.get(pos));
            arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button c=(Button)v.findViewById(R.id.button1);
                    c.setVisibility(View.VISIBLE);
                }
            });
            return arg1;
        }

    }
    }

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

li.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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:text="Button" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

你有

android:visibility="invisible"

这使按钮不可见。

然后你有

c.setVisibility(View.VISIBLE);

您需要了解listview回收视图。因此,当您滚动按钮时,按钮将变为不可见,因为视图会被回收。

检查以下链接

How ListView's recycling mechanism works

请检查此示例与您的非常相似

ListView subobject clickable confilct

要解决此问题,您可以使用Model类

模型类

public class Model {
int visibility;
 String value;


public int getVisibility() {
    return visibility;
}
public void setVisibility(int visibility) {
    this.visibility = visibility;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}

}

MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    SampleAdapter sa;
    final int visible1 = View.INVISIBLE;
    final int visible2 = View.VISIBLE;
    ArrayList<Model> as=new ArrayList<Model>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++)
        {
            Model model  = new Model();
            model.setValue(String.valueOf(i));
            model.setVisibility(visible1);
            as.add(model);
        }
        lv=(ListView)findViewById(R.id.listView1);
        sa=new SampleAdapter(MainActivity.this);
        lv.setAdapter(sa);

        }
    class SampleAdapter extends BaseAdapter
    {

        Context ctx;
        Button b;
        TextView tv;
        LayoutInflater lin;
        public SampleAdapter(Context ct) {
            this.ctx=ct;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return as.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            lin=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
            arg1=lin.inflate(R.layout.fg, null);
            tv=(TextView)arg1.findViewById(R.id.textView1);
            b=(Button)arg1.findViewById(R.id.button1);
            tv.setText(as.get(pos).getValue());
            b.setText(as.get(pos).getValue());
            arg1.setTag(pos);
            b.setVisibility(as.get(pos).getVisibility());
            arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int pos = (int) v.getTag();
                    Model model = as.get(pos);
                    model.setVisibility(visible2);
                    SampleAdapter.this.notifyDataSetChanged();

                }
            });
            return arg1;
        }

    }
    }

对齐

您可以向上和向下滚动,状态仍然是

enter image description here

答案 1 :(得分:0)

在适配器中删除它:

arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button c=(Button)v.findViewById(R.id.button1);
                    c.setVisibility(View.VISIBLE);
                }
            });

这在您的li.xml

android:visibility="invisible"

您的代码可以使您的li.xml中的defailt和您的适配器中的onItemClick使该按钮不可见,使其可见