Skip to content

Commit

Permalink
add TextCard
Browse files Browse the repository at this point in the history
- TextCardModel
- TextCardStackAdapter
- and TextCardActivity as test
  • Loading branch information
wujiawei committed Jun 21, 2016
1 parent 943de4e commit 47fd523
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 38 deletions.
15 changes: 14 additions & 1 deletion AndTinder/src/main/java/com/andtinder/model/CardModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class CardModel {

private OnClickListener mOnClickListener = null;

private OnSwipeListener mOnSwipeListener = null;

public interface OnCardDismissedListener {
void onDismiss();
void onLike();
Expand All @@ -44,6 +46,10 @@ public interface OnClickListener {
void OnClickListener();
}

public interface OnSwipeListener {
void onSwipe(float dx);
}

public CardModel() {
this(null, null, (Drawable)null);
}
Expand Down Expand Up @@ -108,12 +114,19 @@ public OnCardDismissedListener getOnCardDismissedListener() {
return this.mOnCardDismissedListener;
}


public void setOnClickListener( OnClickListener listener ) {
this.mOnClickListener = listener;
}

public OnClickListener getOnClickListener() {
return this.mOnClickListener;
}

public OnSwipeListener getOnSwipeListener() {
return mOnSwipeListener;
}

public void setOnSwipeListener(OnSwipeListener mOnSwipeListener) {
this.mOnSwipeListener = mOnSwipeListener;
}
}
23 changes: 23 additions & 0 deletions AndTinder/src/main/java/com/andtinder/model/TextCardModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.andtinder.model;

/**
* Created by einverne on 16/6/21.
*/

public class TextCardModel extends CardModel {

private String textShow;

public TextCardModel(String textShow) {
this.textShow = textShow;
}

public String getTextShow() {
return textShow;
}

public void setTextShow(String textShow) {
this.textShow = textShow;
}

}
23 changes: 19 additions & 4 deletions AndTinder/src/main/java/com/andtinder/view/CardContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void onInvalidated() {
private ListAdapter mListAdapter;
private float mLastTouchX;
private float mLastTouchY;
private float mFirstTouchX; // recode the x when touched
private View mTopCard; // first card on cards stack
private int mTouchSlop;
private int mGravity;
Expand Down Expand Up @@ -255,7 +256,7 @@ public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " ");
// Log.d("Touch Event", MotionEvent.actionToString(event.getActionMasked()) + " ");
final int pointerIndex;
final float x, y;
final float dx, dy;
Expand Down Expand Up @@ -298,6 +299,13 @@ public boolean onTouchEvent(MotionEvent event) {
if(!mDragging) {
return true;
}
float deltaX = event.getX() - mFirstTouchX;

CardModel cardModel = (CardModel)getAdapter().getItem(0);
if (cardModel.getOnSwipeListener() != null) {
cardModel.getOnSwipeListener().onSwipe(deltaX);
}


mTopCard.setTranslationX(mTopCard.getTranslationX() + dx);
// mTopCard.setTranslationY(mTopCard.getTranslationY() + dy);
Expand Down Expand Up @@ -341,6 +349,11 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}

/**
* 事件拦截
* @param event event
* @return
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mTopCard == null) {
Expand All @@ -352,11 +365,12 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
final int pointerIndex;
final float x, y;
final float dx, dy;
CardModel cardModel;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mTopCard.getHitRect(childRect);

CardModel cardModel = (CardModel)getAdapter().getItem(0);
cardModel = (CardModel)getAdapter().getItem(0);

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

mFirstTouchX = x;
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = event.getPointerId(pointerIndex);
Expand Down Expand Up @@ -515,8 +530,8 @@ public void run() {
.alpha(.75f)
.setInterpolator(new LinearInterpolator())
.x(targetX)
.y(targetY)
.rotation(Math.copySign(45, velocityX))
// .y(targetY)
.rotation(Math.copySign(20, velocityX))
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.andtinder.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andtinder.R;
import com.andtinder.model.CardModel;
import com.andtinder.model.TextCardModel;

/**
* Created by einverne on 16/6/21.
*/

public class TextCardStackAdapter extends CardStackAdapter {
public TextCardStackAdapter(Context context) {
super(context);
}

@Override
protected View getCardView(int position, CardModel model, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.text_card_inner, parent, false);

assert convertView != null;
}

((TextView) convertView.findViewById(R.id.tv_main)).setText(((TextCardModel)model).getTextShow());

return convertView;
}
}
18 changes: 18 additions & 0 deletions AndTinder/src/main/res/layout/text_card_inner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="400dp"
android:background="@android:color/darker_gray"
android:gravity="center_vertical|center_horizontal|center">

<TextView
android:id="@+id/tv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textColor="@android:color/holo_green_dark"
android:textSize="50sp"
android:text="demo" />
</RelativeLayout>
23 changes: 15 additions & 8 deletions AndTinderDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andtinder.demo">

<uses-sdk
android:minSdkVersion="13"
/>
<application android:allowBackup="true"
android:label="@string/app_name"
<uses-sdk android:minSdkVersion="13" />

<android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.andtinder.demo.MainActivity"
android:label="@string/app_name" >
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TextCardActivity"></activity>
</application>
</manifest>

</manifest>
59 changes: 34 additions & 25 deletions AndTinderDemo/src/main/java/com/andtinder/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.andtinder.model.CardModel;
import com.andtinder.model.Orientations;
import com.andtinder.model.TextCardModel;
import com.andtinder.view.CardContainer;
import com.andtinder.view.SimpleCardStackAdapter;

public class MainActivity extends Activity implements View.OnClickListener{
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

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

mCardContainer = (CardContainer) findViewById(R.id.layoutview);
Expand All @@ -63,29 +68,6 @@ public void onCreate(Bundle savedInstanceState) {
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));

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

cardModel.setOnSwipeListener(new CardModel.OnSwipeListener() {
@Override
public void onSwipe(float dx) {
Log.i("Swipe ", "dx : " + dx);
}
});

adapter.add(cardModel);

mCardContainer.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_text_card:
Intent intent = new Intent(this, TextCardActivity.class);
startActivity(intent);

return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onClick(View v) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.andtinder.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.andtinder.model.TextCardModel;
import com.andtinder.view.CardContainer;
import com.andtinder.view.TextCardStackAdapter;

public class TextCardActivity extends AppCompatActivity {

private CardContainer mTextCardContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_card);

mTextCardContainer = (CardContainer) findViewById(R.id.textCardContainer);

TextCardStackAdapter textCardStackAdapter = new TextCardStackAdapter(this);
textCardStackAdapter.add(new TextCardModel("text"));
textCardStackAdapter.add(new TextCardModel("text2"));
textCardStackAdapter.add(new TextCardModel("text3"));
textCardStackAdapter.add(new TextCardModel("text4"));

mTextCardContainer.setAdapter(textCardStackAdapter);

}
}
18 changes: 18 additions & 0 deletions AndTinderDemo/src/main/res/layout/activity_text_card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_text_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andtinder.demo.TextCardActivity">

<com.andtinder.view.CardContainer
android:id="@+id/textCardContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">

</com.andtinder.view.CardContainer>

</RelativeLayout>
7 changes: 7 additions & 0 deletions AndTinderDemo/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/menu_text_card"
android:title="TextCard" />
</menu>
Loading

0 comments on commit 47fd523

Please sign in to comment.