我在应用程序中使用具有数据绑定的MVVM体系结构。我正在尝试在顶部显示图标,并在其下方显示文本,并在屏幕底部显示TabLayout。代码如下:
activity_main.xml:
Button logOut = new Button("Log Out");
logOut.setOnAction(e ->
MobileApplication.getInstance().goHome());
MainActivity.java:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="com.abc.MainActivity" />
<variable
name="manager"
type="android.support.v4.app.FragmentManager" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/main_toolbar"
layout="@layout/toolbar" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
app:handler="@{handler}" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
app:tabTextColor="@color/white"
app:pager="@{(pager)}">
</android.support.design.widget.TabLayout>
</LinearLayout>
</layout>
MainActionsAdapter.java
package com.abc;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import com.abc.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
WebView wv;
Handler uiHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setHandler(this);
binding.setManager(getSupportFragmentManager());
}
@BindingAdapter({"bind:handler"})
public static void bindViewPagerAdapter(final ViewPager view, final MainActivity activity)
{
final MainActionsAdapter adapter = new MainActionsAdapter(view.getContext(), activity.getSupportFragmentManager());
view.setAdapter(adapter);
}
@BindingAdapter({"bind:pager"})
public static void bindViewPagerTabs(final TabLayout view, final ViewPager pagerView)
{
view.setupWithViewPager(pagerView, true);
}
}
toolbar.xml:
package com.finchart;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import com.finchart.Fragments.ChartFragment;
import com.finchart.Fragments.MarketFragment;
import com.finchart.Fragments.MoreFragment;
public class MainActionsAdapter extends FragmentPagerAdapter
{
private static final int MARKETS = 0;
private static final int CHARTS = 1;
private static final int MORE = 2;
private static final int[] TABS = new int[]{MARKETS,CHARTS, MORE};
private Context mContext;
private Drawable myDrawable;
private String title;
public MainActionsAdapter(final Context context, final FragmentManager fm)
{
super(fm);
mContext = context.getApplicationContext();
}
@Override
public Fragment getItem(int position)
{
switch (TABS[position])
{
case MARKETS:
return MarketFragment.newInstance("","");
case CHARTS:
return ChartFragment.newInstance("","");
case MORE:
return MoreFragment.newInstance("","");
}
return null;
}
@Override
public int getCount() {
return TABS.length;
}
@Override
public CharSequence getPageTitle(int position)
{
switch (TABS[position])
{
case MARKETS:
myDrawable = getDrawableObject(R.drawable.baseline_account_balance_white_24);
title = mContext.getResources().getString(R.string.market_fragment);
break;
case CHARTS:
myDrawable =getDrawableObject(R.drawable.baseline_trending_up_24);
title = mContext.getResources().getString(R.string.chart_fragment);
break;
case MORE:
myDrawable = getDrawableObject(R.drawable.baseline_menu_white_24);
title = mContext.getResources().getString(R.string.more_fragment);
break;
}
SpannableStringBuilder sb = new SpannableStringBuilder(" " + title);
try {
myDrawable.setBounds(5, 5, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(myDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Drawable getDrawableObject(int image){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return mContext.getDrawable(image);
} else {
return mContext.getResources().getDrawable(image);
}
}
}
我经历过各种类似的SO线程,但是在我的情况下,它们都无法正常工作,例如enter link description here
因此,请告诉我我在这里做错了什么,是否有可能实现这一目标,或者任何替代方法也都很好。 谢谢。
答案 0 :(得分:0)
在MainActivity.java的bindViewPagerTabs(...)中,我更新了以下代码:
@BindingAdapter({"bind:pager"})
public static void bindViewPagerTabs(final TabLayout view, final ViewPager pagerView)
{
view.setupWithViewPager(pagerView, true);
view.getTabAt(0).setIcon(R.drawable.white_24);
view.getTabAt(1).setIcon(R.drawable.up_24);
view.getTabAt(2).setIcon(R.drawable.baseline_24);
}
并从MainActionsAdapter中删除了所有多余的代码。