RadioGroup--RadioButton结合Viewpager实现页面滑动

 

先上图:

 

首先是Activity布局

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="@dimen/dp_16"
        android:background="@drawable/shape_director_total_earning_bg"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_total_money_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="累计收益"
            android:textColor="@color/white"
            android:textSize="@dimen/dimen_12sp" />

        <TextView
            android:id="@+id/tv_total_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_total_money_desc"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_5"
            android:text="0元"
            android:textColor="@color/white"
            android:textSize="@dimen/dimen_24sp" />

    </RelativeLayout>

    <View style="@style/Line.Horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="@dimen/dp_10"
        android:text="收益明细"
        android:textColor="@color/tc_gray_3"
        android:textSize="@dimen/sp_18" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="@dimen/titleHeight"
        android:background="@color/app_bg_gray"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_one"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_director_time_radio"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="近一月"
            android:textColor="@drawable/shape_director_time_text_radio" />

        <!--<View-->
            <!--android:layout_width="@dimen/dp_0.5"-->
            <!--android:layout_height="@dimen/dimen_30dp"-->
            <!--android:layout_gravity="center_vertical"-->
            <!--android:background="@color/dividerColor" />-->

        <RadioButton
            android:id="@+id/radio_three"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_director_time_radio"
            android:button="@null"
            android:gravity="center"
            android:text="近三月"
            android:textColor="@drawable/shape_director_time_text_radio" />

        <!--<View-->
            <!--android:layout_width="@dimen/dp_0.5"-->
            <!--android:layout_height="@dimen/dimen_30dp"-->
            <!--android:layout_gravity="center_vertical"-->
            <!--android:background="@color/dividerColor" />-->

        <RadioButton
            android:id="@+id/radio_custom"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_director_time_radio"
            android:button="@null"
            android:gravity="center"
            android:text="自定义"
            android:textColor="@drawable/shape_director_time_text_radio" />
    </RadioGroup>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
 

然后是Activity:

package com.dyh.drivingschool.ui.director;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dyh.drivingschool.R;
import com.dyh.drivingschool.ui.director.fragment.ProfitCustomFragment;
import com.dyh.drivingschool.ui.director.fragment.ProfitFixedFragment;
import com.dyh.library.base.TitleFragmentActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dyh.drivingschool.R.id.radio_one;

/**
 * 累计收益
 */

public class ProfitTotalActivity extends TitleFragmentActivity {

    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.radio_one)
    RadioButton mRadio_one;

    @BindView(R.id.radio_three)
    RadioButton mRadio_three;

    @BindView(R.id.radio_custom)
    RadioButton mRadio_custom;


    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addLayout(R.layout.activity_profit_total);
        ButterKnife.bind(this);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case radio_one:
                        viewpager.setCurrentItem(0);

                        break;
                    case R.id.radio_three:
                        viewpager.setCurrentItem(1);

                        break;
                    case R.id.radio_custom:
                        viewpager.setCurrentItem(2);

                        break;
                }
            }
        });

        fragmentList.add(ProfitFixedFragment.newInstance("1"));
        fragmentList.add(ProfitFixedFragment.newInstance("2"));
        fragmentList.add(new ProfitCustomFragment());
        viewpager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        viewpager.setOffscreenPageLimit(2);
        viewpager.setCurrentItem(0);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0){
                    mRadio_one.setChecked(true);
                }else if (position == 1){
                    mRadio_three.setChecked(true);
                }else if (position == 2){
                    mRadio_custom.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        /**
         * 为 RadioGroup 设置选中变化事件监听,当 RadioButton 状态变化,我们同步 Viewpager 的选中页面
         **/
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == mRadio_one.getId()) viewpager.setCurrentItem(0);
                else if (checkedId == mRadio_three.getId()) viewpager.setCurrentItem(1);
                else if (checkedId == mRadio_custom.getId()) viewpager.setCurrentItem(2);

            }
        });

        //设置默认选中页

    }


    @Override
    protected void initTitileBar() {
        titleLayout.showCenterLeftIconView("累计收益");
    }


}

 

然后是其中的fragment:

package com.dyh.drivingschool.ui.director.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.dyh.drivingschool.R;
import com.dyh.drivingschool.ui.director.adapter.ProfitMonthAdapter;
import com.dyh.drivingschool.uinew.earnmoney.bean.ArticleChildBean;
import com.dyh.frame.BaseFragment;
import com.dyh.frame.widget.PinnedHeaderExpandableListView;
import com.dyh.library.base.PageHelperNew;
import com.dyh.library.widget.NoDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
* 收益列表
*/

public class ProfitFixedFragment extends BaseFragment {

    private static final String FIXED_TIME = "fixed_time";

    @BindView(R.id.expandable_listview)
    PinnedHeaderExpandableListView expandableListview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.no_date_layout)
    NoDataView noDateLayout;

    private Unbinder unbinder;

    private String key;

    private PageHelperNew pageHelper;
    private ProfitMonthAdapter adapter;
    private List<Integer> groupList = new ArrayList<>();
    private List<List<ArticleChildBean.ArticleChildJson>> childList = new ArrayList<>();

    public static ProfitFixedFragment newInstance(String key) {
        ProfitFixedFragment fragment = new ProfitFixedFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FIXED_TIME, key);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rootView = inflater.inflate(R.layout.fragment_profit_fixed, container, false);
        unbinder = ButterKnife.bind(this, rootView);

        pageHelper = pageHelper.getNewInstance();
        pageHelper.setPageSize(10);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout layout) {
                if (pageHelper.pullUp(refreshLayout)) {

                }
            }
        });

        expandableListview.setHeaderView(mContext.getLayoutInflater().inflate(R.layout.item_earning_detail_group, null));
        adapter = new ProfitMonthAdapter(mContext, null, null, expandableListview);
        expandableListview.setAdapter(adapter);
        expandableListview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                return false;
            }
        });
        expandableListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        if (getArguments() != null) {
            key = getArguments().getString(FIXED_TIME);
        }
        reqData();

        return rootView;
    }

    private void reqData() {
        for (int i = 0; i < 3; i++) {
            groupList.add(i);
            List<ArticleChildBean.ArticleChildJson> list = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                ArticleChildBean.ArticleChildJson json = new ArticleChildBean.ArticleChildJson();
                list.add(json);
            }
            childList.add(list);
        }
        adapter.setGroupList(groupList);
        adapter.setChildList(childList);
        adapter.notifyDataSetChanged();

        for (int i = 0; i < adapter.getGroupList().size(); i++) {
            expandableListview.expandGroup(i);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}



fragment布局:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <com.dyh.frame.widget.PinnedHeaderExpandableListView
            android:id="@+id/expandable_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:childDivider="@color/dividerColor"
            android:divider="@color/dividerColor"
            android:dividerHeight="@dimen/dp_0.5" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

    <com.dyh.library.widget.NoDataView
        android:id="@+id/no_date_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
</RelativeLayout>

 

这是fragment适配器:

package com.dyh.drivingschool.ui.director.adapter;

import android.content.Context;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dyh.common.utils.DisplayOption;
import com.dyh.drivingschool.R;
import com.dyh.drivingschool.uinew.earnmoney.bean.ArticleChildBean;
import com.dyh.drivingschool.uinew.earnmoney.bean.ArticleGroupBean;
import com.dyh.frame.util.ListUtil;
import com.dyh.frame.widget.PinnedHeaderExpandableListView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 月份列表适配器
 */

public class ProfitMonthAdapter extends BaseExpandableListAdapter implements PinnedHeaderExpandableListView.HeaderAdapter {


    private PinnedHeaderExpandableListView listView;
    private LayoutInflater mInflater;
    private List<ArticleGroupBean.ArticleGroupJson> configList;
    private List<Integer> groupList;
    private List<List<ArticleChildBean.ArticleChildJson>> childList;


    public ProfitMonthAdapter(Context mContext, List<Integer> groupList, List<List<ArticleChildBean.ArticleChildJson>> childList, PinnedHeaderExpandableListView listView) {
        this.listView = listView;
        this.groupList = groupList;
        this.childList = childList;
        this.mInflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getGroupCount() {
        return ListUtil.isNullOrEmpty(groupList) ? 0 : groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return ListUtil.isNullOrEmpty(childList) ? 0 : childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_earning_detail_group, parent, false);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ArticleChildBean.ArticleChildJson dataItem = (ArticleChildBean.ArticleChildJson) getChild(groupPosition, childPosition);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_earning_detail_child, parent, false);
        }


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1 && !listView.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition, int childPosition, int alpha) {
        Integer id = (Integer) getGroup(groupPosition);
        if (!ListUtil.isNullOrEmpty(configList)) {
            for (int i = 0; i < configList.size(); i++) {
                ArticleGroupBean.ArticleGroupJson dataItem = configList.get(i);
                if (dataItem.getCfgId() == id) {
                    View view = header.findViewById(R.id.v_shadow);
                    ImageView complete = header.findViewById(R.id.iv_complete);
                    ImageView icon = header.findViewById(R.id.iv_config_icon);
                    TextView money = header.findViewById(R.id.tv_config_money);
                    TextView desc = header.findViewById(R.id.tv_config_desc);
                    ImageLoader.getInstance().displayImage(dataItem.getIconUrl(), icon, DisplayOption.getRoundedOptions());
                    money.setText(dataItem.getShareNum() + "条/" + dataItem.getAllNum() + "条");
                    desc.setText(dataItem.getDescp());
                }
            }
        }
    }

    private SparseIntArray groupStatusMap = new SparseIntArray();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if (groupStatusMap.keyAt(groupPosition) >= 0) {
            return groupStatusMap.get(groupPosition);
        } else {
            return 0;
        }
    }

    public List<Integer> getGroupList() {
        return groupList;
    }

    public void setGroupList(List<Integer> groupList) {
        this.groupList = groupList;
    }

    public List<List<ArticleChildBean.ArticleChildJson>> getChildList() {
        return childList;
    }

    public void setChildList(List<List<ArticleChildBean.ArticleChildJson>> childList) {
        this.childList = childList;
    }

    public void setConfigList(List<ArticleGroupBean.ArticleGroupJson> configList) {
        this.configList = configList;
    }
}

 

这是fragment其中的头布局适配器:

 

package com.dyh.frame.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;

public class PinnedHeaderExpandableListView extends ExpandableListView implements OnScrollListener, OnGroupClickListener {
   public PinnedHeaderExpandableListView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      registerListener();
   }

   public PinnedHeaderExpandableListView(Context context, AttributeSet attrs) {
      super(context, attrs);
      registerListener();
   }

   public PinnedHeaderExpandableListView(Context context) {
      super(context);
      registerListener();
   }

   /**
    * Adapter 接口 . 列表必须实现此接口 .
    */
   public interface HeaderAdapter {
      int PINNED_HEADER_GONE = 0;
      int PINNED_HEADER_VISIBLE = 1;
      int PINNED_HEADER_PUSHED_UP = 2;

      /**
       * 获取 Header 的状态
       * 
       * @param groupPosition
       * @param childPosition
       * @return 
       *         PINNED_HEADER_GONE,PINNED_HEADER_VISIBLE,PINNED_HEADER_PUSHED_UP
       *         其中之一
       */
      int getHeaderState(int groupPosition, int childPosition);

      /**
       * 配置 Header, 让 Header 知道显示的内容
       * 
       * @param header
       * @param groupPosition
       * @param childPosition
       * @param alpha
       */
      void configureHeader(View header, int groupPosition, int childPosition, int alpha);

      /**
       * 设置组按下的状态
       * 
       * @param groupPosition
       * @param status
       */
      void setGroupClickStatus(int groupPosition, int status);

      /**
       * 获取组按下的状态
       * 
       * @param groupPosition
       * @return
       */
      int getGroupClickStatus(int groupPosition);

   }

   private static final int MAX_ALPHA = 255;

   private HeaderAdapter mAdapter;

   /**
    * 用于在列表头显示的 View,mHeaderViewVisible 为 true 才可见
    */
   private View mHeaderView;

   /**
    * 列表头是否可见
    */
   private boolean mHeaderViewVisible;

   private int mHeaderViewWidth;

   private int mHeaderViewHeight;

   public void setHeaderView(View view) {
      mHeaderView = view;
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
      view.setLayoutParams(lp);

      if (mHeaderView != null) {
         setFadingEdgeLength(0);
      }

      requestLayout();
   }

   private void registerListener() {
      setOnScrollListener(this);
      setOnGroupClickListener(this);
   }

   /**
    * 点击 HeaderView 触发的事件
    */
   private void headerViewClick() {
      long packedPosition = getExpandableListPosition(this.getFirstVisiblePosition());

      int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);

      if (mAdapter.getGroupClickStatus(groupPosition) == 1) {
         this.collapseGroup(groupPosition);
         mAdapter.setGroupClickStatus(groupPosition, 0);
      } else {
         this.expandGroup(groupPosition);
         mAdapter.setGroupClickStatus(groupPosition, 1);
      }

      this.setSelectedGroup(groupPosition);
   }

   private float mDownX;
   private float mDownY;

   /**
    * 如果 HeaderView 是可见的 , 此函数用于判断是否点击了 HeaderView, 并对做相应的处理 , 因为 HeaderView
    * 是画上去的 , 所以设置事件监听是无效的 , 只有自行控制 .
    */
   @Override
   public boolean onTouchEvent(MotionEvent ev) {
      if (mHeaderViewVisible) {
         switch (ev.getAction()) {
         case MotionEvent.ACTION_DOWN:
            mDownX = ev.getX();
            mDownY = ev.getY();
            if (mDownX <= mHeaderViewWidth && mDownY <= mHeaderViewHeight) {
               return true;
            }
            break;
         case MotionEvent.ACTION_UP:
            float x = ev.getX();
            float y = ev.getY();
            float offsetX = Math.abs(x - mDownX);
            float offsetY = Math.abs(y - mDownY);
            // 如果 HeaderView 是可见的 , 点击在 HeaderView 内 , 那么触发 headerClick()
            if (x <= mHeaderViewWidth && y <= mHeaderViewHeight && offsetX <= mHeaderViewWidth && offsetY <= mHeaderViewHeight) {
               if (mHeaderView != null) {
//                headerViewClick();
               }

               return true;
            }
            break;
         default:
            break;
         }
      }

      return super.onTouchEvent(ev);

   }

   @Override
   public void setAdapter(ExpandableListAdapter adapter) {
      super.setAdapter(adapter);
      mAdapter = (HeaderAdapter) adapter;
   }

   /**
    * 
    * 点击了 Group 触发的事件 , 要根据根据当前点击 Group 的状态来
    */
   @Override
   public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
      if (mAdapter.getGroupClickStatus(groupPosition) == 0) {
         mAdapter.setGroupClickStatus(groupPosition, 1);
         parent.expandGroup(groupPosition);
         // Header自动置顶
         // parent.setSelectedGroup(groupPosition);

      } else if (mAdapter.getGroupClickStatus(groupPosition) == 1) {
         mAdapter.setGroupClickStatus(groupPosition, 0);
         parent.collapseGroup(groupPosition);
      }

      // 返回 true 才可以弹回第一行 , 不知道为什么
      return true;
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      if (mHeaderView != null) {
         measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
         mHeaderViewWidth = mHeaderView.getMeasuredWidth();
         mHeaderViewHeight = mHeaderView.getMeasuredHeight();
      }
   }

   private int mOldState = -1;

   @Override
   protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
      super.onLayout(changed, left, top, right, bottom);
      final long flatPostion = getExpandableListPosition(getFirstVisiblePosition());
      final int groupPos = ExpandableListView.getPackedPositionGroup(flatPostion);
      final int childPos = ExpandableListView.getPackedPositionChild(flatPostion);
      int state = mAdapter.getHeaderState(groupPos, childPos);
      if (mHeaderView != null && mAdapter != null && state != mOldState) {
         mOldState = state;
         mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
      }

      configureHeaderView(groupPos, childPos);
   }

   public void configureHeaderView(int groupPosition, int childPosition) {
      if (mHeaderView == null || mAdapter == null || ((ExpandableListAdapter) mAdapter).getGroupCount() == 0) {
         return;
      }

      int state = mAdapter.getHeaderState(groupPosition, childPosition);

      switch (state) {
      case HeaderAdapter.PINNED_HEADER_GONE: {
         mHeaderViewVisible = false;
         break;
      }

      case HeaderAdapter.PINNED_HEADER_VISIBLE: {
         mAdapter.configureHeader(mHeaderView, groupPosition, childPosition, MAX_ALPHA);

         if (mHeaderView.getTop() != 0) {
            mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
         }

         mHeaderViewVisible = true;

         break;
      }

      case HeaderAdapter.PINNED_HEADER_PUSHED_UP: {
         View firstView = getChildAt(0);
         int bottom = firstView.getBottom();

         // intitemHeight = firstView.getHeight();
         int headerHeight = mHeaderView.getHeight();

         int y;

         int alpha;

         if (bottom < headerHeight) {
            y = (bottom - headerHeight);
            alpha = MAX_ALPHA * (headerHeight + y) / headerHeight;
         } else {
            y = 0;
            alpha = MAX_ALPHA;
         }

         mAdapter.configureHeader(mHeaderView, groupPosition, childPosition, alpha);

         if (mHeaderView.getTop() != y) {
            mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y);
         }

         mHeaderViewVisible = true;
         break;
      }
      }
   }

   @Override
   /**
    * 列表界面更新时调用该方法(如滚动时)
    */
   protected void dispatchDraw(Canvas canvas) {
      super.dispatchDraw(canvas);
      if (mHeaderViewVisible) {
         // 分组栏是直接绘制到界面中,而不是加入到ViewGroup中
         drawChild(canvas, mHeaderView, getDrawingTime());
      }
   }

   @Override
   public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      final long flatPos = getExpandableListPosition(firstVisibleItem);
      int groupPosition = ExpandableListView.getPackedPositionGroup(flatPos);
      int childPosition = ExpandableListView.getPackedPositionChild(flatPos);

      configureHeaderView(groupPosition, childPosition);
   }

   @Override
   public void onScrollStateChanged(AbsListView view, int scrollState) {
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@删库跑路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值