HorizontalScrollView怎么监听滑动方向和禁用滑动
重写onTouchEvent,返回false,试一下,
控制方向
.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(MotionEvent.ACTION_DOWN==event.getAction()){
mPosX = (int)event.getX();
mPosY = (int)event.getY();
}
if (MotionEvent.ACTION_MOVE == event.getAction()) {
mCurrentPosX = (int)event.getX()-mPosX;
mCurrentPosY = (int)event.getY()-mPosY;
mPosX = (int)event.getX();
mPosY = (int)event.getY();
}
if (mCurrentPosX – mPosX 》 0 && Math.abs(mCurrentPosY – mPosY) 《 10)
{
Log.e(““, “向右的按下位置“+mPosX+“移动位置“+mCurrentPosX);
}
else if (mCurrentPosX – mPosX 《 0 && Math.abs(mCurrentPosY – mPosY) 《 10
)
{
Log.e(““, “向左的按下位置“+mPosX+“移动位置“+mCurrentPosX);
}
else if (mCurrentPosY – mPosY 》 0 && Math.abs(mCurrentPosX – mPosX) 《 10)
{
Log.e(““, “向下的按下位置“+mPosX+“移动位置“+mCurrentPosX);
}
else if (mCurrentPosY – mPosY 《 0 && Math.abs(mCurrentPosX – mPosX) 《 10)
{
Log.e(““, “向上的按下位置“+mPosX+“移动位置“+mCurrentPosX);
}
return false;
}
});
Android ListView的Item里有horizontalScrollView,怎么判断是点击事件并拦截,如果是横向滑动就放过
HorizontalScrollView
嵌套ScrollView导致滑动卡的情况,急横向水平滑动的View和垂直水平滑动的View,都在接收处理滑动时间,但是这种情况下触摸事件就会发生
冲突。导致滑动非常卡,甚至出现程序停止响应。这种情况下我们需要重写view。下面是两个简单的例子,重写水平滑动View只接收水平方向上滑动的事
件。我们使用手势GestureDetector来作区分
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
public class CustomHScrollView extends HorizontalScrollView{
private GestureDetector mGestureDetector;
private View.OnTouchListener mGestureListener;
private static final String TAG = “CustomHScrollView“;
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
*
*/
public CustomHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
*/
public CustomHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
/**
* @function CustomHScrollView constructor
* @param context Interface to global information about an application environment.
* @param attrs A collection of attributes, as found associated with a tag in an XML document.
* @param defStyle style of view
*/
public CustomHScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(new HScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we’re scrolling in the y direction
class HScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceX) 》 Math.abs(distanceY)) {
return true;
}
return false;
}
}
}
怎么让horizontalscrollview根据所选的内容自动滚动
关键是怎么设置他不滑动呢,这个控件有设置不滑动这个方法吗?我看了API里没有啊
使用viewpager来实现你的需求 继承HorizontalScrollView,设置x边界,当超出的时候就不在滚动。 其实你现在这样的效果挺好的!!!
android horizontalscrollview怎么动态加载网络数据
horizontalscrollview只是一个可以让里面的子控件(有且仅有一个),可以水平滑动的控件而已,如果要动态的加载数据,可以在horizontalscrollview中去添加一个LinearLayout或者RelativeLayout,再向里面加一些比如Textview、ImageView等;或者不使用horizontalscrollview,直接用ListView、Gridview来加载数据
如何获取horizontalscrollview滚动距离
在与scrollview同级建个和中部标签选卡一样的布局,开始的时候设置成隐藏View.GONE,当scrollview滚动到中部位置的时候让它显示出来就OK了,当小于中部位置时再把它隐藏。我就这么做的,不过我当时不是scrollview而是ListView
怎么设置HorizontalScrollView默认位子
经过我的实验,一下代码可以完美解决问题。: new Handler().postDelayed((new Runnable() { @Override public void run() { sv_price.scrollTo(((RadioButton)rg_price.getChildAt(housePriceIndex)).getLeft()-100,0); } }),5);其中 ((RadioButton)rg_price.getChildAt(housePriceIndex)).getLeft()-100是我想把HorizontalScrollView sv_price滚动到的最终位置。最后的一个5是延迟的时间。正如9楼所说,我刚开始用的 handler.post 果断无效!这个好用!!
如何在 HorizontalScrollView 中设置线性布局宽度
你需要一些动态代码来衡量屏幕的宽度和 programtically ,然后设置每个 linearlayout 的宽度 (icon1、 icon2 等) 对这种宽度的 1/4。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, LayoutParams.MATCH_PARENT);
LinearLayout icon1 = (LinearLayout) findViewById(R.id.icon1);
icon1.setLayoutParams(params);
//etc
HorizontalScrollView初始如何滚动到某一值
经过我的实验,一下代码可以完美解决问题。: new Handler().postDelayed((new Runnable() { @Override public void run() { sv_price.scrollTo(((RadioButton)rg_price.getChildAt(housePriceIndex)).getLeft()-100,0); } }),5);其中 ((RadioButton)rg_price.getChildAt(housePriceIndex)).getLeft()-100是我想把HorizontalScrollView sv_price滚动到的最终位置。最后的一个5是延迟的时间。正如9楼所说,我刚开始用的 handler.post 果断无效!这个好用!!
android 中ScrollView 内嵌HorizontalScrollView滚动的问题
同学,你看下srollview的说明,scrollview只能有一个子view,也就是说只能包含一个linearlayout或者一个其他什么view,按照你这种情况,你应该是先scrollview包含一个数值的linearlayout,然后其他的HorizontalScrollView 和LinearLayout都在linearlayout下,linearlayout的宽度为wrapcontent。
因为我觉得你想的是ScrollView 是竖向滑动,而HorizontalScrollView 是横向滑动,并且你的布局感觉像是按照竖直LinearLayout来的,所以我才推荐你用scrollview包含一个数值的linearlayout,然后其他的HorizontalScrollView 和LinearLayout都在linearlayout下。