简体   繁体   中英

ListView making textview visible on going out of screen visibility

I have a listview which contains custom layout having 2 textviews (TV1 and TV2) and 1 button. The button is to set the visibility of the textview(TV2) to be VISIBLE or GONE. The button is working fine. But the problem is suppose if I scroll the listview, the textview(TV2) which goes out of screen is again having text as visible even if it was GONE earlier by button click. I want to retain the state on scoll of the listview. That is if the state was GONE, it should remain same on scroll as well.

Another problem is I have 2 button to hide and show which are not inside listview. I want to use them to hide or show all the textviews(TV2) present inside the listview. That is when I click Hide all button, all the TV2 should have visibility as GONE and same on Show All button.

Here is my code:

MainActivity.java

public class MainActivity extends Activity {
    Button show, hide;
    ListView lv;
    ArrayList<String> al1;
    MyAdapter ma;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show=(Button) findViewById(R.id.button1);
        hide=(Button) findViewById(R.id.button2);
        lv=(ListView) findViewById(R.id.listView1);
        al1=new ArrayList<String>();
        al1.add("aa");
        al1.add("bb");
        al1.add("cc");
        al1.add("dd");
        al1.add("ee");
        al1.add("ff");
        al1.add("gg");
        al1.add("hh");
        al1.add("ii");
        al1.add("jj");
        al1.add("kk");
        al1.add("ll");
        al1.add("mm");
        al1.add("nn");
        al1.add("oo");

        ma=new MyAdapter();

        lv.setAdapter(ma);

        show.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        hide.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });


    }

    class MyAdapter extends BaseAdapter
    {

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LinearLayout ll=(LinearLayout) getLayoutInflater().inflate(R.layout.custom,parent,false);
            TextView tv1=(TextView) ll.findViewById(R.id.textView1);
            Button b=(Button) ll.findViewById(R.id.get_details);
            TextView tv2=(TextView) findViewById(R.id.textView2);

            tv1.setText(al1.get(position));
            b.setTag(position);

            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int pos=(Integer) v.getTag();
                    LinearLayout linear=(LinearLayout) v.getParent();
                    TextView details=(TextView) linear.findViewById(R.id.textView2);
                    if(details.getVisibility()==View.GONE)
                    {
                        details.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        details.setVisibility(View.GONE);
                    }

                }
            });

            return ll;
        }

    }
}

Activity Main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Show All Details" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Hide all details" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1" >

    </ListView>

</RelativeLayout>

Custom.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="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/get_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show/Hide Details"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No details available right now"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Here is the screenshot of my problem:

This is the initial state when I run the project: 在此处输入图片说明

Now I have Hidden the TV2 for aa and bb by show/hide details button click inside listview: 在此处输入图片说明

Here is the scrolled down part: 在此处输入图片说明

And when I scroll back to top, textviews(TV2) respective to aa and bb are again visible whereas it was given GONE earlier. 在此处输入图片说明

Your adapter has no clue if the view was visible or invisibel, because the row is destroyed when it leaves the screen and recreated when it enters the screeen again. I suggest you create a new container class like this:

public class MyContainer {

    public boolean visible = true;
    public String text;

    public MyContainer(String text) {
         this.text = text;
    }
}

then pass the adapter an ArrayList of MyContainer instances and rewrite your getView() to set the visibility based on the boolean visible from the MyContainer instance and the text of the TextView based on the String text .

To set the visibility of an View create a Method setvisible(int index) wich changes the boolean in the array list of MyContainers at the given index (call notifyDataSetChanged() to redraw the litview.

This way the adapter wil remember wich view is visible.

Note

it is not good practice to declare an arraylist for the adapter globaly, instead pass an araylist in the Constructor.

Something you could try:

declare a TextView array globally,

Button show, hide;
ListView lv;
ArrayList<String> al1;
MyAdapter ma;
TextView[] tva;

Then initialize this array with your 'details' TextViews in your onCreate method.

Then change the visibility of these global instances in your onclicklister.

The fact that the instances only exist within the scope of your onClicklistener method for your button might be the reason their effects aren't lasting?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM