[英]ListView making textview visible on going out of screen visibility
我有一個listview,其中包含具有2個textview(TV1和TV2)和1個按鈕的自定義布局。 該按鈕用於將textview(TV2)的可見性設置為VISIBLE或GONE。 該按鈕工作正常。 但是問題在於,如果我滾動列表視圖,則退出屏幕的textview(TV2)再次具有可見的文本,即使先前通過單擊按鈕消失了也是如此。 我想保留列表視圖的scol上的狀態。 也就是說,如果狀態為GONE,則滾動狀態也應保持不變。
另一個問題是我有2個按鈕可以隱藏和顯示不在listview中的按鈕。 我想使用它們隱藏或顯示列表視圖中存在的所有textviews(TV2)。 也就是說,當我單擊“隱藏全部”按鈕時,所有TV2的可見性都應為“消失”,並且在“全部顯示”按鈕上的可見性相同。
這是我的代碼:
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;
}
}
}
活動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>
這是我的問題的屏幕截圖:
這是運行項目時的初始狀態:
現在,我已通過顯示/隱藏詳細信息按鈕在列表視圖中單擊來為aa和bb隱藏TV2:
這是向下滾動的部分:
當我滾動回到頂部時,對應於aa和bb的textviews(TV2)再次可見,而之前給定了它。
您的適配器不知道該視圖是可見的還是不可見的,因為該行在離開屏幕時會被破壞,而在再次進入絲網時會被重新創建。 我建議您創建一個新的容器類,如下所示:
public class MyContainer {
public boolean visible = true;
public String text;
public MyContainer(String text) {
this.text = text;
}
}
然后向適配器傳遞MyContainer實例的ArrayList並重寫getView()
以基於MyContainer實例visible
的布爾值和基於String text
的TextView文本來設置可見性。
要設置視圖的可見性,請創建一個方法setvisible(int index)
該方法setvisible(int index)
更改MyContainers數組列表中給定索引處的布爾值(調用notifyDataSetChanged()
以重繪litview。
這樣,適配器就可以記住夾層視圖。
注意
最好不要為適配器全局聲明一個數組列表,而是在構造函數中傳遞一個數組列表。
您可以嘗試的方法:
全局聲明一個TextView數組,
Button show, hide;
ListView lv;
ArrayList<String> al1;
MyAdapter ma;
TextView[] tva;
然后使用onCreate方法中的“詳細信息” TextViews初始化此數組。
然后在您的onclicklister中更改這些全局實例的可見性。
實例僅存在於按鈕的onClicklistener方法范圍內的事實可能是其效果不持久的原因嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.