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

时间:2014-12-11 10:13:00

标签: android swipe

我在scrollview中有很多自定义视图。我在customview上实现了滑动功能。当我将自定义视图向左滑动时,scrollview也会垂直移动。我不希望滚动视图在滑动时垂直移动。我触摸的Action_Cancel事件是滚动视图,我必须停止。我试图阻止它,但它无法正常工作。

下面是

    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;
    }

}

1 个答案:

答案 0 :(得分:1)

onScroll()

把这个 -

ViewParent parent = getParent();
     if(parent!= null){
     parent.requestDisallowOnInterceptTouchEvent(true);
}