我在Android中有两个Layout
ListView
。第一个列表视图有一个带有三个TextView
的自定义适配器。第二个ListView
还有一个自定义Adapter
,其中包含TextView
个ListView
。首先,第二个TextView
是不可见的。点击第一个ListView
的{{1}}后,第二个ListView
就会显示。
在第一个列表的customAdapter
类视图中我有TextView
的onclick监听器,其中我将第二个ListView
的可见性设置为{{1 }。
我可以在日志中看到正确更改的可见性。但它没有反映在UI中。
包含两个列表视图的主要布局true
:
search_by_bus_id.xml
第一个<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg_dots" >
<FrameLayout
android:id="@+id/listmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listbusid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollingCache="true" />
</FrameLayout>
<FrameLayout
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#60000000"
>
<ListView
android:id="@+id/aboutBus"
android:layout_width="wrap_content"
android:layout_height="458dp"
android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp" >
</ListView>
</FrameLayout>
</RelativeLayout>
(ListView
)
list_item3.xml
第一个LsstView的适配器类
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@color/blue"
card_view:cardCornerRadius="8dp" >
<RelativeLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#42BF51"
android:orientation="vertical" >
<TextView
android:id="@+id/BusNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="124"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Bus1Time"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="4:28pm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/Bus2Time"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/Bus1Time"
android:text="4:28pm"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
listView2适配器:
public class BusAdapter extends ArrayAdapter<Bus> implements OnClickListener {
private ArrayList<Bus> objects;
static BusDetailsAdapter bus_details_adapter;
static ArrayList<AboutBus> busdetails = new ArrayList<AboutBus>();
public BusAdapter(Context context, int textViewResourceId,
ArrayList<Bus> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item3, null);
}
Bus i = objects.get(position);
if (i != null) {
TextView busNo = (TextView) v.findViewById(R.id.BusNo);
TextView departure1 = (TextView) v.findViewById(R.id.Bus1Time);
TextView departure2 = (TextView) v.findViewById(R.id.Bus2Time);
if (busNo != null) {
busNo.setText(i.getBus());
}
if (departure1 != null) {
departure1.setText(i.getDeparture1());
}
if (departure2 != null) {
departure2.setText(i.getDeparture2());
}
busNo.setTag(i);
busNo.setOnClickListener(this);
departure1.setTag(i);
departure1.setOnClickListener(this);
departure2.setTag(i);
departure2.setOnClickListener(this);
}
// the view must be returned to our activity
return v;
}
@Override
public void onClick(View v) {
LayoutInflater mInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v2 = (View) mInflater.inflate(R.layout.search_by_bus_id, null);
ListView listView2 = (ListView) v2.findViewById(R.id.aboutBus);
switch (v.getId()) {
case R.id.BusNo:
if (listView2 != null) {
listView2.setVisibility(View.VISIBLE);
Log.d("Visibilty", " " + listView2.getVisibility());
}
this.notifyDataSetChanged();
break;
case R.id.Bus1Time:
if (listView2 != null) {
listView2.setVisibility(View.VISIBLE);
Log.d("Visibilty", " " + listView2.getVisibility());
}
this.notifyDataSetChanged();
break;
default:
break;
}
}
}
}
ListItem for 2nd ListView(list_item4.xml):
public class BusDetailsAdapter extends ArrayAdapter<AboutBus> {
private ArrayList<AboutBus> objects;
public BusDetailsAdapter(Context context, int textViewResourceId,
ArrayList<AboutBus> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
// assign the view we are converting to a local variable
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item4, null);
}
AboutBus i = objects.get(position);
if (i != null) {
TextView Operator = (TextView) v
.findViewById(R.id.Operator);
TextView Load = (TextView) v
.findViewById(R.id.Load);
if (Operator != null) {
Operator.setText(i.getOperator());
}
if (Load != null) {
Load.setText(i.getLoad());
}
}
return v;
}
Onclick的一部分:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:orientation="vertical" >
<TextView
android:id="@+id/OperatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Operator : "
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/OperatorText"
android:layout_margin="15dp"
android:text="dummy"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/LoadText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/OperatorText"
android:layout_margin="15dp"
android:text="Load : "
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Operator"
android:layout_below="@+id/Operator"
android:layout_marginTop="15dp"
android:text="dummy"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
答案 0 :(得分:0)
您应该尝试从主容器布局中查找listview元素,而不是给另一个布局充气。
我的意思是,对从活动/片段传递到适配器的原始视图对象执行findViewById。
答案 1 :(得分:0)
我认为它是因为listview它属于活动布局而你没有传递活动的引用来提高可见性或者你可能尝试这样的事情
adapter = new (ActivityMain.this, some paramnts)
并且适配器获取引用并更改可见性
答案 2 :(得分:0)
我认为您应该声明search_by_bus_id.xml的FrameLayout并在相关的Java文件中使用它来禁用和启用ListView。只需保持Visible两个LinearLayout并尝试在Java文件中更改Framelayout的Visibilty