android.widget.FrameLayout无法转换为com.example.crowderia.chat.view.SnapTabView吗? [英] android.widget.FrameLayout cannot be cast to com.example.crowderia.chat.view.SnapTabView?

查看:83
本文介绍了android.widget.FrameLayout无法转换为com.example.crowderia.chat.view.SnapTabView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架布局调用view_snap_tabs.xml,其图像和视图如下所示

I have a frame layout call view_snap_tabs.xml that have image and view like below

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="184dp"
    xmlns:tools="http://schemas.android.com/tools"
    tools:layout_gravity="bottom"
    tools:background="@color/light_purple">

    <ImageView
        android:id="@+id/vst_bottom_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="48dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/small_circle"/>

    <View
        android:id="@+id/vst_indicator"
        android:layout_width="48dp"
        android:layout_height="4dp"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="44dp"
        android:background="@drawable/indicator_background"/>

</FrameLayout>

我已经将这种布局包含在我的activity_main.xml中,如下所示

I have included this layout in my activity_main.xml like below

<include
        layout="@layout/view_snap_tabs"
        android:id="@+id/am_snap_tabs"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

我已经创建了一个方法调用SetUpViewPager,我正尝试在如下所示的主要活动中添加该方法

I have created a method call SetUpViewPager and I'm trying to add that method in main activity like below

SnapTabView snapTabView = findViewById(R.id.am_snap_tabs);
snapTabView.setUpWithViewPager(viewPager);

但是当我运行该应用程序时,它将从那里崩溃并给出一个ClassCastExeception如何解决此问题?

But when I run the application It will crash from there and give a ClassCastExeception how can I fix this?

编辑=========================================== =======================

public class SnapTabView extends FrameLayout implements ViewPager.OnPageChangeListener {

    private ImageView mCenterImage;
    private ImageView mStartImage;
    private ImageView mBottomImage;
    private ImageView mEndImage;
    private View mIndicator;
    private ArgbEvaluator mArgbEvaluator;
    private int mCenterColor;
    private int mSideColor;
    private int mEndViewsTranslationX;
    private int mIndicatorTranslationX;
    private int mCenterTransationY;

    public SnapTabView(@NonNull Context context) {
        this(context, null);
    }
    public SnapTabView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public SnapTabView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_snap_tabs, this, false);
        mCenterImage = (ImageView) findViewById(R.id.vst_center_image);
        mBottomImage = (ImageView) findViewById(R.id.vst_bottom_image);
        mEndImage = (ImageView) findViewById(R.id.vst_end_image);
        mStartImage = (ImageView) findViewById(R.id.vst_start_image);
        mIndicator = (View) findViewById(R.id.vst_indicator);
        mCenterColor = ContextCompat.getColor(getContext(), R.color.white);
        mSideColor = ContextCompat.getColor(getContext(), R.color.dark_grey);
        mArgbEvaluator = new ArgbEvaluator();
        mIndicatorTranslationX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
        mBottomImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mEndViewsTranslationX = (int) ((mBottomImage.getX() - mStartImage.getX()) - mIndicatorTranslationX);
                mBottomImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mCenterTransationY = getHeight() - mBottomImage.getBottom();
            }
        });
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(position == 0) {
            setColor(1 - positionOffset);
            moveViews(1 - positionOffset);
            mIndicator.setTranslationX((positionOffset - 1) * mIndicatorTranslationX);
            moveAndScaleCenter(1 - positionOffset);

        } else if(position == 1) {
            setColor(positionOffset);
            moveViews(positionOffset);
            mIndicator.setTranslationX(positionOffset * mIndicatorTranslationX);
            moveAndScaleCenter(positionOffset);

        }
    }
    public void setUpWithViewPager(final ViewPager viewPager) {
        viewPager.addOnPageChangeListener(this);
        mStartImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewPager.getCurrentItem() != 0)
                    viewPager.setCurrentItem(0);
            }
        });
        mEndImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewPager.getCurrentItem() != 2)
                    viewPager.setCurrentItem(2);
            }
        });
    }
    @Override
    public void onPageSelected(int position) {
    }
    @Override
    public void onPageScrollStateChanged(int state) {
    }
    private void setColor(float fractionFromCenter) {
        int color = (int) mArgbEvaluator.evaluate(fractionFromCenter, mCenterColor, mSideColor);
        mCenterImage.setColorFilter(color);
        mStartImage.setColorFilter(color);
        mEndImage.setColorFilter(color);
    }

    private void moveViews(float fractionFromCenter) {
        mStartImage.setTranslationX(fractionFromCenter * mEndViewsTranslationX);
        mEndImage.setTranslationX(-fractionFromCenter * mEndViewsTranslationX);
        mIndicator.setAlpha(fractionFromCenter);
        mIndicator.setScaleX(fractionFromCenter);
    }

    private void moveAndScaleCenter(float fractionFromCenter) {
        float scale = .7f + ((1 - fractionFromCenter) * .3f);
        mCenterImage.setScaleX(scale);
        mCenterImage.setScaleY(scale);
        int translation = (int) (fractionFromCenter * mCenterTransationY);
        mCenterImage.setTranslationY(translation);
        mBottomImage.setTranslationY(translation);
        mBottomImage.setAlpha(1 - fractionFromCenter);
    }
}

view_snap_tabs.xml文件如下

view_snap_tabs.xml file like below

<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="184dp"
    tools:layout_gravity="bottom"
    tools:background="@color/light_purple"
    android:id="@+id/snaps">

    <ImageView
        android:id="@+id/vst_center_image"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/large_circle"
        android:layout_marginBottom="96dp"/>

    <ImageView
        android:id="@+id/vst_start_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="56dp"
        android:layout_marginStart="24dp"
        android:layout_gravity="start|bottom"
        android:src="@drawable/ic_chat_bubble_24dp"/>

    <ImageView
        android:id="@+id/vst_end_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="56dp"
        android:layout_marginEnd="24dp"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_group_work_24dp"/>

    <ImageView
        android:id="@+id/vst_bottom_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginBottom="48dp"
        android:layout_gravity="center|bottom"
        android:src="@drawable/small_circle"/>

    <View
        android:id="@+id/vst_indicator"
        android:layout_width="48dp"
        android:layout_height="4dp"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="44dp"
        android:background="@drawable/indicator_background"/>

</com.example.crowderia.chat.view.SnapTabView>

推荐答案

执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<com.example.crowderia.chat.view.SnapTabView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="184dp"
android:id="@+id/snaps"
xmlns:tools="http://schemas.android.com/tools">

<FrameLayout 
android:layout_width="match_parent"
android:layout_height="184dp"
tools:layout_gravity="bottom"
tools:background="@color/light_purple">

<ImageView
    android:id="@+id/vst_bottom_image"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginBottom="48dp"
    android:layout_gravity="center|bottom"
    android:src="@drawable/small_circle"/>

<View
    android:id="@+id/vst_indicator"
    android:layout_width="48dp"
    android:layout_height="4dp"
    android:layout_gravity="bottom|center"
    android:layout_marginBottom="44dp"
    android:background="@drawable/indicator_background"/>

  </FrameLayout>

</com.example.crowderia.chat.view.SnapTabView>

在活动中执行此操作:

View view=findViewById(R.id.am_snap_tabs);
SnapTabView snap=(SnapTabView)view.findViewById(R.id.snaps);
snap.setUpWithViewPager(viewPager);

//am_snap_tabs,它是问题中include的ID

//am_snap_tabs it is the id of the include in the question

这篇关于android.widget.FrameLayout无法转换为com.example.crowderia.chat.view.SnapTabView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆