片段中的CustomList?

时间:2015-01-31 14:43:17

标签: java android android-fragments android-actionbar custom-lists

当我运行我的应用程序时,只是出现此错误:

  

错误:(123,45)java:不兼容的类型:   com.special.ResideMenuDemo.NoBoringFeragment无法转换为   android.app.Activity

,错误行为:

CustomList adapter = new CustomList(this, name, family, metrics);

我只是不明白,你知道我只是将我的类从活动更改为片段并从片段扩展,如果这可以帮助你。 并且当我想在我的应用程序的这一行中获取actionBarSize时,我的IDE向我显示此错误:

  

错误:(223,9)java:找不到符号         符号:方法getTheme()         location:class com.special.ResideMenuDemo.NoBoringFeragment   你可以帮我搞清楚吗?

编辑: 我只是补充一下,你的来源,也许是明确的事情:

我的java代码:

package com.special.ResideMenuDemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.TypefaceSpan;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.flavienlaurent.notboringactionbar.*;

import com.flavienlaurent.notboringactionbar.NoBoringActionBarActivity;
import com.flavienlaurent.notboringactionbar.AlphaForegroundColorSpan;
import com.flavienlaurent.notboringactionbar.KenBurnsView;
import com.flavienlaurent.notboringactionbar.ParallaxImageView;

/**
 * Created by milad_000 on 31/01/2015.
 */
public class NoBoringFeragment extends Fragment {
    ////////////////////////////////-----------------------------------------------------------------------------------------------------------------------------------

    private DisplayMetrics metrics;
    String[] name = {
            " کرمی فرماندار شهرستان سرخه با اشاره به تهاجم فرهنگی دشمنان اسلام و تلاش برای جداسازی جوانان از آرمانهای انقلاب افزود: بایدسازو کاری دیده شود تا اجلاسیه شهدای امسال به سمت جوان محوری هدایت شود ،چراکه بهترین کار برای پاسداشت ارزشهای انقلاب و دفاع مقدس در زمان",
            " حمیدرضا",
            " عباسعلی"
    };
    String[] family = {
            " ",
            " اسفنجاني",
            " الله دادی"
    };


    private static final String TAG = "NoBoringActionBarActivity";
    private int mActionBarTitleColor;
    private int mActionBarHeight;
    private int mHeaderHeight;
    private int mMinHeaderTranslation;
    private ListView mListView;
    private KenBurnsView mHeaderPicture;
    private ImageView mHeaderLogo;
    private View mHeader;
    private View mPlaceHolderView;
    private AccelerateDecelerateInterpolator mSmoothInterpolator;

    private RectF mRect1 = new RectF();
    private RectF mRect2 = new RectF();

    private AlphaForegroundColorSpan mAlphaForegroundColorSpan;
    private SpannableString mSpannableString;

    private TypedValue mTypedValue = new TypedValue();
    ////////////////////////////////*********************************************************************************

    private View parentView;

    ////////////////////////////////*********************************************************************************

    ////////////////////////////////-----------------------------------------------------------------------------------------------------------------------------------




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        parentView= inflater.inflate(com.flavienlaurent.notboringactionbar.R.layout.activity_noboringactionbar, container, false);
        mSmoothInterpolator = new AccelerateDecelerateInterpolator();
        mHeaderHeight = getResources().getDimensionPixelSize(com.flavienlaurent.notboringactionbar.R.dimen.header_height);
        mMinHeaderTranslation = -mHeaderHeight + getActionBarHeight();

      //  setContentView(com.flavienlaurent.notboringactionbar.R.layout.activity_noboringactionbar);

        mListView = (ListView) parentView.findViewById(com.flavienlaurent.notboringactionbar.R.id.listview);
        mHeader = parentView.findViewById(com.flavienlaurent.notboringactionbar.R.id.header);
        mHeaderPicture = (KenBurnsView) getView().findViewById(com.flavienlaurent.notboringactionbar.R.id.header_picture);
        mHeaderPicture.setResourceIds(com.flavienlaurent.notboringactionbar.R.drawable.picture0, com.flavienlaurent.notboringactionbar.R.drawable.picture1);
        mHeaderLogo = (ImageView) parentView.findViewById(com.flavienlaurent.notboringactionbar.R.id.header_logo);

        mActionBarTitleColor = getResources().getColor(com.flavienlaurent.notboringactionbar.R.color.actionbar_title_color);

        mSpannableString = new SpannableString(getString(com.flavienlaurent.notboringactionbar.R.string.noboringactionbar_title));
        mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(mActionBarTitleColor);

        setupActionBar();
   //     setupListView();

        return parentView;



    }

    private void setupListView() {

        metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        CustomList adapter = new CustomList(this, name, family, metrics);
        mListView.setDivider(null);
        mListView.setDividerHeight(0);
        mPlaceHolderView = getActivity().getLayoutInflater().inflate(com.flavienlaurent.notboringactionbar.R.layout.view_header_placeholder, mListView, false);
        mListView.addHeaderView(mPlaceHolderView);
        mListView.setAdapter(adapter);





        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int scrollY = getScrollY();
                //sticky actionbar
                mHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation));
                //header_logo --> actionbar icon
                float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f);
                interpolate(mHeaderLogo, getActionBarIconView(), mSmoothInterpolator.getInterpolation(ratio));
                //actionbar title alpha
                //getActionBarTitleView().setAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
                //---------------------------------
                //better way thanks to @cyrilmottier
                setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
            }
        });
    }

    private void setTitleAlpha(float alpha) {
        mAlphaForegroundColorSpan.setAlpha(alpha);
        mSpannableString.setSpan(mAlphaForegroundColorSpan, 0, mSpannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        getActivity().getActionBar().setTitle(mSpannableString);
    }

    public static float clamp(float value, float min, float max) {
        return Math.max(min,Math.min(value, max));
    }

    private void interpolate(View view1, View view2, float interpolation) {
        getOnScreenRect(mRect1, view1);
        getOnScreenRect(mRect2, view2);

        float scaleX = 1.0F + interpolation * (mRect2.width() / mRect1.width() - 1.0F);
        float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F);
        float translationX = 0.5F * (interpolation * (mRect2.left + mRect2.right - mRect1.left - mRect1.right));
        float translationY = 0.5F * (interpolation * (mRect2.top + mRect2.bottom - mRect1.top - mRect1.bottom));

        view1.setTranslationX(translationX);
        view1.setTranslationY(translationY - mHeader.getTranslationY());
        view1.setScaleX(scaleX);
        view1.setScaleY(scaleY);
    }

    private RectF getOnScreenRect(RectF rect, View view) {
        rect.set(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        return rect;
    }

    public int getScrollY() {
        View c = mListView.getChildAt(0);
        if (c == null) {
            return 0;
        }

        int firstVisiblePosition = mListView.getFirstVisiblePosition();
        int top = c.getTop();

        int headerHeight = 0;
        if (firstVisiblePosition >= 1) {
            headerHeight = mPlaceHolderView.getHeight();
        }

        return -top + firstVisiblePosition * c.getHeight() + headerHeight;
    }

    private void setupActionBar() {
        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setIcon(com.flavienlaurent.notboringactionbar.R.drawable.ic_transparent);

        //getActionBarTitleView().setAlpha(0f);
    }

    private ImageView getActionBarIconView() {
        return (ImageView) parentView.findViewById(android.R.id.home);
    }

    /*private TextView getActionBarTitleView() {
        int id = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        return (TextView) findViewById(id);
    }*/

    public int getActionBarHeight() {
        if (mActionBarHeight != 0) {
            return mActionBarHeight;
        }
        getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
        mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics());
        return mActionBarHeight;
    }

    public class CustomList extends ArrayAdapter<String> {
        private DisplayMetrics metrics_;
        private final Activity context;
        private final String[] name;
        private final String[] family;

        public CustomList(Activity context,String[] name,String[] family , DisplayMetrics metrics ) {
            super(context, com.flavienlaurent.notboringactionbar.R.layout.viewlisting, name);
            this.context = context;
            this.name = name;
            this.family = family;
            this.metrics_ = metrics;
        }
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View rowView= inflater.inflate(com.flavienlaurent.notboringactionbar.R.layout.viewlisting, null, true);
            TextView nameTxt = (TextView) rowView.findViewById(com.flavienlaurent.notboringactionbar.R.id.name);
            TextView familyTxt = (TextView) rowView.findViewById(com.flavienlaurent.notboringactionbar.R.id.family);

            nameTxt.setText(name[position]);
            familyTxt.setText(family[position]);

            Animation animation = null;
            animation = AnimationUtils.loadAnimation(context, com.flavienlaurent.notboringactionbar.R.anim.push_left_in);
            rowView.startAnimation(animation);

            return rowView;
        }

    }

}

我的xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoBoringActionBarActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" />

    <FrameLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height">

        <com.flavienlaurent.notboringactionbar.KenBurnsView
            android:id="@+id/header_picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/picture0" />

        <ImageView
            android:id="@+id/header_logo"
            android:layout_width="@dimen/header_logo_size"
            android:layout_height="@dimen/header_logo_size"
            android:layout_gravity="center"
            android:src="@drawable/ic_header_logo" />

    </FrameLayout>

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

因为适配器的构造函数是

public CustomList(Activity context,String[] name,String[] family , DisplayMetrics metrics)

你应该替换

CustomList adapter = new CustomList(this, name, family, metrics);

用这个

CustomList adapter = new CustomList(getActivity().getApplicationContext(), name, family, metrics);

原因是因为this中的CustomList adapter = new CustomList(this, name, family, metrics);引用了Fragment,但正确的是Activity

对于第二个错误,您应该尝试替换

getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);

getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);

<强>更新

您的自定义适配器扩展了ArrayAdapter类,因此我建议您更改

public CustomList(Activity context,String[] name,String[] family , DisplayMetrics metrics)

public CustomList(Context context,String[] name,String[] family , DisplayMetrics metrics)