Android 滚动视图在水平滑动行时垂直移动

Android 滚动视图在水平滑动行时垂直移动,android,swipe,Android,Swipe,我在scrollview中有许多customview。我已经在customview上实现了滑动功能。当我向左滑动customview时,scrollview也会垂直移动。我不希望滑动时滚动视图垂直移动。我想动作\取消触摸事件将进入滚动视图,我必须停止。我试图阻止它,但它不起作用。 以下是 package com.AA.KK.customui; import java.util.List; import org.apache.http.NameValuePair; import org

我在scrollview中有许多customview。我已经在customview上实现了滑动功能。当我向左滑动customview时,scrollview也会垂直移动。我不希望滑动时滚动视图垂直移动。我想动作\取消触摸事件将进入滚动视图,我必须停止。我试图阻止它,但它不起作用。 以下是

    package com.AA.KK.customui;

import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GestureDetectorCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.AA.ax4mobile.AAPreferences;
import com.AA.ax4mobile.Constants;
import com.AA.ax4mobile.Session;
import com.AA.ax4mobile.Utils;
import com.AA.ax4mobile.network.Network;
import com.AA.ax4mobile.network.NetworkResponseListener;
import com.AA.KK.R;
import com.AA.KK.activities.KKBaseActivity;
import com.AA.KK.activities.PDFViewerActivity;

public class MailLayout extends RelativeLayout implements OnClickListener, OnTouchListener,
        GestureDetector.OnGestureListener {

    private CheckBox checkBox;
    private TextView title;
    private TextView fileSize;
    private ImageView fileIcon;
    private ImageView tray;
    private String url;
    private String fileId;
    private ViewGroup swipeView;
    private boolean isPreview;
    private boolean isSelected = true;
    private GestureDetectorCompat mDetector;
    private static final int SWIPE_OFFSET = 20;
    private static final int ANIMATION_OFFSET= 30;

    public MailLayout(Context context) {
        super(context);

        init(context);
        // TODO Auto-generated constructor stub
    }

    @SuppressLint("NewApi")
    private void init(Context context) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.activity_mail_layout, this);
        LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        setLayoutParams(linLayoutParam);
        swipeView = (RelativeLayout) findViewById(R.id.swipe);
        swipeView.setOnTouchListener(this);
        title = (TextView) findViewById(R.id.file_name);
        fileIcon = (ImageView) findViewById(R.id.file_image);
        fileSize = (TextView) findViewById(R.id.file_size);
        tray = (ImageView) findViewById(R.id.clickme);
        tray.setOnClickListener(this);
        checkBox = (CheckBox) findViewById(R.id.chk_checkBox);
        mDetector = new GestureDetectorCompat(Utils.getInstance().getContext(), this);
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public boolean isPreview() {
        return isPreview;
    }

    public void setPreview(boolean isPreview) {
        this.isPreview = isPreview;
    }

    public ViewGroup getSwipeView() {
        return swipeView;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox) {
        this.checkBox = checkBox;
    }

    public void setTitleText(String text) {
        title.setText(text);
    }

    public String getTitleText() {
        return (String) title.getText();
    }

    public void setFileSize(String text) {
        fileSize.setText(text);
    }

    public void setChecked(boolean checked) {
        checkBox.setChecked(checked);
    }

    public boolean getChecked() {
        return checkBox.isChecked();
    }

    public void setFileIcon(String fileName) {
        if (fileName.contains(".pdf") || fileName.contains(".PDF")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_pdf_selected);
        } else if (fileName.contains(".docx") || fileName.contains(".DOCX")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_docx_selected);
        } else if (fileName.contains(".doc") || fileName.contains(".DOC")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_doc_selected);
        } else if (fileName.contains(".ppt") || fileName.contains(".PPT")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_ppt_selected);
        } else if (fileName.contains(".pptx") || fileName.contains(".PPTX")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_pptx_selected);
        } else if (fileName.contains(".png") || fileName.contains(".PNG")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_png_selected);
        } else if (fileName.contains(".gif") || fileName.contains(".GIF")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_gif_selected);
        } else if (fileName.contains(".csv") || fileName.contains(".CSV")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_csv_selected);
        } else if (fileName.contains(".bmp") || fileName.contains(".BMP")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_bmp_selected);
        } else if (fileName.contains(".txt") || fileName.contains(".TXT")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_txt_selected);
        } else if (fileName.contains(".jpg") || fileName.contains(".jpeg") || fileName.contains(".JPG")
                || fileName.contains(".JPEG")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_jpg_selected);
        } else if (fileName.contains(".xls") || fileName.contains(".XLS")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_xls_selected);
        } else if (fileName.contains(".xlsx") || fileName.contains(".XLXS")) {
            fileIcon.setBackgroundResource(R.drawable.ic_file_xlsx_selected);
        } else {
            fileIcon.setBackgroundResource(R.drawable.ic_file_selected);
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
        checkBox.setOnCheckedChangeListener(listener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.clickme:
            doAddToTray(getFileId());
            break;
        }

    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
        // TODO Auto-generated method stub

    }

    public String getFileId() {
        return fileId;
    }

    public static void dismissProgress() {
        Message msg = new Message();
        msg.what = Constants.UIMSG_HIDE_PROGRESSDIALOG;
        Bundle data = new Bundle();
        data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
        msg.setData(data);
        Session.getInstance().getMsgHandler().sendMessage(msg);
    }

    public static void showProgress() {
        String message = Utils.getInstance().getContext().getString(R.string.loading);
        Message msg = new Message();
        msg.what = Constants.UIMSG_SHOW_PROGRESSDIALOG;
        Bundle data = new Bundle();
        data.putString("message", message);
        data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
        msg.setData(data);
        Session.getInstance().getMsgHandler().sendMessage(msg);
    }

    public static void doAddToTray(final String ids) {
        AAPreferences settings = AAPreferences.getPreferences(Utils.getInstance().getContext());
        String accToken = settings.getString(Constants.REQUEST_KEY_ACCESS_TOKEN, null);
        String servername = settings.getString(Constants.REQUEST_KEY_SERVERNAME, null);

        JSONObject req = new JSONObject();
        Network net = Network.getInstance();

        try {
            req.accumulate(Constants.REQUEST_KEY_ACCESS_TOKEN, accToken);
            req.accumulate(Constants.REQUEST_KEY_SERVERNAME, servername);
            req.accumulate(Constants.REQUEST_KEY_ID, ids);
            req.accumulate(Constants.REQUEST_KEY_PARENT_ID, "");
            showProgress();
            NetworkResponseListener listener = new NetworkResponseListener() {
                @Override
                public void handleSuccessResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
                    dismissProgress();
                    Toast.makeText(Utils.getInstance().getContext(), R.string.file_added_to_move_tray,
                            Toast.LENGTH_LONG).show();
                    String count = null;
                    try {
                        JSONObject json = response.getJSONObject("payload");

                        count = json.getString("total_count");
                        if (!TextUtils.isEmpty(count))
                            Utils.getInstance().saveTrayCount(count);
                    } catch (JSONException e) {

                        e.printStackTrace();
                    }

                    Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
                    LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);

                }

                @Override
                public void handleErrorResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
                    dismissProgress();
                    String count = null;
                    try {
                        JSONObject json = response.getJSONObject("payload");

                        count = json.getString("total_count");
                        Utils.getInstance().saveTrayCount(count);
                    } catch (JSONException e) {

                        e.printStackTrace();
                    }
                    Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
                    LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);

                    Message msg = Network.defaultErrorManager(request, response);
                    if (msg != null) {
                        KKBaseActivity.defaultUIHandler.sendMessage(msg);
                    }
                }
            };
            net.linkTray(req, listener);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    };

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // TODO Auto-generated method stub
        Log.e("onFling", "event"+e1.getAction());
        if (swipeView.getX() > -(tray.getWidth() + SWIPE_OFFSET)) {
            swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
        } else {
            if (isPreview) {
                swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
            } else {
                swipeView.animate().translationX(-(tray.getWidth() + SWIPE_OFFSET)).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
            }

        }

        return false;

    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // TODO Auto-generated method stub
        if (isSelected) {
            if (swipeView.getX() <= 0)
                swipeView.setX(e2.getX() - e1.getX() + swipeView.getX());
        }
        return true;

    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        String s = getUrl();
        if (getUrl() == "" || getUrl() == null || getUrl().equalsIgnoreCase("null")) {
            Toast.makeText(Utils.getInstance().getContext(), "File is not viewable", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(Utils.getInstance().getContext(), PDFViewerActivity.class);
            intent.putExtra("filename", getTitleText());
            intent.putExtra("view_url", getUrl());
            intent.putExtra("preview_from_notification", true);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Utils.getInstance().getContext().startActivity(intent);
        }
        return true;

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Log.e("onTouch", "event"+event.getAction());
        boolean flag;
        if (isPreview) {
            tray.setVisibility(View.INVISIBLE);
        } else {
            tray.setVisibility(View.VISIBLE);
        }

        if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            flag = onFling(event, event, 0f, 0f);

        } else {
            flag = mDetector.onTouchEvent(event);
        }
        return flag;
    }


  /*  @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.e("onTouchEvent", "event"+event.getAction());
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        Log.e("dispatchTouchEvent", "event"+ev.getAction());
       if(ev.getAction()== 3)
           return false;
       else
       return super.dispatchTouchEvent(ev);
    }*/

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        Log.e("onInterceptTouchEvent", "event"+ev.getAction());

        return true;
    }

}
package com.AA.KK.customui;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.Message;
导入android.support.v4.content.LocalBroadcastManager;
导入android.support.v4.view.GestureDetectorCompat;
导入android.text.TextUtils;
导入android.util.Log;
导入android.view.GestureDetector;
导入android.view.LayoutInflater;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.view.OnTouchListener;
导入android.view.ViewGroup;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.AA.ax4mobile.AA首选项;
导入com.AA.ax4mobile.Constants;
导入com.AA.ax4mobile.Session;
导入com.AA.ax4mobile.Utils;
导入com.AA.ax4mobile.network.network;
导入com.AA.ax4mobile.network.NetworkResponseListener;
进口com.AA.KK.R ;;
导入com.AA.KK.activities.KKBaseActivity;
导入com.AA.KK.activities.PDFViewerActivity;
公共类MailLayout扩展了RelativeLayout实现OnClickListener、OnTouchListener、,
手势检测器{
私有复选框;
私有文本视图标题;
私有文本视图文件大小;
私有图像视图文件图标;
专用图像浏览托盘;
私有字符串url;
私有字符串fileId;
私有视图组swipeView;
私人审查;
私有布尔值isSelected=true;
私人手势检测器;
专用静态最终整数滑动偏移=20;
私有静态最终int动画_偏移=30;
公共邮件布局(上下文){
超级(上下文);
init(上下文);
//TODO自动生成的构造函数存根
}
@SuppressLint(“新API”)
私有void init(上下文){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
充气机。充气(R.layout.activity\u mail\u layout,this);
LayoutParams linLayoutParam=新的LayoutParams(LayoutParams.MATCH_父级,LayoutParams.WRAP_内容);
setLayoutParams(linLayoutParam);
swipeView=(RelativeLayout)findViewById(R.id.swipe);
swipeView.setOnTouchListener(这个);
title=(TextView)findViewById(R.id.file\u name);
fileIcon=(ImageView)findViewById(R.id.file\u image);
fileSize=(TextView)findViewById(R.id.file\u size);
托盘=(图像视图)findViewById(R.id.clickme);
tray.setOnClickListener(此);
复选框=(复选框)findViewById(R.id.chk_复选框);
mDetector=new GestureDetectorCompat(Utils.getInstance().getContext(),this);
}
公选{
选举产生的回报;
}
公共无效设置已选(布尔值已选){
this.isSelected=isSelected;
}
公共审查(){
返回审核;
}
公共void setPreview(布尔值isPreview){
this.isPreview=isPreview;
}
公共视图组getSwipeView(){
返回swipeView;
}
公共复选框getCheckBox(){
返回复选框;
}
公共无效设置复选框(复选框){
this.checkBox=复选框;
}
公共void setTitleText(字符串文本){
title.setText(文本);
}
公共字符串getTitletText(){
返回(字符串)title.getText();
}
public void setFileSize(字符串文本){
fileSize.setText(文本);
}
公共void setChecked(布尔值已选中){
checkBox.setChecked(选中);
}
公共布尔getChecked(){
返回checkBox.isChecked();
}
public void setFileIcon(字符串文件名){
if(fileName.contains(“.pdf”)| | fileName.contains(“.pdf”)){
fileIcon.setBackgroundResource(R.drawable.ic\u文件\u pdf\u选中);
}else if(fileName.contains(“.docx”)| | fileName.contains(“.docx”)){
fileIcon.setBackgroundResource(R.drawable.ic\u文件\u docx\u选中);
}else if(fileName.contains(“.doc”)| fileName.contains(“.doc”)){
fileIcon.setBackgroundResource(选择R.drawable.ic\u文件\u文档);
}else if(fileName.contains(“.ppt”)| | fileName.contains(“.ppt”)){
fileIcon.setBackgroundResource(选择R.drawable.ic\u文件\u ppt\u);
}else if(fileName.contains(“.pptx”)| | fileName.contains(“.pptx”)){
fileIcon.setBackgroundResource(R.drawable.ic_文件\u pptx_选中);
}else if(fileName.contains(“.png”)| | fileName.contains(“.png”)){
fileIcon.setBackgroundResource(选择R.drawable.ic\u文件png\u);
}else if(fileName.contains(“.gif”)| | fileName.contains(“.gif”)){
fileIcon.setBackgroundResource(选择R.drawable.ic\u文件\u gif\u);
}else if(fileName.contains(“.csv”)| | fileName.contains(“.csv”)){
fileIcon.setBackgroundResource(R.drawable.ic\u文件\u csv\u选中);
}else if(fileName.contains(“.bmp”)| | fileName.contains(“.bmp”)){
fileIcon.setBackgroundResource(选择R.drawable.ic\u文件\u bmp\u);
}else if(fileName.contains(“.txt”)| | fileName.contains(“.txt”)){
fileIcon.setBackgroundResource(R.drawable.ic_文件_txt_选中);
}else if(fileName.contains(“.jpg”)| | fileName.contains(“.jpeg”)| | fileName.contains(“.jpg”)
ViewParent parent = getParent();
     if(parent!= null){
     parent.requestDisallowOnInterceptTouchEvent(true);
}