我需要在用户向上滚动时隐藏错误,并在用户向下滚动时显示回来。
以下是代码:
ScrollView scrollViewContainer = (ScrollView) rootView.findViewById(R.id.scrollViewContainer);
mGestureDetector = new GestureDetector(scrollViewContainer.getContext(), new OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
System.out.println("GestureDetector --> onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
System.out.println("GestureDetector --> onShowPress");
// TODO Auto-generated method stub
for (int i = 0; i < editTextList.size(); i++) {
String error = editTextError.get(i);
editTextList.get(i).setError(error);
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("GestureDetector --> onSingleTapUp");
for (int i = 0; i < editTextList.size(); i++) {
//list with errors
String error = editTextError.get(i);
//list with edit text
editTextList.get(i).setError(error);
editTextList.get(i).setFocusableInTouchMode(true);
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("GestureDetector --> onLongPressTapUp");
for (int i = 0; i < editTextList.size(); i++) {
String error = editTextError.get(i);
editTextList.get(i).setError(error);
}
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
LogS.i("GestureDetector --> onFling");
// TODO Auto-generated method stub
for (FloatLabeledEditText floatLabeledEditText : editTextList) {
floatLabeledEditText.setError(null); }
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
LogS.i("GestureDetector --> onScroll");
for (FloatLabeledEditText floatLabeledEditText : editTextList) {
floatLabeledEditText.setError(null);
floatLabeledEditText.setFocusableInTouchMode(false);
}
return false;
}
});
mGestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mIsScrolling ) {
System.out.println("OnTouchListener --> onTouch ACTION_UP");
for (FloatLabeledEditText floatLabeledEditText : editTextList) {
floatLabeledEditText.setError(null);
}
Log.d("OnTouchListener --> onTouch ACTION_UP");
mIsScrolling = false;
}
}
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(mIsScrolling ) {
System.out.println("OnTouchListener --> onTouch ACTION_Down");
for (FloatLabeledEditText floatLabeledEditText : editTextList) {
floatLabeledEditText.setError(null);
}
mIsScrolling = false;
}
}
return false;
}
};
scrollViewContainer.setOnTouchListener(mGestureListener);
答案 0 :(得分:0)
activity_scroll_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrv"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/container2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#1E1E1E"
android:baselineAligned="false"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/black"
android:baselineAligned="false"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/white"
android:baselineAligned="false"
android:orientation="vertical" >
<Button
android:id="@+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/darker_gray"
android:baselineAligned="false"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
ScrollLayoutActivity
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
public class ScrollLayoutActivity extends Activity {
private static String TAG = ScrollLayoutActivity.class.getSimpleName();
private LinearLayout mTopLayout;
private LinearLayout mMiddleLayout;
private LinearLayout mBottomLayout;
private ScrollView mScrollView;
private boolean possitionTop;
private boolean possitionMiddle = true;
int mLayoutHeight;
float mDeviceHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testlayouts);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// final float height=displayMetrics.heightPixels/displayMetrics.xdpi;
// device height in pixels
mDeviceHeight = displayMetrics.heightPixels;
mLayoutHeight = (int) mDeviceHeight / 3;
mScrollView = (ScrollView) findViewById(R.id.scrv);
mTopLayout = (LinearLayout) findViewById(R.id.top);
mMiddleLayout = (LinearLayout) findViewById(R.id.middle);
mBottomLayout = (LinearLayout) findViewById(R.id.bottom);
mTopLayout.setLayoutParams(new LinearLayout.LayoutParams(mTopLayout
.getLayoutParams().width, mLayoutHeight));
mBottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
mBottomLayout.getLayoutParams().width, mLayoutHeight));
mMiddleLayout.setLayoutParams(new LinearLayout.LayoutParams(
mMiddleLayout.getLayoutParams().width, (int) mDeviceHeight));
mScrollView.setHorizontalFadingEdgeEnabled(false);
mScrollView.setVerticalFadingEdgeEnabled(false);
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(0, mLayoutHeight);
}
});
// findViewById(R.id.button).setOnClickListener(new
// View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// Toast.makeText(ScrollLayoutActivity.this, "height : " +
// mDeviceHeight, Toast.LENGTH_SHORT).show();
// }
// });
Toast.makeText(this, "Device Height : " + mDeviceHeight,
Toast.LENGTH_SHORT).show();
}
public boolean dispatchTouchEvent(MotionEvent ev) {
return mGestureDetector.onTouchEvent(ev);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
String swipe = "";
float sensitvity = 100;
if ((e1.getX() - e2.getX()) > sensitvity) {
swipe += "Swipe Left\n";
} else if ((e2.getX() - e1.getX()) > sensitvity) {
swipe += "Swipe Right\n";
} else {
swipe += "\n";
}
if ((e1.getY() - e2.getY()) > sensitvity) {
swipe += "Swipe Up\n";
if (!possitionMiddle && possitionTop) {
mScrollView.scrollBy(0, mLayoutHeight);
possitionTop = false;
possitionMiddle = true;
} else if (possitionMiddle && !possitionTop) {
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
possitionTop = false;
possitionMiddle = false;
}
} else if ((e2.getY() - e1.getY()) > sensitvity) {
swipe += "Swipe Down\n";
if (possitionMiddle && !possitionTop) {
mScrollView.fullScroll(ScrollView.FOCUS_UP);
possitionTop = true;
possitionMiddle = false;
}
if (!possitionMiddle && !possitionTop) {
mScrollView
.scrollTo(mScrollView.getBottom(), mLayoutHeight);
possitionTop = false;
possitionMiddle = true;
}
} else {
swipe += "\n";
}
Log.d(TAG, swipe);
return super.onFling(e1, e2, velocityX, velocityY);
}
};
GestureDetector mGestureDetector = new GestureDetector(
simpleOnGestureListener);
}
答案 1 :(得分:0)
您可以检查某个视图是否可见,然后显示/隐藏错误。 那是你在找什么?