Skip to content

Commit 47fd523

Browse files
author
wujiawei
committedJun 21, 2016
add TextCard
- TextCardModel - TextCardStackAdapter - and TextCardActivity as test
1 parent 943de4e commit 47fd523

File tree

12 files changed

+224
-38
lines changed

12 files changed

+224
-38
lines changed
 

‎AndTinder/src/main/java/com/andtinder/model/CardModel.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class CardModel {
3434

3535
private OnClickListener mOnClickListener = null;
3636

37+
private OnSwipeListener mOnSwipeListener = null;
38+
3739
public interface OnCardDismissedListener {
3840
void onDismiss();
3941
void onLike();
@@ -44,6 +46,10 @@ public interface OnClickListener {
4446
void OnClickListener();
4547
}
4648

49+
public interface OnSwipeListener {
50+
void onSwipe(float dx);
51+
}
52+
4753
public CardModel() {
4854
this(null, null, (Drawable)null);
4955
}
@@ -108,12 +114,19 @@ public OnCardDismissedListener getOnCardDismissedListener() {
108114
return this.mOnCardDismissedListener;
109115
}
110116

111-
112117
public void setOnClickListener( OnClickListener listener ) {
113118
this.mOnClickListener = listener;
114119
}
115120

116121
public OnClickListener getOnClickListener() {
117122
return this.mOnClickListener;
118123
}
124+
125+
public OnSwipeListener getOnSwipeListener() {
126+
return mOnSwipeListener;
127+
}
128+
129+
public void setOnSwipeListener(OnSwipeListener mOnSwipeListener) {
130+
this.mOnSwipeListener = mOnSwipeListener;
131+
}
119132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.andtinder.model;
2+
3+
/**
4+
* Created by einverne on 16/6/21.
5+
*/
6+
7+
public class TextCardModel extends CardModel {
8+
9+
private String textShow;
10+
11+
public TextCardModel(String textShow) {
12+
this.textShow = textShow;
13+
}
14+
15+
public String getTextShow() {
16+
return textShow;
17+
}
18+
19+
public void setTextShow(String textShow) {
20+
this.textShow = textShow;
21+
}
22+
23+
}

‎AndTinder/src/main/java/com/andtinder/view/CardContainer.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void onInvalidated() {
6464
private ListAdapter mListAdapter;
6565
private float mLastTouchX;
6666
private float mLastTouchY;
67+
private float mFirstTouchX; // recode the x when touched
6768
private View mTopCard; // first card on cards stack
6869
private int mTouchSlop;
6970
private int mGravity;
@@ -255,7 +256,7 @@ public boolean onTouchEvent(MotionEvent event) {
255256
if (mGestureDetector.onTouchEvent(event)) {
256257
return true;
257258
}
258-
Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " ");
259+
// Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " ");
259260
final int pointerIndex;
260261
final float x, y;
261262
final float dx, dy;
@@ -298,6 +299,13 @@ public boolean onTouchEvent(MotionEvent event) {
298299
if(!mDragging) {
299300
return true;
300301
}
302+
float deltaX = event.getX() - mFirstTouchX;
303+
304+
CardModel cardModel = (CardModel)getAdapter().getItem(0);
305+
if (cardModel.getOnSwipeListener() != null) {
306+
cardModel.getOnSwipeListener().onSwipe(deltaX);
307+
}
308+
301309

302310
mTopCard.setTranslationX(mTopCard.getTranslationX() + dx);
303311
// mTopCard.setTranslationY(mTopCard.getTranslationY() + dy);
@@ -341,6 +349,11 @@ public boolean onTouchEvent(MotionEvent event) {
341349
return true;
342350
}
343351

352+
/**
353+
* 事件拦截
354+
* @param event event
355+
* @return
356+
*/
344357
@Override
345358
public boolean onInterceptTouchEvent(MotionEvent event) {
346359
if (mTopCard == null) {
@@ -352,11 +365,12 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
352365
final int pointerIndex;
353366
final float x, y;
354367
final float dx, dy;
368+
CardModel cardModel;
355369
switch (event.getActionMasked()) {
356370
case MotionEvent.ACTION_DOWN:
357371
mTopCard.getHitRect(childRect);
358372

359-
CardModel cardModel = (CardModel)getAdapter().getItem(0);
373+
cardModel = (CardModel)getAdapter().getItem(0);
360374

361375
if (cardModel.getOnClickListener() != null) {
362376
cardModel.getOnClickListener().OnClickListener();
@@ -369,6 +383,7 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
369383
return false;
370384
}
371385

386+
mFirstTouchX = x;
372387
mLastTouchX = x;
373388
mLastTouchY = y;
374389
mActivePointerId = event.getPointerId(pointerIndex);
@@ -515,8 +530,8 @@ public void run() {
515530
.alpha(.75f)
516531
.setInterpolator(new LinearInterpolator())
517532
.x(targetX)
518-
.y(targetY)
519-
.rotation(Math.copySign(45, velocityX))
533+
// .y(targetY)
534+
.rotation(Math.copySign(20, velocityX))
520535
.setListener(new AnimatorListenerAdapter() {
521536
@Override
522537
public void onAnimationEnd(Animator animation) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.andtinder.view;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.TextView;
8+
9+
import com.andtinder.R;
10+
import com.andtinder.model.CardModel;
11+
import com.andtinder.model.TextCardModel;
12+
13+
/**
14+
* Created by einverne on 16/6/21.
15+
*/
16+
17+
public class TextCardStackAdapter extends CardStackAdapter {
18+
public TextCardStackAdapter(Context context) {
19+
super(context);
20+
}
21+
22+
@Override
23+
protected View getCardView(int position, CardModel model, View convertView, ViewGroup parent) {
24+
if (convertView == null) {
25+
LayoutInflater inflater = LayoutInflater.from(getContext());
26+
convertView = inflater.inflate(R.layout.text_card_inner, parent, false);
27+
28+
assert convertView != null;
29+
}
30+
31+
((TextView) convertView.findViewById(R.id.tv_main)).setText(((TextCardModel)model).getTextShow());
32+
33+
return convertView;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="400dp"
4+
android:layout_height="400dp"
5+
android:background="@android:color/darker_gray"
6+
android:gravity="center_vertical|center_horizontal|center">
7+
8+
<TextView
9+
android:id="@+id/tv_main"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_alignParentLeft="true"
13+
android:layout_alignParentStart="true"
14+
android:layout_alignParentTop="true"
15+
android:textColor="@android:color/holo_green_dark"
16+
android:textSize="50sp"
17+
android:text="demo" />
18+
</RelativeLayout>
+15-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
23
package="com.andtinder.demo">
34

4-
<uses-sdk
5-
android:minSdkVersion="13"
6-
/>
7-
<application android:allowBackup="true"
8-
android:label="@string/app_name"
5+
<uses-sdk android:minSdkVersion="13" />
6+
7+
<android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
8+
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
9+
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
10+
11+
<application
12+
android:allowBackup="true"
913
android:icon="@drawable/ic_launcher"
14+
android:label="@string/app_name"
1015
android:theme="@style/AppTheme">
1116
<activity
12-
android:name="com.andtinder.demo.MainActivity"
13-
android:label="@string/app_name" >
17+
android:name=".MainActivity"
18+
android:label="@string/app_name">
1419
<intent-filter>
1520
<action android:name="android.intent.action.MAIN" />
1621

1722
<category android:name="android.intent.category.LAUNCHER" />
1823
</intent-filter>
1924
</activity>
25+
<activity android:name=".TextCardActivity"></activity>
2026
</application>
21-
</manifest>
27+
28+
</manifest>

‎AndTinderDemo/src/main/java/com/andtinder/demo/MainActivity.java

+34-25
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121
import android.content.Intent;
2222
import android.content.res.Resources;
2323
import android.os.Bundle;
24+
import android.support.v7.app.AppCompatActivity;
2425
import android.util.Log;
26+
import android.view.Menu;
27+
import android.view.MenuInflater;
28+
import android.view.MenuItem;
2529
import android.view.View;
2630
import android.view.Window;
2731
import android.widget.Button;
2832

2933
import com.andtinder.model.CardModel;
3034
import com.andtinder.model.Orientations;
35+
import com.andtinder.model.TextCardModel;
3136
import com.andtinder.view.CardContainer;
3237
import com.andtinder.view.SimpleCardStackAdapter;
3338

34-
public class MainActivity extends Activity implements View.OnClickListener{
39+
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
3540

3641
/**
3742
* This variable is the container that will host our cards
@@ -44,7 +49,7 @@ public class MainActivity extends Activity implements View.OnClickListener{
4449
@Override
4550
public void onCreate(Bundle savedInstanceState) {
4651
super.onCreate(savedInstanceState);
47-
requestWindowFeature(Window.FEATURE_NO_TITLE);
52+
// requestWindowFeature(Window.FEATURE_NO_TITLE);
4853
setContentView(R.layout.mainlayout);
4954

5055
mCardContainer = (CardContainer) findViewById(R.id.layoutview);
@@ -63,29 +68,6 @@ public void onCreate(Bundle savedInstanceState) {
6368
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
6469
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
6570
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
66-
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
67-
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
68-
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
69-
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
70-
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
71-
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
72-
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
73-
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
74-
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
75-
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
76-
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
77-
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
78-
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
79-
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
80-
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
81-
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
82-
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
83-
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
84-
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
85-
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
86-
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
87-
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
88-
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
8971

9072
CardModel cardModel = new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1));
9173
cardModel.setOnClickListener(new CardModel.OnClickListener() {
@@ -112,11 +94,38 @@ public void onDislike() {
11294
}
11395
});
11496

97+
cardModel.setOnSwipeListener(new CardModel.OnSwipeListener() {
98+
@Override
99+
public void onSwipe(float dx) {
100+
Log.i("Swipe ", "dx : " + dx);
101+
}
102+
});
103+
115104
adapter.add(cardModel);
116105

117106
mCardContainer.setAdapter(adapter);
118107
}
119108

109+
@Override
110+
public boolean onCreateOptionsMenu(Menu menu) {
111+
MenuInflater inflater = getMenuInflater();
112+
inflater.inflate(R.menu.main_menu, menu);
113+
return true;
114+
}
115+
116+
@Override
117+
public boolean onOptionsItemSelected(MenuItem item) {
118+
switch (item.getItemId()) {
119+
case R.id.menu_text_card:
120+
Intent intent = new Intent(this, TextCardActivity.class);
121+
startActivity(intent);
122+
123+
return true;
124+
default:
125+
return super.onOptionsItemSelected(item);
126+
}
127+
}
128+
120129
@Override
121130
public void onClick(View v) {
122131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.andtinder.demo;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
6+
import com.andtinder.model.TextCardModel;
7+
import com.andtinder.view.CardContainer;
8+
import com.andtinder.view.TextCardStackAdapter;
9+
10+
public class TextCardActivity extends AppCompatActivity {
11+
12+
private CardContainer mTextCardContainer;
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_text_card);
18+
19+
mTextCardContainer = (CardContainer) findViewById(R.id.textCardContainer);
20+
21+
TextCardStackAdapter textCardStackAdapter = new TextCardStackAdapter(this);
22+
textCardStackAdapter.add(new TextCardModel("text"));
23+
textCardStackAdapter.add(new TextCardModel("text2"));
24+
textCardStackAdapter.add(new TextCardModel("text3"));
25+
textCardStackAdapter.add(new TextCardModel("text4"));
26+
27+
mTextCardContainer.setAdapter(textCardStackAdapter);
28+
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/activity_text_card"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.andtinder.demo.TextCardActivity">
9+
10+
<com.andtinder.view.CardContainer
11+
android:id="@+id/textCardContainer"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:gravity="center">
15+
16+
</com.andtinder.view.CardContainer>
17+
18+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<item
5+
android:id="@+id/menu_text_card"
6+
android:title="TextCard" />
7+
</menu>

0 commit comments

Comments
 (0)