android action 页面,android – 页面卷曲在Action栏标签中不起作...

博主尝试在Action Bar的tabs中结合ViewPager实现页面卷曲效果,目标是当滑动ViewPager时,操作栏标签能随页面一起卷曲滑动。目前实现了CurlView,但还不清楚如何将其与ViewPager集成。代码中展示了MainActivity、PageProvider、SizeChangedObserver等关键类的实现。问题在于如何在ViewPager中添加页面卷曲功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

>我参考了Harism页卷曲,在Action栏中实现它

tabs.Because因为我认为这是唯一应该满足我的要求.

>下面我展示了到目前为止我得到的输出.

>我需要在Viewpager.So中添加寻呼机卷曲,即操作栏标签

将与卷曲页面一起滑动.

>下面我发布了与此相关的代码.

MainActivity.java:

public class MainActivity extends FragmentActivity implements

ActionBar.TabListener {

private CurlView mCurlView;

private ViewPager viewPager;

private TabsPagerAdapter mAdapter;

private ActionBar actionBar;

@SuppressWarnings("unused")

private Menu optionsMenu;

private MenuItem menuItem;

// Tab titles

private String[] tabs = { "Top Rated", "Games", "Movies" };

@SuppressWarnings("deprecation")

@SuppressLint("NewApi")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

int index = 0;

if (getLastNonConfigurationInstance() != null) {

index = (Integer) getLastNonConfigurationInstance();

}

mCurlView = (CurlView) findViewById(R.id.curl);

mCurlView.setPageProvider(new PageProvider());

mCurlView.setSizeChangedObserver(new SizeChangedObserver());

mCurlView.setCurrentIndex(index);

mCurlView.setBackgroundColor(0xFF202830);

// Initilization

viewPager = (ViewPager) findViewById(R.id.pager);

actionBar = getActionBar();

mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);

actionBar.setHomeButtonEnabled(false);

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME

| ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);

// Adding Tabs

for (String tab_name : tabs) {

actionBar.addTab(actionBar.newTab().setText(tab_name)

.setTabListener(this));

}

/**

* on swiping the viewpager make respective tab selected

* */

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override

public void onPageSelected(int position) {

// on changing the page

// make respected tab selected

actionBar.setSelectedNavigationItem(position);

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

});

}

@Override

public void onPause() {

super.onPause();

mCurlView.onPause();

}

@Override

public void onResume() {

super.onResume();

mCurlView.onResume();

}

@Override

public Object getLastCustomNonConfigurationInstance() {

return mCurlView.getCurrentIndex();

}

/**

* Bitmap provider.

*/

private class PageProvider implements CurlView.PageProvider {

// Bitmap resources.

private int[] mBitmapIds = { R.layout.fragment_top_rated, R.layout.fragment_games,

R.layout.fragment_movies };

@Override

public int getPageCount() {

return 2;

}

private Bitmap loadBitmap(int width, int height, int index) {

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Log.d("index", String.valueOf(index));

View v = inflater.inflate(mBitmapIds[index], null);

v.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),

MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),

Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(b);

v.draw(c);

return b;

}

@Override

public void updatePage(CurlPage page, int width, int height, int index) {

switch (index) {

// First case is image on front side, solid colored back.

default:

Bitmap front = loadBitmap(width, height, 0);

page.setTexture(front, CurlPage.SIDE_FRONT);

page.setColor(Color.rgb(180, 180, 180), CurlPage.SIDE_BACK);

break;

}

}

}

/**

* CurlView size changed observer.

*/

private class SizeChangedObserver implements CurlView.SizeChangedObserver {

@Override

public void onSizeChanged(int w, int h) {

if (w > h) {

mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);

mCurlView.setMargins(.1f, .05f, .1f, .05f);

} else {

mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);

// mCurlView.setMargins(.1f, .1f, .1f, .1f);

}

}

}

}

activity_main.xml中:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/curl"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:id="@+id/pager"

android:layout_width="match_parent"

android:layout_height="match_parent" >

TopRatedFragment.java:

public class TopRatedFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

return rootView;

}

}

fragment_top_rated.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fa6a6a"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:gravity="center"

android:text="Design Top Rated Screen"

android:textSize="20dp" />

TabsPagerAdapter.java:

import info.androidhive.tabsswipe.GamesFragment;

import info.androidhive.tabsswipe.MoviesFragment;

import info.androidhive.tabsswipe.TopRatedFragment;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {

super(fm);

}

@Override

public Fragment getItem(int index) {

switch (index) {

case 0:

// Top Rated fragment activity

return new TopRatedFragment();

case 1:

// Games fragment activity

return new GamesFragment();

case 2:

// Movies fragment activity

return new MoviesFragment();

}

return null;

}

@Override

public int getCount() {

// get item count - equal to number of tabs

return 3;

}

}

我的问题是我需要在ViewPager.So中添加页面curl,我可以和viewpager一起刷卡.我不知道该怎么做.但是我想要得到它.任何建议都是最受欢迎的.谢谢你.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值