亲宝软件园·资讯

展开

Android选项卡切换效果 Android自定义选项卡切换效果

偶是老李头 人气:0
想了解Android自定义选项卡切换效果的相关内容吗,偶是老李头在本文为您仔细讲解Android选项卡切换效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,选项卡,下面大家一起来学习吧。

一、实际使用的效果

二、自定义可切换的标题栏

1、布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="232dp"
    android:layout_height="32dp"
    android:background="@drawable/leave_back_tab_bg_selector"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tvLeaveNum"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/leave_back_button_bg_selector"
        android:gravity="center"
        android:layout_weight="1"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/leave_crews_num"/>

    <TextView
        android:id="@+id/tvBackNum"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/leave_back_button_bg_selector"
        android:gravity="center"
        android:layout_weight="1"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/back_crews_num"/>
</LinearLayout>

leave_back_button_bg_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#328BDD" />
            <corners android:radius="3dp" />
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

leave_back_button_bg_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#328BDD" />
            <corners android:radius="3dp" />
            <solid android:color="#328BDD" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/transparent" />
            <corners android:radius="3dp" />
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

2、控件封装

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnFocusChange;

public class LeaveBackTitleTabView extends LinearLayout {

    public final static int INDEX_LEAVE = 1;
    public final static int INDEX_BACK = 2;

    @BindView(R.id.tvBackNum)
    TextView tvBackNum;
    @BindView(R.id.tvLeaveNum)
    TextView tvLeaveNum;
    private Context mContext;

    private ITabChangeListener tabChangeListener;

    public void setTabChangeListener(ITabChangeListener tabChangeListener) {
        this.tabChangeListener = tabChangeListener;
    }

    public LeaveBackTitleTabView(Context context) {
        super(context);
        mContext = context;
    }

    public LeaveBackTitleTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = (View) LayoutInflater.from(context).inflate(R.layout.view_leave_back_list_tab, this, true);
        ButterKnife.bind(view);
    }

    @OnFocusChange({R.id.tvLeaveNum,R.id.tvBackNum})
    public void doFocusChanged(View view){
        switch(view.getId()){
            case R.id.tvLeaveNum :
                if(tabChangeListener != null){
                    tabChangeListener.onTabChanged(INDEX_LEAVE);
                }
                break;
            case R.id.tvBackNum:
                if(tabChangeListener != null){
                    tabChangeListener.onTabChanged(INDEX_BACK);
                }
                break;
        }
    }

    public void setCrewsNum(int leaveNum,int backNum){
        tvLeaveNum.setText(String.format(getResources().getString(R.string.leave_crews_num), String.valueOf(leaveNum)));
        tvBackNum.setText(String.format(getResources().getString(R.string.back_crews_num), String.valueOf(backNum)));

        if(leaveNum > 0 && backNum > 0){
            tvLeaveNum.requestFocus();
        }else if(leaveNum > 0 && backNum == 0){
            tvLeaveNum.setClickable(true);
            tvLeaveNum.setFocusable(true);
            tvBackNum.setClickable(false);
            tvBackNum.setFocusable(false);
            tvLeaveNum.requestFocus();
        }else if(leaveNum == 0 && backNum > 0){
            tvLeaveNum.setClickable(false);
            tvLeaveNum.setFocusable(false);
            tvBackNum.setClickable(true);
            tvBackNum.setFocusable(true);
            tvBackNum.requestFocus();
        }else{
            tvLeaveNum.setClickable(false);
            tvLeaveNum.setFocusable(false);
            tvBackNum.setClickable(false);
            tvBackNum.setFocusable(false);
        }
    }

    /**
     * TAB切换时的listener
     */
    public interface ITabChangeListener{
        public void onTabChanged(int index);
    }
}

3、使用方法

<com.hisign.ship_terminal_hs518.view.LeaveBackTitleTabView
      android:layout_width="232dp"
      android:layout_height="32dp"
      android:layout_marginTop="10dp"
      android:visibility="gone"
      android:id="@+id/lttTitle">
</com.hisign.ship_terminal_hs518.view.LeaveBackTitleTabView>

4、注册回调事件(一般在UI界面上进行注册)

 /**
     * 离船和在船船员信息列表
 */
    private LeaveBackTitleTabView.ITabChangeListener iTabChangeListener = new LeaveBackTitleTabView.ITabChangeListener() {
        @Override
        public void onTabChanged(int index) {
            switch (index) {
                case LeaveBackTitleTabView.INDEX_LEAVE:
                    // 界面上点击了离船
                    ll_leave_crews.setVisibility(View.VISIBLE);
                    ll_back_crews.setVisibility(View.GONE);
                    break;
                case LeaveBackTitleTabView.INDEX_BACK:
                    // 界面上点击了在船
                    ll_back_crews.setVisibility(View.VISIBLE);
                    ll_leave_crews.setVisibility(View.GONE);
                    break;
            }
        }
    };

5、注意事项:

(1)、控件需要能响应点击事件,同时切换到某一选项时,该选项卡需要显示选中的状态,所以在控件中需要指定:

android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"

但是这样设置了之后,控件就在点击时就不能在点击的第一下响应onClick点击事件,我的做法是响应onFouceChange事件

(2)、为啥这样设置,在点击的第一下就不响应onClick了呢?源码中显示w 在 onTouchEvent() 中的 MotionEvent.ACTION_UP 中对focus做了处理, 如果View focusableInTouchMode 是true, 并且当前没有获得焦点, 那么会尝试获取焦点, 并且不会调用 performClick()。

public boolean onTouchEvent(MotionEvent event) {
  ...
 if (((viewFlags & CLICKABLE) == CLICKABLE ||
         (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
         (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
     switch (action) {
         case MotionEvent.ACTION_UP:
             boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
             if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
                 boolean focusTaken = false;
                 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
                     focusTaken = requestFocus();
                 }

                 if (prepressed) {
                     setPressed(true, x, y);
                }

                 if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
                     removeLongPressCallback();
 
                     if (!focusTaken) {
                         if (mPerformClick == null) {
                             mPerformClick = new PerformClick();
                         }
                         if (!post(mPerformClick)) {
                             performClick();
                         }
                     }
                 }
 
 ...
}

加载全部内容

相关教程
猜你喜欢
用户评论