Use ViewPager instead of custom dragging mechanism

This commit is contained in:
Bartosz Przybylski 2016-02-13 18:29:26 +01:00 committed by AndyScherzinger
parent deaf569c1c
commit bd91cb0c22
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
4 changed files with 125 additions and 119 deletions

View file

@ -16,13 +16,12 @@
android:layout_weight="10"
android:gravity="center"/>
<LinearLayout
<android.support.v4.view.ViewPager
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:orientation="horizontal">
</LinearLayout>
android:layout_weight="80">
</android.support.v4.view.ViewPager>
<LinearLayout
android:orientation="horizontal"

View file

@ -21,6 +21,9 @@
package com.owncloud.android.features;
import android.os.Parcel;
import android.os.Parcelable;
import com.owncloud.android.BuildConfig;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
@ -70,7 +73,7 @@ public class FeatureList {
return features.toArray(new FeatureItem[features.size()]);
}
static public class FeatureItem {
static public class FeatureItem implements Parcelable {
public static final int DO_NOT_SHOW = -1;
private int image;
private int titleText;
@ -104,7 +107,44 @@ public class FeatureList {
public int getVersionNumber() { return versionNumber; }
public int getBetaVersionNumber() { return betaVersion; }
public boolean shouldShowOnFirstRun() { return showOnInitialRun; }
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(image);
dest.writeInt(titleText);
dest.writeInt(contentText);
dest.writeInt(versionNumber);
dest.writeInt(betaVersion);
dest.writeByte((byte) (showOnInitialRun ? 1 : 0));
}
private FeatureItem(Parcel p) {
image = p.readInt();
titleText = p.readInt();
contentText = p.readInt();
versionNumber = p.readInt();
betaVersion = p.readInt();
showOnInitialRun = p.readByte() == 1;
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
@Override
public Object createFromParcel(Parcel source) {
return new FeatureItem(source);
}
@Override
public Object[] newArray(int size) {
return new FeatureItem[size];
}
};
}
static int versionCodeFromString(String version) {
String v[] = version.split(Pattern.quote("."));

View file

@ -21,23 +21,24 @@
package com.owncloud.android.ui.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.owncloud.android.MainApp;
@ -47,32 +48,32 @@ import com.owncloud.android.features.FeatureList;
import com.owncloud.android.features.FeatureList.FeatureItem;
import com.owncloud.android.ui.whatsnew.ProgressIndicator;
/**
* @author Bartosz Przybylski
*/
public class WhatsNewActivity extends Activity {
public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
private static final String KEY_LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
private ImageButton mForwardFinishButton;
private ProgressIndicator mProgress;
private LinearLayout mContentPanel;
private int mCurrentStep;
private ViewPager mPager;
private FeaturesViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whats_new_activity);
mCurrentStep = 0;
mProgress = (ProgressIndicator) findViewById(R.id.progressIndicator);
mPager = (ViewPager)findViewById(R.id.contentPanel);
final boolean isBeta = getResources().getBoolean(R.bool.is_beta);
final int listLength = FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta).length;
mAdapter = new FeaturesViewAdapter(getSupportFragmentManager(), FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta));
mProgress.setNumberOfSteps(mAdapter.getCount());
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(this);
mProgress.setNumberOfSteps(listLength);
mForwardFinishButton = (ImageButton) findViewById(R.id.forward);
mForwardFinishButton.setOnClickListener(new View.OnClickListener() {
@ -80,7 +81,7 @@ public class WhatsNewActivity extends Activity {
public void onClick(View view) {
if (mProgress.hasNextStep()) {
mProgress.animateToNextStep();
mContentPanel.animate().x(-mContentPanel.getChildAt(++mCurrentStep).getLeft());
mPager.setCurrentItem(mPager.getCurrentItem()+1, true);
} else {
onFinish();
finish();
@ -100,54 +101,9 @@ public class WhatsNewActivity extends Activity {
TextView tv = (TextView)findViewById(R.id.welcomeText);
tv.setText(isFirstRun() ? R.string.welcome_to_oc_title : R.string.whats_new_title);
mContentPanel = (LinearLayout)findViewById(R.id.contentPanel);
LinearLayout.LayoutParams ll2 = (LinearLayout.LayoutParams) mContentPanel.getLayoutParams();
ll2.width = getScreenWidth()*listLength;
mContentPanel.setLayoutParams(ll2);
fillContentPanelWithFeatureData();
updateNextButtonIfNeeded();
}
private void fillContentPanelWithFeatureData() {
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
final boolean isBeta = getResources().getBoolean(R.bool.is_beta);
for (FeatureItem item : FeatureList.getFiltered(getLastSeenVersionCode(), isFirstRun(), isBeta)) {
LinearLayout newElement = (LinearLayout)inflater.inflate(R.layout.whats_new_element, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getScreenWidth(), ViewGroup.LayoutParams.MATCH_PARENT);
newElement.setLayoutParams(params);
mContentPanel.addView(newElement);
ImageView iv = (ImageView)newElement.findViewById(R.id.whatsNewImage);
if (item.shouldShowImage())
iv.setImageResource(item.getImage());
TextView tv2 = (TextView)newElement.findViewById(R.id.whatsNewTitle);
if (item.shouldShowTitleText())
tv2.setText(item.getTitleText());
tv2 = (TextView)newElement.findViewById(R.id.whatsNewText);
if (item.shouldShowContentText())
tv2.setText(item.getContentText());
}
mContentPanel.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
@Override
public void onSwipeLeft() {
handleMoveToNext();
}
@Override
public void onSwipeRight() {
handleMoveToPrev();
}
});
}
@Override
public void onBackPressed() {
onFinish();
@ -173,22 +129,6 @@ public class WhatsNewActivity extends Activity {
}
}
private void handleMoveToNext() {
if (mProgress.hasNextStep()) {
mProgress.animateToNextStep();
mContentPanel.animate().x(-mContentPanel.getChildAt(++mCurrentStep).getLeft());
updateNextButtonIfNeeded();
}
}
private void handleMoveToPrev() {
if (mProgress.hasPrevStep()) {
mProgress.animateToPrevStep();
mContentPanel.animate().x(-mContentPanel.getChildAt(--mCurrentStep).getLeft());
updateNextButtonIfNeeded();
}
}
private void onFinish() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
@ -196,12 +136,6 @@ public class WhatsNewActivity extends Activity {
editor.apply();
}
private int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
static private int getLastSeenVersionCode() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
return pref.getInt(KEY_LAST_SEEN_VERSION_CODE, 0);
@ -224,44 +158,76 @@ public class WhatsNewActivity extends Activity {
context.startActivity(new Intent(context, WhatsNewActivity.class));
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public abstract class OnSwipeTouchListener implements View.OnTouchListener {
@Override
public void onPageSelected(int position) {
mProgress.animateToStep(position+1);
updateNextButtonIfNeeded();
}
private final GestureDetector gestureDetector;
@Override
public void onPageScrollStateChanged(int state) {
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
private final class FeaturesViewAdapter extends FragmentPagerAdapter {
FeatureItem[] mFeatures;
public FeaturesViewAdapter(FragmentManager fm, FeatureItem[]features) {
super(fm);
mFeatures = features;
}
abstract public void onSwipeLeft();
abstract public void onSwipeRight();
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
@Override
public Fragment getItem(int position) {
return FeatureFragment.newInstance(mFeatures[position]);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public int getCount() {
return mFeatures.length;
}
}
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
public static class FeatureFragment extends Fragment {
private FeatureItem mItem;
@Override
public boolean onDown(MotionEvent e) { return true; }
static public FeatureFragment newInstance(FeatureItem item) {
FeatureFragment f = new FeatureFragment();
Bundle args = new Bundle();
args.putParcelable("feature", item);
f.setArguments(args);
return f;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final float distanceX = e2.getX() - e1.getX();
final float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mItem = getArguments() != null ? (FeatureItem)getArguments().getParcelable("feature") : null;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.whats_new_element, container, false);
ImageView iv = (ImageView)v.findViewById(R.id.whatsNewImage);
if (mItem.shouldShowImage())
iv.setImageResource(mItem.getImage());
TextView tv2 = (TextView)v.findViewById(R.id.whatsNewTitle);
if (mItem.shouldShowTitleText())
tv2.setText(mItem.getTitleText());
tv2 = (TextView)v.findViewById(R.id.whatsNewText);
if (mItem.shouldShowContentText())
tv2.setText(mItem.getContentText());
return v;
}
}

View file

@ -74,11 +74,11 @@ public class ProgressIndicator extends FrameLayout {
}
public void animateToNextStep() {
animateToStep(++mCurrentStep);
animateToStep(mCurrentStep+1);
}
public void animateToPrevStep() {
animateToStep(--mCurrentStep);
animateToStep(mCurrentStep-1);
}
public void setNumberOfSteps(int steps) {
@ -101,8 +101,9 @@ public class ProgressIndicator extends FrameLayout {
mCurrentProgressDot.setLayoutParams(lp);
}
private void animateToStep(int step) {
public void animateToStep(int step) {
if (step < 1 || step > mNumberOfSteps) return;
mCurrentStep = step;
View dot = mDotsContainer.getChildAt(step-1);
mCurrentProgressDot
.animate()