gpt4 book ai didi

android - 拖放操作栏选项卡(Android 4.0 ICS)?

转载 作者:行者123 更新时间:2023-11-29 00:38:58 25 4
gpt4 key购买 nike

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

谢谢!

最佳答案

至于使用ActionBar.Tabs去,他们根本没有实现这一目标的功能。另一方面,创建您自己的模拟 Tabs 的自定义类非常简单,然后您所要做的就是创建并添加一个 OnDragListener用于填充标签栏的 View 的 OnTouchListener

例如,这是我在我的一个模仿 ActionBar.Tabs 的应用程序中使用的类。

可滚动标签 View

    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);
}

附加您的 TabAdapter

    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;
}
}

您可以使用默认的可绘制对象和真实 ActionBar.Tabs 的属性来设置您膨胀的 Button 的样式。您可以从 SDK 或网络上的某个地方获取它们。要使用它,请将 ViewPager 对象附加到 ScrollableTabView 并将每个 Fragments 添加到 FragmentPagerAdapter 中。 This 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

关于android - 拖放操作栏选项卡(Android 4.0 ICS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10755934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com