Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android SlidingTabLayout實現ViewPager頁卡滑動效果

android SlidingTabLayout實現ViewPager頁卡滑動效果

編輯:關於Android編程

先來張效果圖(可以滑動切換頁卡)

\

 

主頁面布局文件 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);
        }
    }
}




 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved