我是一个有用的助手,可以翻译文本。
这段文字描述了一个Android应用程序中的布局和功能。以下是英语到中文的翻译:
我在我的活动中有一个TextView和ListView。ListView最初设置为Visibility = gone,我需要一种功能,就像如果用户点击TextView,ListView会绑定并显示为“Wrap_Content”。到目前为止,我已经编写了代码。
我的XML文件
在这段代码中,我调用了
如何设置
这段文字描述了一个Android应用程序中的布局和功能。以下是英语到中文的翻译:
我在我的活动中有一个TextView和ListView。ListView最初设置为Visibility = gone,我需要一种功能,就像如果用户点击TextView,ListView会绑定并显示为“Wrap_Content”。到目前为止,我已经编写了代码。
我的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtDate"
android:textSize="50dp"
android:background="@color/colorSplash"
android:text="20-11-2016"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lstDesc"
android:visibility="gone" />
Java代码
private void BindBookedList(){
final ArrayList<String> arr = new ArrayList<>();
arr.add("21-11-2016");
arr.add("22-11-2016");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_collapse, arr){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_collapse, null);
}
final TextView txtDate = (TextView) v.findViewById(R.id.txtDate);
final ListView lstDesc = (ListView) v.findViewById(R.id.lstDesc);
String strItemDAta = arr.get(position);
txtDate.setText(strItemDAta);
String[] arrContries = getResources().getStringArray(R.array.ListOfAirport);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getApplicationContext(), R.layout.spinner_item, arrContries);
lstDesc.setAdapter(adapter);
txtDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
com.kommlabs.pikmybox.MyUtlilites.ViewAnimationUtils.expand(lstDesc);
}
});
return v;
}
};
ListView listBooked = ((ListView) findViewById(R.id.lstBookedPackages));
listBooked.setAdapter(adapter);
}
ViewAnimationUtils类
public class ViewAnimationUtils {
static View view;
public static void expand(View v) {
view = v;
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
v.setVisibility(View.VISIBLE);
}
在这段代码中,我调用了
ViewAnimationUtils
类的Expand
函数。在扩展函数中,我将布局高度设置为Wrap_Content
,但是Listview只渲染了足以显示一个项目的高度。绑定时Listview
有8个项目。如何设置
ListView
的高度以显示列表中的所有项目?