你真的理解 getLocationInWindow 了嗎?

碼無止境發表於2019-03-01

近期專案比較忙,今日才有時間給大家分享android的一些實用的知識~

what is getLocationInWindow

android中一種獲取view座標的方法,獲取在當前視窗內的絕對座標。
int[] location = new int[2] ;
view.getLocationInWindow(location);
解釋:
location[0] —–> x座標
location[1] —–> y座標

對比

android中獲取view座標的方法有兩種:

  • getLocationInWindow
    • 獲取在當前視窗內的絕對座標
  • getLocationOnScreen
    • 獲取在整個螢幕內的絕對座標
    • 從螢幕頂端算起,包括了通知欄的高度

你真的理解 getLocationInWindow 了嗎?

踩過的坑

你真的理解 getLocationInWindow 了嗎?

在onCreate裡面呼叫,會得到location[0]和location[1]的值均為空,這是因為UI控制元件還沒載入好的原因。所以我們可以使用view.post(runnable)方法去獲取或者在onWindowFocusChanged(boolean hasFocus)方法中獲取。

使用案例

你真的理解 getLocationInWindow 了嗎?

案例效果圖如下:

你真的理解 getLocationInWindow 了嗎?

核心程式碼分析

首先我們需要儲存頂部Tab滑動各item寬度座標,程式碼如下:

    /**
     * 儲存症狀詳情頂部tab橫向寬度座標
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    private void saveSymptomDetailHorizontalWidth() {
        int[] location = new int[2];
        for (int i = 0; i < symptomDetailContainerLy.getChildCount(); i++) {
            getSingleNavigation(i).getLocationInWindow(location);
            symptomDetailHorizontalWidth[i] = location[0];
        }
    }複製程式碼

然後儲存症狀詳情滑動高度座標,程式碼如下:

/**
     * 儲存症狀詳情滑動高度座標
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    private void savesymtomDetailScvHeight() {
            // 症狀詳情
            final int[] symptomModuleLoc = new int[2];
            final int[] symptomLocation = new int[2];
            // 症狀模組
            symptomModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    symptomModuleLy.getLocationInWindow(symptomModuleLoc);
                    symtomDetailScvHeight[0] = 0;
                }
            });
            // 病因模組
            pathogenyModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    pathogenyModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[1] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            // 檢查模組
            checkoutModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    checkoutModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[2] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            // 診斷模組
            diagnoseModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    diagnoseModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[3] = symptomLocation[1] -symptomModuleLoc[1];
                }
            });
            // 預防模組
            preventModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    preventModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[4] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            foodTreatModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    // 食療模組
                    foodTreatModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[5] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
    }複製程式碼

接著設定滑動監聽、點選頂部Tab事件,程式碼如下:

    /**
     * 症狀詳情頂部tab橫向監聽
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    class OnSymtomHorizontalScClickedListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            isTopClick = true;
            setSymtomHorizontalCurrentPostion(view, true, 0, 0);
        }
    }

  @Override
    protected void registerListener() {
        // 退出當前頁面
        findViewById(R.id.iv_actionbar_back).setOnClickListener(this);
        // 設定滑動監聽
        if (symptomDetailScv != null)
            symptomDetailScv.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
                @Override
                public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                                            int oldx, int oldy, int oritention) {
                        if (isTopClick) {
                            isTopClick = false;
                            return;
                         }
                        // 症狀詳情
                        // 如果症狀詳情頂部tab橫向寬度座標或者症狀詳情滑動高度座標則返回
                        if (symtomDetailScvHeight == null || symptomDetailHorizontalWidth == null)
                            return;
                        if (y >= symtomDetailScvHeight[0] && y < symtomDetailScvHeight[1]) {
                            // 症狀
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(0),
                                    false, symptomDetailHorizontalWidth[0], y);
                        } else if (y >= symtomDetailScvHeight[1] && y < symtomDetailScvHeight[2]) {
                            // 病因
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(1),
                                    false, symptomDetailHorizontalWidth[1], y);
                        } else if (y >= symtomDetailScvHeight[2] && y < symtomDetailScvHeight[3]) {
                            // 檢查
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(2),
                                    false, symptomDetailHorizontalWidth[2], y);
                        } else if (y >= symtomDetailScvHeight[3] && y < symtomDetailScvHeight[4]) {
                            // 診斷
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(3),
                                    false, symptomDetailHorizontalWidth[3], y);
                        } else if (y >= symtomDetailScvHeight[4] && y < symtomDetailScvHeight[5]) {
                            // 預防
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(4),
                                    false, symptomDetailHorizontalWidth[4], y);
                        } else if (y >= symtomDetailScvHeight[5]) {
                            // 食療
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(5),
                                    false, symptomDetailHorizontalWidth[5], y);
                        }
                }
            });
    }複製程式碼

最後設定頂部Tab位置,程式碼如下:

   /**
     * 設定症狀詳情頂部tab位置
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param view
     * @param fromClick 是否點選
     * @param x x座標
     * @param y y座標
     * @return
     */
    public  void setSymtomHorizontalCurrentPostion(View view, boolean fromClick, int x, int y){
        // 如果不是點選事件並橫向滑動控制元件不為空則滑動到指定座標
        if (!fromClick && symptomDetailHscv != null) {
            symptomDetailHscv.scrollTo(x, y);
        }
        if (view == null){
            return;
        }
        if (view.getTag() == null)
            return;
        // 獲取當前位置
        int position = (Integer) view.getTag();
        // 如果當前位置非上次位置
        if (lastPosition != position){
            // 如果頂部tab動態載入容器為空,則重新例項化
            if (symptomDetailContainerLy == null) {
                // 頂部tab動態載入容器
                symptomDetailContainerLy = (LinearLayout) findViewById(R.id.ly_symptom_detail_container);
            }
            // 設定上次位置藍色下滑線不可見
            getNavigationImageView(lastPosition).setVisibility(View.INVISIBLE);
            // 設定上次位置字型顏色為黑色
            getNavigationTextView(lastPosition).setTextColor(
                    getResources().getColor(R.color.text_color_black));
            // 設定當前位置藍色下劃線可見
            getNavigationImageView(position).setVisibility(View.VISIBLE);
            // 設定當前位置字型顏色為藍色
            getNavigationTextView(position).setTextColor(
                    getResources().getColor(R.color.title_color));
        }
        lastPosition = position;
        if (symptomDetailScv != null && fromClick) {
            symptomDetailScv.scrollTo(0, symtomDetailScvHeight[position]);
        }
    }複製程式碼

症狀詳情頁面完整程式碼如下:

package cn.jianke.getlocationinwindowdemo.module.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.jianke.getlocationinwindowdemo.R;
import cn.jianke.getlocationinwindowdemo.httprequest.ApiCallback;
import cn.jianke.getlocationinwindowdemo.httprequest.api.ApiSymptomDetail;
import cn.jianke.getlocationinwindowdemo.httprequest.httpresponse.SymptomDetailResponse;
import cn.jianke.getlocationinwindowdemo.module.util.HTMLSpirit;
import cn.jianke.getlocationinwindowdemo.module.util.StringUtil;
import cn.jianke.getlocationinwindowdemo.module.util.Unicode2String;
import cn.jianke.getlocationinwindowdemo.module.widget.ObservableScrollView;
import static cn.jianke.getlocationinwindowdemo.module.activity.MainActivity.SYMPTOM_ID;

/**
 * @className: SymptomDetailActivity
 * @classDescription: 症狀詳情
 * @author: leibing
 * @createTime: 2016/10/29
 */
public class SymptomDetailActivity extends BaseActivity implements View.OnClickListener{
    // 症狀id
    private String id = "";
    // 頂部橫向滑動控制元件
    private HorizontalScrollView symptomDetailHscv;
    // 頂部tab動態載入容器
    private LinearLayout symptomDetailContainerLy;
    // 滑動控制元件
    private ObservableScrollView symptomDetailScv;
    // 症狀模組
    private LinearLayout symptomModuleLy;
    // 症狀標題
    private TextView symptomTitleTv;
    // 症狀內容
    private TextView symptomContentTv;
    // 病因模組
    private LinearLayout pathogenyModuleLy;
    // 病因內容
    private TextView pathogenyContentTv;
    // 檢查模組
    private LinearLayout checkoutModuleLy;
    // 檢查內容
    private TextView checkoutContentTv;
    // 診斷模組
    private LinearLayout diagnoseModuleLy;
    // 診斷內容
    private TextView diagnoseContentTv;
    // 預防模組
    private LinearLayout preventModuleLy;
    // 預防內容
    private TextView preventContentTv;
    // 食療模組
    private LinearLayout foodTreatModuleLy;
    // 食療內容
    private TextView foodTreatContentTv;
    // 症狀詳情資料
    private SymptomDetailResponse symptomDetailResponse;
    // 症狀詳情頂部tab橫向寬度座標
    private final int[] symptomDetailHorizontalWidth = new int[6];
    // 症狀詳情滑動高度座標
    private final int[] symtomDetailScvHeight = new int[6];
    // 是否頂部點選事件
    private boolean isTopClick = false;
    // 上次點選點頂部tab item的位置
    private int lastPosition = 0;
    // 症狀詳情api
    private ApiSymptomDetail mApiSymptomDetail;

    @Override
    protected void setContentView() {
        // 指定佈局
        setContentView(R.layout.activity_symptom_detail);
    }

    @Override
    protected void initView() {
        // 滑動控制元件
        symptomDetailScv = (ObservableScrollView) findViewById(R.id.scv_symptom_detail);
        // 頂部橫向滑動控制元件
        symptomDetailHscv = (HorizontalScrollView) findViewById(R.id.hscv_symptom_detail);
        // 頂部tab動態載入容器
        symptomDetailContainerLy = (LinearLayout) findViewById(R.id.ly_symptom_detail_container);
        // 症狀模組
        symptomModuleLy = (LinearLayout) findViewById(R.id.ly_symptom_module);
        // 症狀標題
        symptomTitleTv = (TextView) findViewById(R.id.tv_symptom_title);
        // 症狀內容
        symptomContentTv = (TextView) findViewById(R.id.tv_symptom_content);
        // 病因模組
        pathogenyModuleLy = (LinearLayout) findViewById(R.id.ly_pathogeny_module);
        // 病因內容
        pathogenyContentTv = (TextView) findViewById(R.id.tv_pathogeny_content);
        // 檢查模組
        checkoutModuleLy = (LinearLayout) findViewById(R.id.ly_checkout_module);
        // 檢查內容
        checkoutContentTv = (TextView) findViewById(R.id.tv_checkout_content);
        // 診斷模組
        diagnoseModuleLy = (LinearLayout) findViewById(R.id.ly_diagnose_module);
        // 診斷內容
        diagnoseContentTv = (TextView) findViewById(R.id.tv_diagnose_content);
        // 預防模組
        preventModuleLy = (LinearLayout) findViewById(R.id.ly_prevent_module);
        // 預防內容
        preventContentTv = (TextView) findViewById(R.id.tv_prevent_content);
        // 食療模組
        foodTreatModuleLy = (LinearLayout) findViewById(R.id.ly_food_treat_module);
        // 食療內容
        foodTreatContentTv = (TextView) findViewById(R.id.tv_food_treat_content);

        // 獲取意圖傳值
        getIntentData();
        // 初始化症狀詳情api
        mApiSymptomDetail = new ApiSymptomDetail();
        // 初始化症狀詳情頂部tab
        initSymptomDetailHorizontalTab();
    }

    @Override
    protected void registerListener() {
        // 退出當前頁面
        findViewById(R.id.iv_actionbar_back).setOnClickListener(this);
        // 設定滑動監聽
        if (symptomDetailScv != null)
            symptomDetailScv.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
                @Override
                public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
                                            int oldx, int oldy, int oritention) {
                        if (isTopClick) {
                            isTopClick = false;
                            return;
                         }
                        // 症狀詳情
                        // 如果症狀詳情頂部tab橫向寬度座標或者症狀詳情滑動高度座標則返回
                        if (symtomDetailScvHeight == null || symptomDetailHorizontalWidth == null)
                            return;
                        if (y >= symtomDetailScvHeight[0] && y < symtomDetailScvHeight[1]) {
                            // 症狀
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(0),
                                    false, symptomDetailHorizontalWidth[0], y);
                        } else if (y >= symtomDetailScvHeight[1] && y < symtomDetailScvHeight[2]) {
                            // 病因
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(1),
                                    false, symptomDetailHorizontalWidth[1], y);
                        } else if (y >= symtomDetailScvHeight[2] && y < symtomDetailScvHeight[3]) {
                            // 檢查
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(2),
                                    false, symptomDetailHorizontalWidth[2], y);
                        } else if (y >= symtomDetailScvHeight[3] && y < symtomDetailScvHeight[4]) {
                            // 診斷
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(3),
                                    false, symptomDetailHorizontalWidth[3], y);
                        } else if (y >= symtomDetailScvHeight[4] && y < symtomDetailScvHeight[5]) {
                            // 預防
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(4),
                                    false, symptomDetailHorizontalWidth[4], y);
                        } else if (y >= symtomDetailScvHeight[5]) {
                            // 食療
                            setSymtomHorizontalCurrentPostion(symptomDetailContainerLy.getChildAt(5),
                                    false, symptomDetailHorizontalWidth[5], y);
                        }
                }
            });
    }

    @Override
    protected void getData() {
        if (mApiSymptomDetail != null){
            // 請求資料
            mApiSymptomDetail.getSymptomDetail(id, SymptomDetailActivity.this,
                    new ApiCallback<SymptomDetailResponse>() {
                @Override
                public void onSuccess(SymptomDetailResponse response) {
                    // 更新UI
                    updateSymptomUI(response);
                }

                @Override
                public void onError(String err_msg) {
                    Toast.makeText(SymptomDetailActivity.this,
                            err_msg, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure() {
                    Toast.makeText(SymptomDetailActivity.this,
                            "網路不給力", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * 更新症狀詳情UI
     * @author leibing
     * @createTime 2016/10/08
     * @lastModify 2016/10/08
     * @param response 症狀詳情資料
     * @return
     */
    private void updateSymptomUI(SymptomDetailResponse response) {
        // 更新症狀詳情資料
        symptomDetailResponse = response;
        // 症狀
        if (StringUtil.isNotEmpty(symptomDetailResponse.namecn)
                && StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.summarize)))){
            // 症狀標題
            symptomTitleTv.setText(symptomDetailResponse.namecn);
            // 症狀內容
            symptomContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.summarize)));
        }
        if (StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.pathogeny)))){
            // 病因內容
            pathogenyContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.pathogeny)));
        }
        if (StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.diagnoses)))){
            // 檢查內容
            checkoutContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.diagnoses)));
        }
        if (StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.differential)))){
            // 診斷內容
            diagnoseContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.differential)));
        }
        if (StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.prevent)))){
            // 預防內容
            preventContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.prevent)));
        }
        if (StringUtil.isNotEmpty(HTMLSpirit.removeHtmlTag(
                Unicode2String.decodeUnicode(symptomDetailResponse.foodtreat)))){
            // 食療內容
            foodTreatContentTv.setText(HTMLSpirit.removeHtmlTag(
                    Unicode2String.decodeUnicode(symptomDetailResponse.foodtreat)));
        }

        // 設定高度和寬度
        savesymtomDetailScvHeight();
        saveSymptomDetailHorizontalWidth();
    }

    /**
     * 獲取意圖傳值
     * @author leibing
     * @createTime 2016/10/29
     * @lastModify 2016/10/29
     * @param
     * @return
     */
    private void getIntentData() {
        // 獲取意圖傳值
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            // 症狀id
            id = bundle.getString(SYMPTOM_ID, "");
        }
    }

    /**
     * 初始化症狀詳情頂部tab
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    private void initSymptomDetailHorizontalTab() {
        // 獲取頂部滑動item名稱
        String[] array = getResources().getStringArray(
                R.array.symptom_detail_navigation_name);
        // 初始化監聽
        OnSymtomHorizontalScClickedListener listener = new OnSymtomHorizontalScClickedListener();
        for (int i = 0; i < array.length; i++) {
            // 獲取子view
            View childView = LayoutInflater.from(SymptomDetailActivity.this).inflate(
                    R.layout.check_details_single_navigation_model, null);
            // 例項化子view控制元件
            TextView titleTv = (TextView)
                    childView.findViewById(R.id.check_details_navigation_textView);
            // 給子view控制元件初始化值
            titleTv.setText(array[i]);
            // 子view設定tag
            childView.setTag(i);
            // 子view設定監聽
            childView.setOnClickListener(listener);
            // 子view新增到父容器
            symptomDetailContainerLy.addView(childView);
        }
        // 預設為選中第一個
        getNavigationImageView(0).setVisibility(View.VISIBLE);
        getNavigationTextView(0).setTextColor(
                getResources().getColor(R.color.title_color));
    }

    /**
     * 設定症狀詳情頂部tab位置
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param view
     * @param fromClick 是否點選
     * @param x x座標
     * @param y y座標
     * @return
     */
    public  void setSymtomHorizontalCurrentPostion(View view, boolean fromClick, int x, int y){
        // 如果不是點選事件並橫向滑動控制元件不為空則滑動到指定座標
        if (!fromClick && symptomDetailHscv != null) {
            symptomDetailHscv.scrollTo(x, y);
        }
        if (view == null){
            return;
        }
        if (view.getTag() == null)
            return;
        // 獲取當前位置
        int position = (Integer) view.getTag();
        // 如果當前位置非上次位置
        if (lastPosition != position){
            // 如果頂部tab動態載入容器為空,則重新例項化
            if (symptomDetailContainerLy == null) {
                // 頂部tab動態載入容器
                symptomDetailContainerLy = (LinearLayout) findViewById(R.id.ly_symptom_detail_container);
            }
            // 設定上次位置藍色下滑線不可見
            getNavigationImageView(lastPosition).setVisibility(View.INVISIBLE);
            // 設定上次位置字型顏色為黑色
            getNavigationTextView(lastPosition).setTextColor(
                    getResources().getColor(R.color.text_color_black));
            // 設定當前位置藍色下劃線可見
            getNavigationImageView(position).setVisibility(View.VISIBLE);
            // 設定當前位置字型顏色為藍色
            getNavigationTextView(position).setTextColor(
                    getResources().getColor(R.color.title_color));
        }
        lastPosition = position;
        if (symptomDetailScv != null && fromClick) {
            symptomDetailScv.scrollTo(0, symtomDetailScvHeight[position]);
        }
    }

    /**
     * 儲存症狀詳情頂部tab橫向寬度座標
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    private void saveSymptomDetailHorizontalWidth() {
        int[] location = new int[2];
        for (int i = 0; i < symptomDetailContainerLy.getChildCount(); i++) {
            getSingleNavigation(i).getLocationInWindow(location);
            symptomDetailHorizontalWidth[i] = location[0];
        }
    }

    /**
     * 儲存症狀詳情滑動高度座標
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    private void savesymtomDetailScvHeight() {
            // 症狀詳情
            final int[] symptomModuleLoc = new int[2];
            final int[] symptomLocation = new int[2];
            // 症狀模組
            symptomModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    symptomModuleLy.getLocationInWindow(symptomModuleLoc);
                    symtomDetailScvHeight[0] = 0;
                }
            });
            // 病因模組
            pathogenyModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    pathogenyModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[1] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            // 檢查模組
            checkoutModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    checkoutModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[2] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            // 診斷模組
            diagnoseModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    diagnoseModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[3] = symptomLocation[1] -symptomModuleLoc[1];
                }
            });
            // 預防模組
            preventModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    preventModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[4] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
            foodTreatModuleLy.post(new Runnable() {
                @Override
                public void run() {
                    // 食療模組
                    foodTreatModuleLy.getLocationInWindow(symptomLocation);
                    symtomDetailScvHeight[5] = symptomLocation[1] - symptomModuleLoc[1];
                }
            });
    }

    /**
     * 症狀詳情頂部tab橫向監聽
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param
     * @return
     */
    class OnSymtomHorizontalScClickedListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            isTopClick = true;
            setSymtomHorizontalCurrentPostion(view, true, 0, 0);
        }
    }

    /**
     * 根據索引獲取頂部滑動欄TextView例項
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param index 索引值
     * @return
     */
    private TextView getNavigationTextView(int index) {
        if (symptomDetailContainerLy == null)
            return null;
        return (TextView) symptomDetailContainerLy.getChildAt(index).findViewById(
                R.id.check_details_navigation_textView);
    }

    /**
     * 根據索引獲取頂部滑動欄ImageView例項
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param index 索引值
     * @return
     */
    private ImageView getNavigationImageView(int index) {
        if (symptomDetailContainerLy == null)
            return null;
        return (ImageView) symptomDetailContainerLy.getChildAt(index).findViewById(
                R.id.check_details_navigation_imageView);
    }

    /**
     * 根據索引獲取頂部滑動欄View例項
     * @author leibing
     * @createTime 2016/10/19
     * @lastModify 2016/10/19
     * @param index 索引值
     * @return
     */
    private View getSingleNavigation(int index) {
        if (symptomDetailContainerLy == null)
            return null;
        return symptomDetailContainerLy.getChildAt(index);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_actionbar_back:
                // 退出當前頁面
                this.finish();
                break;
            default:
                break;
        }
    }
}複製程式碼

專案地址:LocationInWindow

關於作者

相關文章