我在listView项目布局中有一个隐藏的textView,当单击该项目时,我正在显示隐藏的textView。但是,当我单击另一个项目时,我希望再次隐藏项目中的其他可见textview。那么如何访问这个其他项目的textView呢?
theListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
itemView2.setVisibility(View.VISIBLE);
itemView2.setBackgroundColor(color.white);
Double lat = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLat());
Double lng = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLng());
LatLng itemLocation = new LatLng(lat,lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(itemLocation, 18 ));
}
});
item_layout.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/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="7dp"
android:textSize="15sp"
android:textColor="@color/loginblue"></TextView>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="7dp"
android:textSize="12sp"
android:visibility="gone"
android:textColor="@color/loginblue"></TextView>
</LinearLayout>
答案 0 :(得分:1)
您需要记住ListView中所选项目的索引/位置。然后,如果选择了其他项目,则测试是否已存在其他所选项目。如果是这样,您可以像这样获得该项目的视图:
// Hide previously selected text view
if (selectedIndex > -1) {
int viewIndex = selectedIndex - listView.getFirstVisiblePosition();
// if previously selected list item is still visible, hide its text view
if (viewIndex > -1 && viewIndex < listView.getChildCount()) {
View itemView = listView.getChildAt(viewIndex);
TextView textView = itemView.findViewById(R.id.textView2);
textView.setVisibility(View.GONE);
}
}
// Now you make the newly selected text view visible and remember the selected item
selectedIndex = position;
selectedIndex
是点击侦听器或任何其他周围类的实例变量。使用-1
初始化它,因为在开头没有选择任何项目。
显然你可以优化代码。它只是为了澄清完成任务的基本步骤。
答案 1 :(得分:1)
在活动中将其定义为成员变量,
int clickedPosition = -1;
然后使用此代码,
heListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(clickedPosition!= position && clickedPosition != -1){
clickedPosition = position;
View previousView = heListView.getChildAt(clickedPosition);
TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
itemView2.setVisibility(View.INVISIBLE);
}
TextView itemView2 = (TextView)view.findViewById(R.id.textView2);
itemView2.setVisibility(View.VISIBLE);
itemView2.setBackgroundColor(color.white);
Double lat = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLat());
Double lng = Double.parseDouble(((KioskInfo)parent.getItemAtPosition(position)).getLng());
LatLng itemLocation = new LatLng(lat,lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(itemLocation, 18 ));
}
});