无法打开"评论"选项卡首先

时间:2014-11-03 09:51:25

标签: android android-fragments android-viewpager

在我的应用程序中,我自定义了Fragment Tab Activity。这个应用程序运行顺畅。当此应用程序启动“关于”选项卡时选中。我怎样才能将此“关于”更改为“评论”标签????

这是代码:

private TextView chatView, address, findView;
private ViewPager mViewPager;
private TextView mBottomLine;
private int screenWidth = 0;

private int currentIndex = 0;

private MyPagerAdapter myPagerAdapter;
private List<Fragment> mFragmentList = new ArrayList<Fragment>();
private List<String> mTitleList = new ArrayList<String>();

CustomFragment1 chatFragment;
CustomFragment2 findFragment;
CustomFragment3 addressFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initComponents();
}

@SuppressWarnings("deprecation")
private void initComponents() {

    this.mBottomLine = (TextView) findViewById(R.id.bottom_line);
    this.screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    this.chatView = (TextView) findViewById(R.id.chat_top_info);
    this.findView = (TextView) findViewById(R.id.find_top_info);
    this.address = (TextView) findViewById(R.id.address_top_info);

    TopTabClickListener clickListener = new TopTabClickListener();

    this.chatView.setOnClickListener(clickListener);
    this.findView.setOnClickListener(clickListener);
    this.address.setOnClickListener(clickListener);

    this.mViewPager = (ViewPager)findViewById(R.id.viewPager);

    chatFragment = new CustomFragment1();
    mFragmentList.add(chatFragment);

    findFragment = new CustomFragment2();
    mFragmentList.add(findFragment);

    addressFragment = new CustomFragment3();
    mFragmentList.add(addressFragment);

    myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);
    mViewPager.setAdapter(myPagerAdapter);
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {}

        @Override
        public void onPageScrolled(int index, float arg1, int pixes) {
            if(pixes != 0){
                mBottomLine.layout((int) ((index + arg1) * screenWidth / 3), 0, (int) ((index + 1 + arg1) * screenWidth / 3), mBottomLine.getWidth());
            }
            if (pixes == 0) {
                currentIndex = index;
                changeIndex(currentIndex);
            }
           // System.out.println("the state is : " + arg1 + "   and index is " + index);

            Log.d("Index Is", String.valueOf(index));
            Log.d("the state is", String.valueOf(arg1));
        }

        @Override
        public void onPageScrollStateChanged(int state) {}
    });
}

public void onLeftBtnClick(View view){
    this.finish();
}

private class TopTabClickListener implements OnClickListener{

    @Override
    public void onClick(View view) {
        if (view.getId() == chatView.getId()) {
            mViewPager.setCurrentItem(0);

        }else if(view.getId() == findView.getId()){
            mViewPager.setCurrentItem(1);

        }else{
            mViewPager.setCurrentItem(2);
        }
        changeIndex(mViewPager.getCurrentItem());
    }

}

private void changeIndex(int index){
    if(index == 0){
        chatView.setTextColor(getResources().getColor(R.color.green));
        findView.setTextColor(getResources().getColor(R.color.black));
        address.setTextColor(getResources().getColor(R.color.black));
    }else if(index == 1){
        findView.setTextColor(getResources().getColor(R.color.green));
        chatView.setTextColor(getResources().getColor(R.color.black));
        address.setTextColor(getResources().getColor(R.color.black));
    }else {

        address.setTextColor(getResources().getColor(R.color.green));
        findView.setTextColor(getResources().getColor(R.color.black));
        chatView.setTextColor(getResources().getColor(R.color.black));
    }
    mBottomLine.layout((int) (index * screenWidth / 3), 0, (int) ((index + 1) * screenWidth / 3), mBottomLine.getWidth());
}

private class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragmentList;
    private List<String> titleList;

    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList,
                          List<String> titleList) {
        super(fm);
        this.fragmentList = fragmentList;
        this.titleList = titleList;
    }


    @Override
    public Fragment getItem(int arg0) {
        return (fragmentList == null || fragmentList.size() == 0) ? null
                : fragmentList.get(arg0);
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return (titleList.size() > position) ? titleList.get(position) : "";
    }

    @Override
    public int getCount() {
        return fragmentList == null ? 0 : fragmentList.size();
    }
}

这是欲望OP SS

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试这样可以帮到你,

@SuppressWarnings("deprecation")
private void initComponents() {

    this.mBottomLine = (TextView) findViewById(R.id.bottom_line);
    this.screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    this.chatView = (TextView) findViewById(R.id.chat_top_info);
    this.findView = (TextView) findViewById(R.id.find_top_info);
    this.address = (TextView) findViewById(R.id.address_top_info);

    TopTabClickListener clickListener = new TopTabClickListener();

    this.chatView.setOnClickListener(clickListener);
    this.findView.setOnClickListener(clickListener);
    this.address.setOnClickListener(clickListener);

    this.mViewPager = (ViewPager)findViewById(R.id.viewPager);

    chatFragment = new CustomFragment1();
    mFragmentList.add(chatFragment);

    findFragment = new CustomFragment2();
    mFragmentList.add(findFragment);

    addressFragment = new CustomFragment3();
    mFragmentList.add(addressFragment);

    myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);
    mViewPager.setAdapter(myPagerAdapter);
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {}

        @Override
        public void onPageScrolled(int index, float arg1, int pixes) {
            if(pixes != 0){
                mBottomLine.layout((int) ((index + arg1) * screenWidth / 3), 0, (int) ((index + 1 + arg1) * screenWidth / 3), mBottomLine.getWidth());
            }
            if (pixes == 0) {
                currentIndex = index;
                changeIndex(currentIndex);
            }
           // System.out.println("the state is : " + arg1 + "   and index is " + index);

            Log.d("Index Is", String.valueOf(index));
            Log.d("the state is", String.valueOf(arg1));
        }

        @Override
        public void onPageScrollStateChanged(int state) {}
    });

     this.mViewPager.setCurrentItem(//review tab position)
}

Here is SS enter image description here

答案 1 :(得分:0)

我相信myPagerAdapter.setCurrentItem(2)可以解决问题:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initComponents();
    myPagerAdapter.setCurrentItem(2);
}