У меня есть TextView
и ListView
в моей деятельности. ListView
intially set to Visibility = gone
Мне нужна функциональность, например, если пользователь нажимает на TextView
, ListView
связывается и будет отображаться на `Wrap_Content '. У меня есть писать код до сих порSet ListView Height Programmatically
Мой 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 Code
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);
}
В этом коде я звоню Expand
функция ViewAnimationUtils
класс. В функции расширения я установил высоту макета Wrap_Content
, но высота рендеринга Listview покажет только один элемент. Listview
имеет 8 пунктов на bind.
Как установить высоту ListView
, чтобы показать все элементы в списке?
Он работает Rushi .. Большое спасибо !! –
cool.Это здорово, что он работал даже через год. :) –