Reputation: 225
I have tried to scroll the ListView up when the event item is more than the space of the ListView but it fails. I would like to know if I can make the ListView scrollable without adding any listener. Thank you.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:focusableInTouchMode="true"
tools:context="com.example.ssycorpapp.SecondActivity" >
<TabHost
android:id="@+id/tabhost2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/events"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SearchView
android:id="@+id/search"
android:queryHint="@string/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</SearchView>
<RelativeLayout
android:id="@+id/titleBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_below="@id/search" >
<TextView
android:id="@+id/resultName"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/event"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/resultDate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/resultTime"
android:layout_toStartOf="@+id/resultTime"
android:text="@string/eventDate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@id/resultTime"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="@string/eventTime"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<ListView
android:id="@+id/event_list"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@id/titleBar"
android:layout_above="@+id/DelAll" />
<Button
android:id="@+id/DelAll"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:text="@string/DelAll" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/EventName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/event" />
<EditText
android:id="@+id/eveName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
</EditText>
<TextView
android:id="@+id/EventDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/eventDate" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="match_parent"
android:layout_height="158dp" />
<TextView
android:id="@+id/EventTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/eventTime" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="109dp" />
<Button
android:id="@+id/Tim"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Set" />
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Upvotes: 0
Views: 931
Reputation: 11873
If you don't want to use any listener then you can simply use a custom ListView like this,
public class CustomListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
private int listViewTouchAction;
public CustomListView (Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
Then use in your layout,
<CustomListView
android:id="@+id/event_list"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@id/titleBar"
android:layout_above="@+id/DelAll" />
Upvotes: 1