如何在Activity中实现QuickReturnList?

时间:2014-02-13 12:44:41

标签: android android-listview android-fragments

如何使用每个标签的活动来实施标签。我找到了this tutorial,但它是用Fragments实现的。 Here是一段视频,可以准确显示我想要做的事情。

这是我一直想做的事情:

DefaultActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater  = (LayoutInflater) DefaultActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.fragment, null);
        mHeader = inflater.inflate(R.layout.header, null);
        mQuickReturnView = (TextView) view.findViewById(R.id.sticky);
        mPlaceHolder = mHeader.findViewById(R.id.placeholder);



        mListView = (QuickReturnListView) getListView(); //showing class cast exception here

        mQuickReturnView.setText("Default");
        mListView.addHeaderView(mHeader);

        String[] array = new String[] { "Android", "Android", "Android",
                "Android", "Android", "Android", "Android", "Android",
                "Android", "Android", "Android", "Android", "Android",
                "Android", "Android", "Android" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,array);


        mListView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mQuickReturnHeight = mQuickReturnView.getHeight();
                        mListView.computeScrollY();
                        mCachedVerticalScrollRange = mListView.getListHeight();
                    }
                });

        mListView.setOnScrollListener(new OnScrollListener() {
            @SuppressLint("NewApi")
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

                mScrollY = 0;
                int translationY = 0;

                if (mListView.scrollYIsComputed()) {
                    mScrollY = mListView.getComputedScrollY();
                }

                int rawY = mPlaceHolder.getTop()
                        - Math.min(
                                mCachedVerticalScrollRange
                                        - mListView.getHeight(), mScrollY);

                switch (mState) {
                case STATE_OFFSCREEN:
                    if (rawY <= mMinRawY) {
                        mMinRawY = rawY;
                    } else {
                        mState = STATE_RETURNING;
                    }
                    translationY = rawY;
                    break;

                case STATE_ONSCREEN:
                    if (rawY < -mQuickReturnHeight) {
                        mState = STATE_OFFSCREEN;
                        mMinRawY = rawY;
                    }
                    translationY = rawY;
                    break;

                case STATE_RETURNING:
                    translationY = (rawY - mMinRawY) - mQuickReturnHeight;
                    if (translationY > 0) {
                        translationY = 0;
                        mMinRawY = rawY - mQuickReturnHeight;
                    }

                    if (rawY > 0) {
                        mState = STATE_ONSCREEN;
                        translationY = rawY;
                    }

                    if (translationY < -mQuickReturnHeight) {
                        mState = STATE_OFFSCREEN;
                        mMinRawY = rawY;
                    }
                    break;
                }

                /** this can be used if the build is below honeycomb **/
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
                    anim = new TranslateAnimation(0, 0, translationY,
                            translationY);
                    anim.setFillAfter(true);
                    anim.setDuration(0);
                    mQuickReturnView.startAnimation(anim);
                } else {
                    mQuickReturnView.setTranslationY(translationY);
                }

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }
        });
        rlMain = (RelativeLayout)findViewById(R.id.main);
        rlMain.addView(view);
        setContentView(R.layout.activity_main);

    }

现在它在mListView = (QuickReturnListView) getListView();

中显示错误
02-13 21:23:03.389: E/AndroidRuntime(26896): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quickreturnlistwithactivity/com.example.quickreturnlistwithactivity.QuickReturnActivity}: java.lang.ClassCastException: android.widget.ListView cannot be cast to com.example.quickreturnlistwithactivity.QuickReturnListView
02-13 21:23:03.389: E/AndroidRuntime(26896): Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to com.example.quickreturnlistwithactivity.QuickReturnListView
02-13 21:23:03.389: E/AndroidRuntime(26896):    at com.example.quickreturnlistwithactivity.QuickReturnActivity.onCreate(QuickReturnActivity.java:67)

2 个答案:

答案 0 :(得分:0)

只需将每个标签视为具有相同视图的单独应用程序即可。唯一的区别是选择了女巫标签。

你唯一的缺点是表现,因为活动比片段重得多。 此外,如果您想在活动之间共享数据,那将比使用片段更难。

另外,您可能会发现this教程很有趣。它显示了活动过渡的小例子。

答案 1 :(得分:0)

你确定你的标记看起来像是:

<your_package_path.QuickReturnListView