简体   繁体   English

ListView使textview在超出屏幕可见性时可见

[英]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. 我有一个listview,其中包含具有2个textview(TV1和TV2)和1个按钮的自定义布局。 The button is to set the visibility of the textview(TV2) to be VISIBLE or GONE. 该按钮用于将textview(TV2)的可见性设置为VISIBLE或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. 但是问题在于,如果我滚动列表视图,则退出屏幕的textview(TV2)再次具有可见的文本,即使先前通过单击按钮消失了也是如此。 I want to retain the state on scoll of the listview. 我想保留列表视图的scol上的状态。 That is if the state was GONE, it should remain same on scroll as well. 也就是说,如果状态为GONE,则滚动状态也应保持不变。

Another problem is I have 2 button to hide and show which are not inside listview. 另一个问题是我有2个按钮可以隐藏和显示不在listview中的按钮。 I want to use them to hide or show all the textviews(TV2) present inside the listview. 我想使用它们隐藏或显示列表视图中存在的所有textviews(TV2)。 That is when I click Hide all button, all the TV2 should have visibility as GONE and same on Show All button. 也就是说,当我单击“隐藏全部”按钮时,所有TV2的可见性都应为“消失”,并且在“全部显示”按钮上的可见性相同。

Here is my code: 这是我的代码:

MainActivity.java 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: 活动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 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: 现在,我已通过显示/隐藏详细信息按钮在列表视图中单击来为aa和bb隐藏TV2: 在此处输入图片说明

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. 当我滚动回到顶部时,对应于aa和bb的textviews(TV2)再次可见,而之前给定了它。 在此处输入图片说明

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 . 然后向适配器传递MyContainer实例的ArrayList并重写getView()以基于MyContainer实例visible的布尔值和基于String text的TextView文本来设置可见性。

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. 要设置视图的可见性,请创建一个方法setvisible(int index)该方法setvisible(int index)更改MyContainers数组列表中给定索引处的布尔值(调用notifyDataSetChanged()以重绘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, 全局声明一个TextView数组,

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

Then initialize this array with your 'details' TextViews in your onCreate method. 然后使用onCreate方法中的“详细信息” TextViews初始化此数组。

Then change the visibility of these global instances in your onclicklister. 然后在您的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? 实例仅存在于按钮的onClicklistener方法范围内的事实可能是其效果不持久的原因吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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