Android 带有自定义视图的ActionBar选项卡未切换
我有以下带有自定义选项卡的操作栏: 如果我想通过单击下面的红色区域来切换选项卡,它会起作用: 但是,如果我在自定义视图中单击,它不会切换视图(即,如果我在下面的红色区域中单击): (为糟糕的图纸道歉!---) 以下是我尝试过的代码(包括Android 带有自定义视图的ActionBar选项卡未切换,android,android-custom-view,android-tabs,Android,Android Custom View,Android Tabs,我有以下带有自定义选项卡的操作栏: 如果我想通过单击下面的红色区域来切换选项卡,它会起作用: 但是,如果我在自定义视图中单击,它不会切换视图(即,如果我在下面的红色区域中单击): (为糟糕的图纸道歉!---) 以下是我尝试过的代码(包括setOnClickListener): // Create the adapter that will return a fragment for each of the three // primary sections of the a
setOnClickListener
):
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
final int position = i;
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View rootView = layoutInflater.inflate(R.layout.tabs_add_item, null);
if(rootView != null) {
FontTextView tv = (FontTextView) rootView.findViewById(R.id.tab_title);
tv.setText(mSectionsPagerAdapter.getPageTitle(i));
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.tab);
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ADDITEM", "layout Clicked");
mViewPager.setCurrentItem(position);
}
});
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ADDITEM", "view Clicked");
mViewPager.setCurrentItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setCustomView(rootView)
.setTabListener(this));
}
}
}
当然还有自定义选项卡布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
xmlns:vers="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="0dp">
<uk.verscreative.smart5_a_day.views.widget.FontTextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:inputType="textCapCharacters"
android:textColor="@color/white"
android:text="TEXT"
android:singleLine="true"
vers:customTypeface="fonts/segoeui.ttf"
/>
</RelativeLayout>
如果你们需要更多的细节,请告诉我!提前谢谢 我意识到我的自定义文本视图是
match\u parent
ing,因此我将setOnClickListener
更改为:
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
final int position = i;
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View rootView = layoutInflater.inflate(R.layout.tabs_add_item, null);
if(rootView != null) {
FontTextView tv = (FontTextView) rootView.findViewById(R.id.tab_title);
tv.setText(mSectionsPagerAdapter.getPageTitle(i));
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setCustomView(rootView)
.setTabListener(this));
}
}
//对于应用程序中的每个部分,在操作栏中添加一个选项卡。
对于(int i=0;i
添加它现在工作 png的外观很像sameI刚刚意识到他们更新了我正在更新的同一个图像-u-选项卡的自定义视图真的需要它自己的onClickListener()吗?我尝试过使用和不使用onClickListener(),但都不适用于突出显示的区域(图3):(
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
final int position = i;
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View rootView = layoutInflater.inflate(R.layout.tabs_add_item, null);
if(rootView != null) {
FontTextView tv = (FontTextView) rootView.findViewById(R.id.tab_title);
tv.setText(mSectionsPagerAdapter.getPageTitle(i));
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setCustomView(rootView)
.setTabListener(this));
}
}