簡體   English   中英

Android我如何調用查看尋呼機的按鈕?

[英]Android How can i call view pager's button?

暈,大家好,我遵循了android開發人員的步驟:http://developer.android.com/training/animation/screen-slide.html

我該如何調用MainJava中的“ btnRight”和“ btnLeft”來創建onClick函數? 我對此一無所知,(???)。findViewByID嗎?

順便說一句,還有另一個更好的編碼設計可以產生這種效果嗎? 我想創建3個可以滑動或單擊以更改屏幕的頁面,我是否正確使用了此viewpager? 是否可能使用pagenumber技巧添加了一些新的視圖瞬間?

請幫我一點,謝謝!

MainJava.java:

public class MainJava extends FragmentActivity {

private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getActionBar();
    actionBar.hide();

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setCurrentItem(1); 

}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return ScreenSlidePageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

}

ScreenSlidePageFragment.java:

public class ScreenSlidePageFragment extends Fragment {

public static final String ARG_PAGE = "page";

private int mPageNumber;

public static ScreenSlidePageFragment create(int pageNumber) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.scroll_view_content, container, false);
    //rootView.addView(inflater.inflate(R.layout.scroll_view_content, null));

    View pageLeft = (View) rootView.findViewById(R.id.pageLeft);
    View pageRight = (View) rootView.findViewById(R.id.pageRight);
    View pageMain = (View) rootView.findViewById(R.id.pageMain);
    if (mPageNumber==0){
        pageLeft.setVisibility(View.VISIBLE);
        pageMain.setVisibility(View.GONE);
        pageRight.setVisibility(View.GONE);
    }else if (mPageNumber==1){
        pageLeft.setVisibility(View.GONE);
        pageMain.setVisibility(View.VISIBLE);
        pageRight.setVisibility(View.GONE);
    }else if (mPageNumber==2){
        pageLeft.setVisibility(View.GONE);
        pageMain.setVisibility(View.GONE);
        pageRight.setVisibility(View.VISIBLE);
    }

    return rootView;
}
public int getPageNumber() {
    return mPageNumber;
}

scroll_view_content.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Dummy content. -->

    <LinearLayout
        android:id="@+id/pageLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            style="?android:textAppearanceMedium"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:lineSpacingMultiplier="1.2"
            android:text="AAA" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/pageRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            style="?android:textAppearanceMedium"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:lineSpacingMultiplier="1.2"
            android:text="CCC"/>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/pageMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#123456"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textB"
            style="?android:textAppearanceMedium"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:background="#654321"
            android:text="BBB"/>
        <LinearLayout 
            android:layout_below="@+id/textB"
            android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
            <Button 
                android:id="@+id/btnLeft"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#00ffff"
                android:text="A"/>
            <Button 
                android:id="@+id/btnRight"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#ffff00"
                android:text="B"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

真的很簡單。 您必須使用視圖尋呼機類的setCurrentItem(int)。 該算法為:

  1. int currPage = 1 //指向viewpager中的第二頁

  2. 如果單擊左鍵// //監聽按鈕
    2a。 currPage--
    2b。 mPager.setCurrentItem(currPage)//分頁器是viewpager類的對象

  3. 如果右鍵單擊,// //監聽按鈕
    3a。 currPage ++
    3b。 mPpager.setCurrentItem(currPage)

查看鏈接以獲取信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM