0

我试图从android-playground 项目中开发的 SwipeyTabsSampleActivity 制作另一个样本

这是示例代码,我的问题是为什么在方法 getTab->view.setOnClickListener 中引用两个变量“mViewPager”和“position”时没有出现编译错误

这是完整的示例源代码。我首先尝试了示例代码,它编译并运行良好,当我尝试根据我的需要自定义示例时,我不知道如何。一切都很顺利(编译和运行),除了当我打算设置 onClickListener 并引用这两个变量时,我得到以下两个编译错误 1- 无法对非静态字段 mViewPager 进行静态引用 2- 无法引用非以不同方法定义的内部类中的最终变量位置

我认为这两个错误也应该出现在示例代码中,不仅是我的代码,而且我不知道为什么?

/*
 * Copyright 2011 Peter Kuterna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.peterkuterna.android.apps.swipeytabs;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SwipeyTabsSampleActivity extends FragmentActivity {

    private static final String [] TITLES = {
        "CATEGORIES",
        "FEATURED",
        "TOP PAID",
        "TOP FREE",
        "TOP GROSSING",
        "TOP NEW PAID",
        "TOP NEW FREE",
        "TRENDING",
    };

    private SwipeyTabs mTabs;
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_swipeytab);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

        SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
                getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mTabs.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(mTabs);
        mViewPager.setCurrentItem(0);
    }

    private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
            SwipeyTabsAdapter {

        private final Context mContext;

        public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);

            this.mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            return SwipeyTabFragment.newInstance(TITLES[position]);
        }

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

        public TextView getTab(final int position, SwipeyTabs root) {
            TextView view = (TextView) LayoutInflater.from(mContext).inflate(
                    R.layout.swipey_tab_indicator, root, false);
            view.setText(TITLES[position]);
            view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    mViewPager.setCurrentItem(position);
                }
            });

            return view;
        }

    }

}
4

1 回答 1

1

getTab()是 的非静态内部类的实例方法SwipeyTabsSampleActivity,因此它具有对其外部类实例的隐式引用,并且可以访问此实例的所有字段和方法,因此可以访问mViewPager. 是的OnclickListener内部类的匿名内部类SwipeyTabsSampleActivity,因此也可以访问其两个外部类实例的所有字段和方法。

如果内部类是静态的,它将无法编译。如果mViewPager是方法的非最终局部变量getTab(),它也不会编译。

于 2012-11-17T14:01:36.367 回答