博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android SlidingTabLayout实现ViewPager页卡滑动效果
阅读量:6315 次
发布时间:2019-06-22

本文共 7506 字,大约阅读时间需要 25 分钟。

先来张效果图(能够滑动切换页卡)

主页面布局文件 remind_auction_new_list.xml :

主页面代码:

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

你可能感兴趣的文章
Docker 自定义SSH服务镜像
查看>>
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
buildroot下查找外部编译器通过ext-toolchain-wrapper调用的参数
查看>>
MySQL Replication 主主配置详细说明
查看>>
Linux的任务调度
查看>>
在Android studio中添加jar包方法如下
查看>>
iframe 在ie下面总是弹出新窗口解决方法
查看>>
分享10款漂亮实用的CSS3按钮
查看>>
安装nginx 常见错误及 解决方法
查看>>
在之前链表的基础上改良的链表
查看>>
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>
Eclipse 添加 Ibator
查看>>
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
查看>>
Python编程语言
查看>>
十四、转到 linux
查看>>
Got error 241 'Invalid schema
查看>>
ReferenceError: event is not defined
查看>>
男人要内在美,更要外在美
查看>>