有没有办法在设计支持 TabLayout 中动态更新选项卡的标题?我尝试在 Adapter 中添加一个方法,该方法更改保存选项卡标题的 ArrayList 的内容并通知适配器,但选项卡标题不会更改为新标题。
适配器:
public class TabAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments = new ArrayList<>();
private final List<String> fragmentTiles = new ArrayList<>();
public TabAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
fragments.add(fragment);
fragmentTiles.add(title);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTiles.get(position);
}
public void setFragmentTiles(int index, String title) {
fragmentTiles.set(index, title);
Log.e("ARRAY", …Run Code Online (Sandbox Code Playgroud) arraylist android-arrayadapter android-design-library androiddesignsupport android-tablayout
我可以通过代码更改 TabLayout 中 Tabs 的滑动方向吗?
当我的设备语言为英语时它工作正常,但是当我将其更改为阿拉伯语时,许多选项卡消失并且滑动的方向仍然相同(向右)。注意:在 Manifest 文件中支持 RTL 选项为“true”。
活动:
public class PreviewsFragment extends Fragment {
private ViewPager mPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.section_all_icons, container, false);
ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (toolbar != null)
toolbar.setTitle(R.string.section_two);
mPager = (ViewPager) root.findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
TabLayout mTabs = (TabLayout) layout.findViewById(R.id.tabs); //layout: qualifier must be an expression.
mTabs.setupWithViewPager(mPager);
mTabs.setTabTextColors(getResources().getColor(R.color.semitransparent_white),
getResources().getColor(android.R.color.white));
mTabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.accent));
mTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab …Run Code Online (Sandbox Code Playgroud) 我有一个简单的项目,在 MainActivity 中有一个 TabLayout,有 3 个选项卡,每个选项卡都有一个 SwipeRefreshLayout。
如果我拉动以刷新第一个选项卡,然后在第一个选项卡仍在刷新时移动到第三个选项卡,当我返回第一个选项卡时,看起来第一个选项卡上有一个视图,其中包含状态的快照在我移动到第三个选项卡之前的选项卡。我可以滚动项目并正常使用选项卡。
请看下面的图片和我的代码。
我可以关闭“刷新指示器”调用 swipeLayout.setRefreshing(false); 在 ContentFragment 的 onPause 中,但覆盖视图保持在那里。
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
PagerAdapter pageAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pageAdapter);
tabLayout.setupWithViewPager(viewPager);
}
static class PagerAdapter extends FragmentPagerAdapter {
private final int TAB_COUNT = 3;
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new ContentFragment(); …Run Code Online (Sandbox Code Playgroud) 当我尝试在工具栏下面添加TabLayout时得到了一些提示。我尝试通过添加来尝试解决此问题中建议的选项卡<android.support.design.widget.TabLayout />,但该选项卡位于工具栏的顶部。而且,当我做这个博客之类的事情时,它仍然无法正常工作。
这是我的代码。我从创建它,scrolling activity然后通过添加进行更改tablayout。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.myquestionth.myquestionth10.activity.Profile4Activity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_profile4" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
请帮助和建议。
android android-layout android-fragments android-studio android-tablayout
我正在使用TabLayout两个嵌套的片段,我注意到当用户点击另一个Tab时,内容立即改变,指示器从第一个Tab移动到第二个Tab需要3-4秒.
我在迄今为止尝试应用程序的任何设备中都有相同的行为(不仅仅是genymotion).Nexus 4和Nexus 5X是一些测试设备.
布局是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:EMVideoView="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:clickable="true"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.devbrackets.android.exomedia.EMVideoView
android:id="@+id/video_play_activity_video_view"
android:layout_width="match_parent" android:layout_height="360dp"
EMVideoView:defaultControlsEnabled="true"/>
<android.support.design.widget.TabLayout
android:id="@+id/nested_tabs" android:layout_below="@+id/video_play_activity_video_view"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
app:tabMode="fixed" app:tabGravity="fill"/>
<FrameLayout android:id="@+id/fl_nested_tabs_container" android:layout_below="@+id/nested_tabs"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
和我更改标签的代码:
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (isCommentsFragmentSelected) {
isCommentsFragmentSelected = false;
getChildFragmentManager()
.beginTransaction()
.replace(R.id.fl_nested_tabs_container, PollsFragment.newInstance())
.commit();
} else {
isCommentsFragmentSelected = true;
getChildFragmentManager()
.beginTransaction()
.replace(R.id.fl_nested_tabs_container, CommentsFragment.newInstance())
.commit();
}
}
Run Code Online (Sandbox Code Playgroud) 我得到了一个表格:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:elevation="4dp"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabBackground="@color/colorPrimary"
app:tabTextColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"/>
Run Code Online (Sandbox Code Playgroud)
和一个工具栏,但工具栏和分隔线之间仍然有一条小线。我还没有设置工具栏样式。如何取下分隔线?
我目前正在开发 v22 的应用程序并使用以下库:
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:design:22.2.1'
Run Code Online (Sandbox Code Playgroud)
我只有一个名为HomeActivity 的活动,里面有一个导航抽屉。这是activity_home.xml布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:menu="@menu/menu_navigation"/>
Run Code Online (Sandbox Code Playgroud)
因此,每次我从导航抽屉中选择一个项目时,我都会@+id/content_frame用一个新的 Fragment替换 FrameView ,如下所示:
int id = menuItem.getItemId();
switch (id)
{
case R.id.gazzette:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new ListViewFragment())
.commit();
drawerLayout.closeDrawers();
break;
case R.id.concorsi: …Run Code Online (Sandbox Code Playgroud) android fragment material-design android-elevation android-tablayout
我想实现这个动画,它是覆盖标签布局的搜索编辑框,我必须告诉我在父布局上尝试了这个代码android:animateLayoutChanges="true"并设置了标签布局的可见性,View.GONE但它只是动画标签向上移动,而不是搜索框覆盖制表符布局.
我有将数据传递给片段的问题.它在生产中的所有时间都有0.1%的崩溃.让我们说100k开放活动它发生了100次.它看起来并不常见,但它非常困扰我,我认为我在使用数据进行片段初始化时出错了.问题是,我只创建了一次片段,而其他所有时间我都需要将数据传递给它们我正在做下一步:myFragmentInstance.setData(Object someData);发生崩溃是因为它告诉片段中的那些视图元素未找到并且它们是NULL,但如果我没有重新创建它们,一切都应该没问题.我没有旋转手机,也没有足够的内存.它发生在网络重新连接上,因为在网络重新连接时,我将服务器获取新数据,然后将新数据设置为我的片段.我有我使用的两个片段的字段照片,maby你们中的一些人知道这些数据可以告诉崩溃时片段的状态.我正在使用库ButterKnife初始化片段和活动的字段,而不是初始化它findById,maby它有一些影响或没有?
这里是简单项目的链接(在github上只有这个问题): https ://github.com/yozhik/Reviews/tree/master/app/src/main/java/com/ylet/sr/review
描述:
CollapsingActivity- 活动Collapsing AppBarLayout.它将一个或两个片段加载到" fragment_content_holder"中,并且必须TabLayout在视图寻呼机中的片段之间切换.
在活动方法onCreate()- 我只是模拟对server(loadData)的请求,并且当加载一些假数据时 - 我在第一次调用时在视图寻呼机中显示片段 - 我正在创建新的TabMenuAdapter扩展FragmentPagerAdapter,用片段填充它并保存到实例的链接.在下一个调用中 - 我不会从头开始创建片段,只是用新数据填充它们.
MenuFragment1, MenuFragment1 - 两个片段.MenuFragment1- 有方法public void setupData(SomeCustomData data),设置新数据,不在网络重新连接上重新创建片段.
NetworkStateReceiver - 收听网络更改并发送通知.
TabMenuAdapter - 只是简单的类来保存片段.
05-11 18:11:05.088 12279-12279/com.myProjectName E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myProjectName, PID: 12279
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95) …Run Code Online (Sandbox Code Playgroud) android android-fragments fragmentpageradapter butterknife android-tablayout