Skip to content

Commit 2d0691b

Browse files
author
luobl
committed
解决第一个item不能滑动的问题
1 parent a3b200d commit 2d0691b

File tree

11 files changed

+250
-78
lines changed

11 files changed

+250
-78
lines changed

AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:versionName="1.0" >
66

77
<uses-sdk
8-
android:minSdkVersion="14"
8+
android:minSdkVersion="8"
99
android:targetSdkVersion="19" />
1010

1111
<application

res/drawable-hdpi/a.jpg

79.9 KB
Loading

res/drawable-hdpi/close_bg.png

1.9 KB
Loading

res/layout/activity_main.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
xmlns:tools="http://schemas.android.com/tools"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
tools:context="com.chiemy.cardview.MainActivity" >
5+
tools:context="com.chiemy.cardview.MainActivity"
6+
android:background="#00688B"
7+
>
68

79
<com.chiemy.cardview.view.CardView
810
android:id="@+id/cardView1"
911
android:layout_width="match_parent"
1012
android:layout_height="match_parent"
11-
android:padding="10dp"
1213
>
1314
</com.chiemy.cardview.view.CardView>
14-
15+
16+
<LinearLayout
17+
android:id="@+id/contentView"
18+
android:layout_width="match_parent"
19+
android:layout_height="match_parent"
20+
android:orientation="vertical"
21+
>
22+
</LinearLayout>
1523
</RelativeLayout>

res/layout/frag_layout.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/white"
6+
android:visibility="invisible"
7+
>
8+
9+
<TextView
10+
android:id="@+id/textView"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_centerHorizontal="true"
14+
android:textAppearance="?android:attr/textAppearanceLarge"
15+
/>
16+
17+
<ImageView
18+
android:id="@+id/button"
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:layout_alignParentLeft="true"
22+
android:layout_alignParentTop="true"
23+
android:src="@drawable/close_bg"
24+
android:padding="10dp"
25+
/>
26+
27+
<ImageView
28+
android:id="@+id/imageView1"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_centerHorizontal="true"
32+
android:layout_centerVertical="true"
33+
android:src="@drawable/a" />
34+
35+
</RelativeLayout>

res/layout/item_layout.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
android:layout_height="wrap_content"
1111
android:gravity="center"
1212
android:padding="20dp"
13-
android:text="TextView" />
13+
android:textAppearance="?android:attr/textAppearanceMedium"
14+
/>
1415

15-
<ImageView
16+
<TextView
1617
android:id="@+id/imageView1"
1718
android:layout_width="match_parent"
1819
android:layout_height="wrap_content"
1920
android:layout_below="@+id/textView1"
20-
android:scaleType="centerCrop"
21+
android:text="@string/text"
22+
android:textAppearance="?android:attr/textAppearanceMedium"
23+
android:gravity="center"
24+
android:layout_marginTop="30dp"
25+
android:layout_marginBottom="30dp"
2126
/>
2227

2328
</RelativeLayout>

res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<string name="app_name">CardView</string>
55
<string name="hello_world">Hello world!</string>
66
<string name="action_settings">Settings</string>
7+
<string name="text">面带笑容,不气馁往前冲,我愈挫愈勇!!</string>
78

89
</resources>

src/com/chiemy/cardview/MainActivity.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,58 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import android.app.Activity;
76
import android.content.Context;
87
import android.os.Bundle;
8+
import android.support.v4.app.FragmentActivity;
9+
import android.support.v4.app.FragmentManager;
910
import android.view.LayoutInflater;
1011
import android.view.View;
1112
import android.view.ViewGroup;
12-
import android.widget.ImageView;
1313
import android.widget.TextView;
1414
import android.widget.Toast;
1515

1616
import com.chiemy.cardview.view.CardAdapter;
1717
import com.chiemy.cardview.view.CardView;
1818
import com.chiemy.cardview.view.CardView.OnCardClickListener;
1919

20-
public class MainActivity extends Activity {
21-
private List<String> list;
20+
public class MainActivity extends FragmentActivity implements OnCardClickListener{
21+
List<String> list;
22+
private TestFragment frag;
23+
private View contentView;
2224
@Override
2325
protected void onCreate(Bundle savedInstanceState) {
2426
super.onCreate(savedInstanceState);
2527
setContentView(R.layout.activity_main);
28+
initUI();
29+
}
30+
31+
private void initUI() {
2632
CardView cardView = (CardView) findViewById(R.id.cardView1);
27-
cardView.setOnCardClickListener(new OnCardClickListener() {
28-
@Override
29-
public void onCardClick(View view, int position) {
30-
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
31-
}
32-
});
33+
cardView.setOnCardClickListener(this);
34+
cardView.setItemSpace(Utils.convertDpToPixelInt(this, 20));
35+
3336
MyCardAdapter adapter = new MyCardAdapter(this);
3437
adapter.addAll(initData());
3538
cardView.setAdapter(adapter);
39+
40+
contentView = findViewById(R.id.contentView);
41+
FragmentManager manager = getSupportFragmentManager();
42+
frag = new TestFragment();
43+
manager.beginTransaction().add(R.id.contentView, frag).commit();
44+
}
45+
46+
@Override
47+
public void onCardClick(final View view, final int position) {
48+
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
49+
Bundle bundle = new Bundle();
50+
bundle.putString("text", list.get(position%list.size()));
51+
frag.show(view,bundle);
3652
}
3753

54+
55+
3856
private List<String> initData() {
39-
List<String> list = new ArrayList<String>();
57+
list = new ArrayList<String>();
4058
list.add("a");
4159
list.add("b");
4260
list.add("c");
@@ -66,9 +84,9 @@ protected View getCardView(int position,
6684
convertView = inflater.inflate(R.layout.item_layout, parent, false);
6785
}
6886
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
69-
tv.setText(getItem(position%7).toString());
87+
tv.setText(getItem(position%list.size()).toString());
7088
return convertView;
7189
}
7290
}
73-
91+
7492
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.chiemy.cardview;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.View.OnClickListener;
9+
import android.view.ViewGroup;
10+
import android.view.animation.AccelerateInterpolator;
11+
import android.widget.TextView;
12+
13+
import com.nineoldandroids.animation.Animator;
14+
import com.nineoldandroids.animation.AnimatorListenerAdapter;
15+
import com.nineoldandroids.view.ViewHelper;
16+
import com.nineoldandroids.view.ViewPropertyAnimator;
17+
18+
public class TestFragment extends Fragment{
19+
private TextView tv;
20+
private View root;
21+
22+
@Override
23+
public View onCreateView(LayoutInflater inflater,
24+
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
25+
root = inflater.inflate(R.layout.frag_layout, container,false);
26+
initUI(root);
27+
return root;
28+
}
29+
30+
private void initUI(final View root) {
31+
root.setClickable(true);
32+
tv = (TextView) root.findViewById(R.id.textView);
33+
root.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
34+
@Override
35+
public void onClick(View v) {
36+
root.setClickable(false);
37+
ViewPropertyAnimator.animate(root)
38+
.rotationY(-90).setDuration(200)
39+
.setListener(new AnimatorListenerAdapter(){
40+
@Override
41+
public void onAnimationEnd(Animator animation) {
42+
root.clearAnimation();
43+
root.setVisibility(View.INVISIBLE);
44+
}
45+
});
46+
}
47+
});
48+
}
49+
50+
public void show(final View view,Bundle bundle){
51+
String text = bundle.getString("text");
52+
tv.setText(text);
53+
view.setClickable(false);
54+
view.setRotationY(0);
55+
ViewHelper.setRotationY(root, -90);
56+
root.setVisibility(View.VISIBLE);
57+
58+
ViewPropertyAnimator.animate(view).rotationY(90)
59+
.setDuration(300).setListener(null)
60+
.setInterpolator(new AccelerateInterpolator());
61+
62+
ViewPropertyAnimator.animate(root)
63+
.rotationY(0).setDuration(200).setStartDelay(300)
64+
.setListener(new AnimatorListenerAdapter() {
65+
@Override
66+
public void onAnimationEnd(Animator animation) {
67+
ViewHelper.setRotationY(view, 0);
68+
view.setClickable(true);
69+
}
70+
});
71+
}
72+
}

src/com/chiemy/cardview/Utils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.chiemy.cardview;
2+
3+
import android.content.Context;
4+
import android.util.DisplayMetrics;
5+
6+
public class Utils {
7+
8+
9+
10+
/**
11+
* This method converts device specific pixels to device independent pixels.
12+
*
13+
* @param px
14+
* A value in px (pixels) unit. Which we need to convert into db
15+
* @param context
16+
* Context to get resources and device specific display metrics
17+
* @return A float value to represent db equivalent to px value
18+
*/
19+
public float convertPixelsToDp(Context ctx, float px) {
20+
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
21+
float dp = px / (metrics.densityDpi / 160f);
22+
return dp;
23+
24+
}
25+
26+
public static int convertDpToPixelInt(Context context, float dp) {
27+
28+
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
29+
int px = (int) (dp * (metrics.densityDpi / 160f));
30+
return px;
31+
}
32+
33+
public static float convertDpToPixel(Context context, float dp) {
34+
35+
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
36+
float px = (float) (dp * (metrics.densityDpi / 160f));
37+
return px;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)