gpt4 book ai didi

android - 在 Android 上为每个选项卡滑动更改操作栏标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:37 24 4
gpt4 key购买 nike

我已经使用 ActionBarSherlock 和 Fragments 设置了带有滑动选项卡的操作栏。

我想为每个选项卡设置不同的操作栏标题。如果我可以设置图像而不是标题,那就太好了,这样每个选项卡都会有不同的操作栏图像标题。

如果有任何建议和教程链接,我将不胜感激!谢谢:)

这是我的代码:TabActivity.java

package com.tptabs;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.support.v4.view.ViewPager;

public class TabActivity extends SherlockFragmentActivity {

private ViewPager mViewPager;
private TabAdapter mTabsAdapter;

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

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);

final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

mTabsAdapter = new TabAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.social_group)), JoinFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.content_edit)), CreateFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.location_web_site)), PlayFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.collections_collection)), ResultFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.device_access_accounts)), StoreFragment.class, null);
}
}

TabAdapter.java

package com.tptabs;
import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class TabAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "21st Polling:";

static final class TabInfo{
private final Class<?> clss;
private final Bundle args;

TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}

public TabAdapter(SherlockFragmentActivity fa, ViewPager pager) {
super(fa.getSupportFragmentManager());
mContext = fa;
mActionBar = fa.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}

@Override
public void onPageScrollStateChanged(int state) {


}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


}

@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(mContext, "You've deselected a tab", Toast.LENGTH_SHORT).show();
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}

@Override
public int getCount() {
return mTabs.size();
}

}

最佳答案

您需要像这样将 addOnPageChangeListener 设置为您的 View 寻呼机

 pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if (position == 1) {
setTitle("Page 1");
} else if (position == 2)
{
setTitle("Page 2");
}
else
{
setTitle("Page 3");
}


}

关于android - 在 Android 上为每个选项卡滑动更改操作栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com