我所使用的当前解决方案(或多或少)是使用嵌套在PagerTabStrip标题中的TextView作为拖动区域.这种方法中最大的问题是,由于某种原因,这些切换区域太小. ENTER事件仅在这些区域触发.
我相信这个解决方案对于oldschool选项卡会很好.因此这里是代码.可能对某人有用.
1).从PagerTabStrip解析TextViews
private ArrayList titles;
...
public void setPager(PagerTabStrip mPagerTabStrip) {
for (int i = 0; i < mPagerTabStrip.getChildCount(); ++i) {
View nextChild = mPagerTabStrip.getChildAt(i);
if (nextChild instanceof TextView) {
TextView textViewToConvert = (TextView) nextChild;
if (textViewToConvert.getText().toString().contains(TabsFragment.TAG_MONITOR)){ // I take only few tabs
if (!titles.contains(textViewToConvert)) {
titles.add(textViewToConvert);
}
}
}
}
}
public ArrayList getTitles() {
return titles;
}
在这个操作之后,我有3个需要拖放导航的TextView.
2).将TextView列表传递给实现DragListeners的类:
public void setUpDragNDropTabMoves(ArrayList views) {
if (views != null) {
for (TextView tv : views){
tv.setOnDragListener(TabToggleDragListener);
}
}
}
3).而听众本身
View.OnDragListener TabToggleDragListener = new View.OnDragListener() {
@Override
public boolean onDrag(View v,DragEvent event) {
TextView toggleView = (TextView) v;
String title = toggleView.getText().toString();
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
...
break;
case DragEvent.ACTION_DRAG_ENTERED:
int tabId = -1;
//define which tab is destination
if (title.contains(TabsFragment.TAG_MONITOR1)){
tabId = 1;
} else if (title.contains(TabsFragment.TAG_MONITOR2)){
tabId = 2;
} else if (title.contains(TabsFragment.TAG_MONITOR3)){
tabId = 3;
}
//send event to tab host fragment/activity and perform tab change to passed index
//you may use EventBus or Otto for this. I used Local Broadcast
BroadcastSender.sendMessageToUI(getContext(),BroadcastSender.TABS_FRAGMENT,BroadcastActions.TF_TOGGLE_TAB,tabId
);
break;
case DragEvent.ACTION_DRAG_EXITED:
...
break;
case DragEvent.ACTION_DRAG_ENDED:
...
break;
}
return true;
}
};