android SlidingTabLayout实现ViewPager页卡滑动效果

先来张效果图(可以滑动切换页卡)

android SlidingTabLayout实现ViewPager页卡滑动效果_第1张图片


主页面布局文件 remind_auction_new_list.xml :

<RelativeLayout
    style="@style/Rosemary.Plane"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!--头部布局-->
    <com.liam.rosemary.ui.view.SlidingTabLayout
        android:id="@+id/stl_square"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <!--内容页-->
    <android.support.v4.view.ViewPager
        android:id="@+id/vp_square"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/stl_square"/>

    <!--分割线-->
    <ImageView
        style="@style/Rosemary.Line"
        android:layout_below="@id/stl_square"
        android:src="@drawable/horizontal_shadow"/>

</RelativeLayout>



主页面代码:

public class MyRemindAuctionNewListActivity extends ActionBarActivity implements IInit {

    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;

    private RemindAdapter mRemindAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remind_auction_new_list);

        init();
    }

    @Override
    public void init() {
        ActionBarUtil.setup(this, R.string.my_remind);

        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.stl_square);
        mViewPager =(ViewPager)findViewById(R.id.vp_square);

        // 设置ViewPager属性
        mRemindAdapter = new RemindAdapter(getSupportFragmentManager(), this);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(mRemindAdapter);

        // 定义 SlidingTabLayout
        mSlidingTabLayout.setDistributeEvenly(true);
        mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.primary));
        mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.white));
        mSlidingTabLayout.setCustomTabView(R.layout.view_tab, R.id.tv_text);
        mSlidingTabLayout.setViewPager(mViewPager); // 加载ViewPager

    }

}


为ViewPager添加多个页卡的Adapter:

public class RemindAdapter extends FragmentStatePagerAdapter {

    private String[] titles;
    private Context ctx;

    public RemindAdapter(FragmentManager fm, Context ctx) {
        super(fm);
        this.ctx = ctx;
        titles = ctx.getResources().getStringArray(R.array.remind_type);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {//专场提醒
            return MyRemindAuctionListFragment.newInstance(0);
        } else {//卖家提醒
            return MyRemindSallerListFragment.newInstance(1);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    @Override
    public int getCount() {
        return titles.length;
    }
}


每个页卡实际内容的Fragment:

public class MyRemindAuctionListFragment extends BaseFragment implements IPagination, IResponseHandler, SwipeRefreshLayout.OnRefreshListener {

    private int mTag;
    private ListView mListView;//拍场列表
    private ListAdapter<AuctionInfoViewModel> mAdapter;//拍场适配器
    private MyRemindAuctionNewListActivity mActivity;

    //下拉刷新组件
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private AuctionInfoViewModel mViewModel = new AuctionInfoViewModel();
    private TextView mEmptyView;   // 空白视图

    public static MyRemindAuctionListFragment newInstance(int tag) {
        MyRemindAuctionListFragment f = new MyRemindAuctionListFragment();
        f.mTag = tag;
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.remind_auction_list, null);

        mSwipeRefreshLayout = $(v, R.id.srl_refresh);
        mListView = $(v, R.id.remind_auction_listview);
        mEmptyView = $(v, R.id.tv_remind_warning);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.background_blue_standard, R.color.white, R.color.background_blue_standard, R.color.white);
        fetchData(FIRST);

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof MyRemindAuctionNewListActivity) {
            mActivity = ((MyRemindAuctionNewListActivity) activity);
        }
    }

    @Override
    public void fetchNewData(int tag) {
        mViewModel.increasePageIndex();
        fetchData(FIRST);
    }

    @Override
    public void fetchData(int tag) {
        mActivity.toggleProgress(true);
        GetMyRemindAuctionListParam param = new GetMyRemindAuctionListParam(Data.getUserID(), mViewModel.getPageIndex(), Data.PAGE_SIZE_MEDIUM);
        AuctionApi.getMyRemindAuctionList(this, param);
    }

    @Override
    public void updateUI(Object response, int tag) {
        mActivity.toggleProgress(false);
        mSwipeRefreshLayout.setRefreshing(false);

        mViewModel.inflate(response);

        if (mAdapter == null) {

            mAdapter = new ListAdapter<AuctionInfoViewModel>(mActivity, mViewModel.getList(), R.layout.item_list_remind_auction) {
                @Override
                protected void onBind(int position, AuctionInfoViewModel item, ViewHolder holder) {
                    holder.setImageUrl(R.id.head_icon, item.getImageUrl());
                    holder.setText(R.id.title, item.getAuctionIDStr());
                    holder.setText(R.id.sub_title, item.getAuctionName());
                    holder.setText(R.id.tv_auction_status, item.getStatusStr());
                    ((TextView) holder.get(R.id.tv_auction_status)).setTextColor(getResources().getColor(item.getColor()));
                }
            };

            mListView.setAdapter(mAdapter);
            mEmptyView.setText(R.string.notice_auction_no_remind);
            mEmptyView.setGravity(Gravity.CENTER);
            mListView.setEmptyView(mEmptyView);

            /**
             *  上拉更多
             */
            mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    switch (scrollState) {
                        case SCROLL_STATE_IDLE:

                            if (mListView.getLastVisiblePosition() == mViewModel.getList().size() - 1) {
                                if (!mViewModel.isComplete()) {
                                    fetchNewData(FIRST);
                                }
                            }
                            break;
                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                }
            });
        } else {
            mAdapter.notifyDataSetChanged();
        }

        mListView.setOnItemClickListener(new onItemClickListener());
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void refresh() {// 重置页索引、适配器
        mViewModel.reset();
        fetchData(FIRST);
    }

    @Override
    public int getTotal() {
        if (mAdapter == null) return 0;
        return mAdapter.getCount();
    }

    /**
     * 监听事件,以此启动不同页面
     */
    class onItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int temp = mViewModel.getList().get(position).getStatus();
            int auctionID = mViewModel.getList().get(position).getAuctionID();

            Intent mIntent = new Intent();
            switch (temp) {
                case 1:
                    mIntent.setClass(getActivity(), AuctionPreviewActivity.class);
                    break;
                case 2:
                    mIntent.setClass(getActivity(), AuctionPreviewActivity.class);
                    break;
                case 3:
                    mIntent.setClass(getActivity(), AuctionPreviewActivity.class);
                    break;
                default:
                    break;
            }
            mIntent.putExtra(EnumIntentKey.AUCTION_ID.toString(), auctionID);
            startActivity(mIntent);
        }
    }
}




你可能感兴趣的:(viewpager,滑动,页卡)