问题
How to implement tabs, using activity for each tab. I found this tutorial but it implemented with Fragments. Here is a video that show exactly what I want to do.
Here what I been trying to do:
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);
}
Now it showing error in 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)
回答1:
Just think about each tab as separate application, that have the same view. The only difference is witch tab is selected.
Your only disadvantage will be performance, as activity is much heavier process than fragment. Also if you would like to share data between activities it will be harder than using fragments.
Also you may find this tutorial interesting. It show small example of activities transitions.
回答2:
Are you sure that you have the markup looks like:
<your_package_path.QuickReturnListView
来源:https://stackoverflow.com/questions/21754635/how-to-implement-quickreturnlist-in-an-activity