Android 向上滑动视图,然后再次向下滑动同一视图
我想制作一个android应用程序,我想在其中实现与Inshorts新闻应用程序相同的行为。我想添加一个像卡片堆栈一样的视图,当向上滑动时,卡片会向上滑动,当向下滑动时,刚才我有一张向上滑动的卡片会出现在前面 这是我的密码Android 向上滑动视图,然后再次向下滑动同一视图,android,Android,我想制作一个android应用程序,我想在其中实现与Inshorts新闻应用程序相同的行为。我想添加一个像卡片堆栈一样的视图,当向上滑动时,卡片会向上滑动,当向下滑动时,刚才我有一张向上滑动的卡片会出现在前面 这是我的密码 public class CardStackViewExample extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super
public class CardStackViewExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_stack_view);
LayoutInflater inflater = LayoutInflater.from(this);
MyCardStackView sv = (MyCardStackView)findViewById(R.id.my_stack_view);
sv.setOrientation(SwipeTouchListener.Orientation.Vertical);
sv.setAdapter(new CardAdapter());
View emptyView = inflater.inflate(R.layout.empty_page, null);
sv.setEmptyView(emptyView);
}
}
public class MyCardStackView extends AbstractCardsStackView {
private static final String TAG = "MyCardsStackView";
public MyCardStackView(Context context) {
super(context);
}
public MyCardStackView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCardStackView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onStackGettingEmpty() {
Log.d(TAG, "Stack getting empty");
}
@Override
public void onSwipedLeft() {
Log.d(TAG, "Swiped Left");
super.onSwipedLeft();
}
@Override
public void onSwipedRight() {
Log.d(TAG, "Swiped Right");
super.onSwipedRight();
}
@Override
public void onSwipedUp() {
Log.d(TAG, "Swiped Up");
super.onSwipedUp();
}
@Override
public void onSwipedDown() {
Log.d(TAG, "Swiped Down");
super.onSwipedDown();
}
}
public class AbstractCardsStackView extends FrameLayout implements SwipeTouchListener.OnCardMovement {
private static final int STACK_BUFFER = 5;
private int index = 0;
private View emptyStackView = null;
private boolean emptyPageAdded = false;
private BaseAdapter adapter;
private SwipeTouchListener.Orientation orientation = SwipeTouchListener.Orientation.Horizontal;
public AbstractCardsStackView(Context context) {
super(context);
}
public AbstractCardsStackView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AbstractCardsStackView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAdapter(BaseAdapter adapter) {
this.adapter = adapter;
index = 0;
loadNext();
appendBottomView();
}
public BaseAdapter getAdapter() {
return adapter;
}
public void setOrientation(SwipeTouchListener.Orientation orientation) {
this.orientation = orientation;
}
public void setEmptyView(View view) {
emptyStackView = view;
}
private void loadNext() {
removeTopView();
appendBottomView();
}
private boolean removeTopView() {
boolean removed = false;
int children = getChildCount();
if ( children > 0 ) {
View topView = getChildAt(children-1);
if ( topView != emptyStackView ) {
removeView(topView);
removed = true;
}
children = getChildCount();
if ( children > 0 ) {
topView = getChildAt(children - 1);
if ( topView.getAlpha() < 0.98f ) {
topView.setAlpha(1.0f);
}
}
}
return removed;
}
private boolean appendBottomView() {
boolean added = false;
if ( index < adapter.getCount() ) {
View nextBottomView = adapter.getView(index++, null, this);
if (nextBottomView != null) {
nextBottomView.setOnTouchListener(new SwipeTouchListener(nextBottomView, this, orientation));
addView(nextBottomView, 0);
added = true;
if (index + STACK_BUFFER > adapter.getCount()) {
onStackGettingEmpty();
}
}
} else if ( emptyStackView != null && !emptyPageAdded ) {
addView(emptyStackView, 0);
emptyPageAdded = true;
added = true;
}
return added;
}
@Override
public void onSwipedLeft() {
loadNext();
}
@Override
public void onSwipedRight() {
loadNext();
}
@Override
public void onSwipedUp() {
loadNext();
}
@Override
public void onSwipedDown() {
loadNext();
}
@Override
public void onMovedFromCenter(float distance) {
if ( getChildCount() > 1 ) {
View bottomView = getChildAt(0);
float alpha = Math.max(-1.0f, Math.min(1.0f, distance / 300.0f));
bottomView.setAlpha(Math.abs(alpha));
}
}
public void onStackGettingEmpty() {
}
}
public class CardAdapter extends BaseAdapter {
@Override
public int getCount() {
return 5;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View container = inflater.inflate(R.layout.my_card, viewGroup, false);
final TextView tv = (TextView)container.findViewById(R.id.textView);
Button button= (Button)container.findViewById(R.id.button);
tv.setText("This is card #" + (i+1));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText("You clicked on button #" + (i+1));
}
});
return container;
}
}
activity_card_stack_view.xml
----------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".CardStackViewExample">
<com.edcast.cardsctackview.MyCardStackView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
android:id="@+id/my_stack_view" />
</RelativeLayout>
empty_page.xml
--------------
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="There is no more card"
android:id="@+id/textView"
android:layout_gravity="center" />
</FrameLayout>
my_card.xml
-----------
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click on Me"
android:id="@+id/button" />
</LinearLayout>
</FrameLayout>
slide_down.xml
--------------
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
slide_left.xml
--------------
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
slide_right.xml
---------------
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
slide_up.xml
------------
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
公共类CardStackViewExample扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u card\u stack\u视图);
LayoutFlater充气机=LayoutFlater.from(本机);
MyCardStackView sv=(MyCardStackView)findviewbyd(R.id.my_stack_view);
sv.setOrientation(SwipeTouchListener.Orientation.Vertical);
sv.setAdapter(新卡适配器());
View emptyView=充气机。充气(R.layout.empty_页面,空);
sv.setEmptyView(emptyView);
}
}
公共类MyCardStackView扩展了AbstractCardsStackView{
私有静态最终字符串TAG=“MyCardsStackView”;
公共MyCardStackView(上下文){
超级(上下文);
}
公共MyCardStackView(上下文、属性集属性){
超级(上下文,attrs);
}
公共MyCardStackView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
@凌驾
public void onStackGettingEmpty(){
Log.d(标记“堆栈变空”);
}
@凌驾
公共空间{
Log.d(标签“向左滑动”);
super.onSwipedLeft();
}
@凌驾
公共空间{
Log.d(标签“向右滑动”);
super.onswipdright();
}
@凌驾
公共void onswipdup(){
Log.d(标记“刷卡”);
super.onswipdup();
}
@凌驾
公共无效onSwipedDown(){
Log.d(标记“刷下”);
super.onSwipedDown();
}
}
公共类AbstractCardsTackView扩展了FrameLayout,实现了SwipeTouchListener.OnCardMovement{
专用静态最终整数堆栈_缓冲区=5;
私有整数指数=0;
私有视图emptyStackView=null;
私有布尔值emptyPageAdded=false;
专用基址适配器;
专用SwipeTouchListener.Orientation=SwipeTouchListener.Orientation.Horizontal;
公共摘要CardsStackView(上下文){
超级(上下文);
}
public AbstractCardsStackView(上下文、属性集属性){
超级(上下文,attrs);
}
public AbstractCardsStackView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
public void setAdapter(BaseAdapter适配器){
this.adapter=适配器;
指数=0;
loadNext();
appendBottomView();
}
公共BaseAdapter getAdapter(){
返回适配器;
}
公共void setOrientation(SwipeTouchListener.Orientation){
这个方向=方向;
}
公共void setEmptyView(视图){
emptyStackView=视图;
}
私有void loadNext(){
removeTopView();
appendBottomView();
}
私有布尔值removeTopView(){
布尔值=假;
int children=getChildCount();
如果(子项>0){
视图topView=getChildAt(children-1);
如果(topView!=emptyStackView){
移除视图(topView);
删除=真;
}
children=getChildCount();
如果(子项>0){
topView=getChildAt(children-1);
if(topView.getAlpha()<0.98f){
topView.setAlpha(1.0f);
}
}
}
移除返回;
}
私有布尔视图(){
布尔加法=假;
if(索引adapter.getCount()){
onStackGettingEmpty();
}
}
}else if(emptyStackView!=null&!emptyPageAdded){
addView(emptyStackView,0);
emptyPageAdded=true;
添加=真;
}
增加了退货;
}
@凌驾
公共空间{
loadNext();
}
@凌驾
公共空间{
loadNext();
}
@凌驾
公共void onswipdup(){
loadNext();
}
@凌驾
公共无效onSwipedDown(){
loadNext();
}
@凌驾
移动中心上的公共空隙(浮动距离){
如果(getChildCount()>1){
视图底部视图=getChildAt(0);
浮点alpha=数学最大值(-1.0f,数学最小值(1.0f,距离/300.0f));
bottomView.setAlpha(Math.abs(alpha));
}
}
public void onStackGettingEmpty(){
}
}
公共类CardAdapter扩展了BaseAdapter{
@凌驾
public int getCount(){
返回5;
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(最终int i、视图视图、视图组视图组){
LayoutInflater充气器=LayoutInflater.from(viewGroup.getContext());
视图容器=充气机。充气(R.layout.my_卡,视图组,false);
final TextView tv=(TextView)container.findviewbyd(R.id.TextView);
Button Button=(Button)container.findViewById(R.id.Button);
tv.setText(“这是卡片”+(i+1));
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共空间