拖放操作栏标签(Android 4.0 ICS)?

时间:2012-05-25 14:11:15

标签: android tabs drag-and-drop android-actionbar

是否有允许用户拖放ActionBar中的导航标签以在Android 4.0 ICS上重新排序?我并不是指已弃用的TabHost中的选项卡,我指的是您添加到Honeycomb及以上版本中使用的ActionBar的选项卡。

谢谢!

1 个答案:

答案 0 :(得分:2)

就使用ActionBar.Tabs而言,它们根本没有实现此功能的功能。另一方面,创建自己的模仿Tabs的自定义类很容易,那么您只需为{{创建OnDragListenerOnTouchListener即可。 1}}用于填充标签栏。

例如,这是我在我的一个模仿View的应用中使用的类。

<强> ScrollableTabView

ActionBar.Tabs

<强> TabAdapter

    public class ScrollableTabView extends HorizontalScrollView implements OnPageChangeListener {

    private final Context mContext;

    private final LinearLayout mContainer;

    private final ArrayList<View> mTabs = new ArrayList<View>();

    private final int mDividerColor = 0xFF636363;

    private int mDividerMarginTop = 12;

    private int mDividerMarginBottom = 12;

    private int mDividerWidth = 1;

    private ViewPager mPager;

    private TabAdapter mAdapter;

    private Drawable mDividerDrawable;

    public ScrollableTabView(Context context) {
        this(context, null);
    }

    public ScrollableTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollableTabView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);

        mContext = context;

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);

        mDividerMarginTop = (int)(getResources().getDisplayMetrics().density * mDividerMarginTop);
        mDividerMarginBottom = (int)(getResources().getDisplayMetrics().density * mDividerMarginBottom);
        mDividerWidth = (int)(getResources().getDisplayMetrics().density * mDividerWidth);

        setHorizontalScrollBarEnabled(false);
        setHorizontalFadingEdgeEnabled(false);

        mContainer = new LinearLayout(context);
        mContainer.setOrientation(LinearLayout.HORIZONTAL);
        mContainer.setLayoutParams(params);

        addView(mContainer);
    }

    /**
     * Set the tabs Adapter
     * 
     * @param adapter
     */
    public void setAdapter(TabAdapter adapter) {
        mAdapter = adapter;

        if (mPager != null && mAdapter != null) {
            initTabs();
        }
    }

    /**
     * Attach ViewPager
     * 
     * @param pager
     */
    public void setViewPager(ViewPager pager) {
        mPager = pager;
        mPager.setOnPageChangeListener(this);

        if (mPager != null && mAdapter != null) {
            initTabs();
        }
    }

    /**
     * Initiate the tabs
     */
    private void initTabs() {

        mContainer.removeAllViews();
        mTabs.clear();

        if (mAdapter == null) {
            return;
        }

        for (int i = 0; i < mPager.getAdapter().getCount(); i++) {

            final int index = i;

            final View tab = mAdapter.getView(i);
            mContainer.addView(tab);

            tab.setFocusable(true);

            mTabs.add(tab);

            if (i != mPager.getAdapter().getCount() - 1) {
                mContainer.addView(getSeparator());
            }

            tab.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mPager.getCurrentItem() == index) {
                        selectTab(index);
                    } else {
                        mPager.setCurrentItem(index, true);
                    }
                }
            });

        }

        selectTab(mPager.getCurrentItem());
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // Nothing to do
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // Nothing to do
    }

    @Override
    public void onPageSelected(int position) {
        selectTab(position);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if (changed) {
            selectTab(mPager.getCurrentItem());
        }
    }

    /**
     * @return Separates the tabs
     */
    private View getSeparator() {
        final View v = new View(mContext);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mDividerWidth,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(0, mDividerMarginTop, 0, mDividerMarginBottom);
        v.setLayoutParams(params);

        if (mDividerDrawable != null) {
            v.setBackground(mDividerDrawable);
        } else {
            v.setBackgroundColor(mDividerColor);
        }

        return v;
    }

    /**
     * @param position
     */
    private void selectTab(int position) {

        for (int i = 0, pos = 0; i < mContainer.getChildCount(); i += 2, pos++) {
            final View tab = mContainer.getChildAt(i);
            tab.setSelected(pos == position);
        }

        final View selectedTab = mContainer.getChildAt(position * 2);

        final int w = selectedTab.getMeasuredWidth();
        final int l = selectedTab.getLeft();

        final int x = l - this.getWidth() / 2 + w / 2;

        smoothScrollTo(x, this.getScrollY());
    }
}

附加TabAdapter

    public interface TabAdapter {
    public View getView(int position);
}

您可以使用真实 public class ScrollingTabsAdapter implements TabAdapter { private final FragmentActivity activity; private final LayoutInflater inflater; private Button mTabs; // Tab titles private static final String[] mTitles = { "RECENT", "ARTISTS", "ALBUMS", "SONGS", "PLAYLISTS", "GENRES" }; /** * @param act */ public ScrollingTabsAdapter(FragmentActivity act) { activity = act; inflater = activity.getLayoutInflater(); } @Override public View getView(int position) { mTabs = (Button)inflater.inflate(R.layout.tabs, null); if (position < mTitles.length) { mTabs.setText(mTitles[position]); } return mTabs; } } 的默认drawable和属性来设置您膨胀的ActionBar.Tabs样式。您可以从SDK或Web上的某个位置获取它们。要使用它,请将Button对象附加到ViewPager,然后将ScrollableTabView中的每个Fragments添加到FragmentPagerAdapterThis is what they look like, if you're curious about the style after adding the default drawables and attributes

就拖拽而言,Android在他们的网站上有一些不错的文档。 Drag and Drop

网上还有一些易于学习的教程。 Android Drag and Drop Tutorial, via Lars Vogel

Or you can always simply use Google to find more