Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發-DesignDemo-AndroidStudio(六)CoordinatorDemo(1)

Android開發-DesignDemo-AndroidStudio(六)CoordinatorDemo(1)

編輯:關於Android編程

實現效果:

\

項目需求的2個jar包:

\

\

MainActivity.java:

 

package com.iwanghang.coordinatordemo;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.SwipeDismissBehavior;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private TextView textView_1;
    private TextView textView_2;

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

        textView_1 = (TextView) findViewById(R.id.textView_1);
        textView_2 = (TextView) findViewById(R.id.textView_2);

        // 手動強轉控件類型
        CoordinatorLayout.LayoutParams params_1 =
                (CoordinatorLayout.LayoutParams) textView_1.getLayoutParams();
        CoordinatorLayout.LayoutParams params_2 =
                (CoordinatorLayout.LayoutParams) textView_2.getLayoutParams();

        // setBehavior 設置控件的行為
        //params.setBehavior();
        // SwipeDismissBehavior 實例化滑動刪除行為
        SwipeDismissBehavior behavior = new SwipeDismissBehavior<>();

        // 滑動刪除行為監聽
        behavior.setListener(new SwipeDismissBehavior.OnDismissListener() {
            @Override
            public void onDismiss(View view) { // 當控件消失
                // 在設置控件隱藏之前,其實控件只是透明,還可以滑動回來
                view.setVisibility(View.GONE); // 設置控件隱藏

                Snackbar.make(view,"刪除控件",Snackbar.LENGTH_LONG)
                        .setAction("撤銷", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                textView_2.setVisibility(View.VISIBLE); // 設置控件顯示
                                ViewCompat.animate(textView_2).alpha(1).start();
                            }
                        }).show();
            }

            @Override
            public void onDragStateChanged(int state) { // 當控件停止

            }
        });

        // 把滑動刪除行為設置到textView控件上
        params_2.setBehavior(behavior);











        RecyclerView recycler_1 = (RecyclerView) findViewById(R.id.recycler_1);
        List list = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
            list.add(String.format(Locale.CHINA,"第%03d條數據",i+1));
        }
        recycler_1.setAdapter(new MyAdapter(this,list));

        MyBehavior myBehavior = new MyBehavior();
        params_1.setBehavior(myBehavior);









    }
}

 

MyAdapter.java:

 

package com.iwanghang.coordinatordemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter{
    private Context context;
    private List list;

    public MyAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView view = new TextView(context);
        return new RecyclerView.ViewHolder(view){};
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((TextView) holder.itemView).setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}

 

MyBehavior.java:

 

package com.iwanghang.coordinatordemo;

import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.view.View;

/**
 * 重寫Behavior的方法
 */
public class MyBehavior extends CoordinatorLayout.Behavior {

    // Behavior 開始滾動
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return true;
    }

    // Behavior 正在滾動
    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        if (dy < 0){ // 向上滾動
            ViewCompat.animate(child).scaleX(1).scaleY(1).start(); // 顯示控件
        }else { // 向下滾動
            ViewCompat.animate(child).scaleX(0).scaleY(0).start(); // 隱藏控件
        }
    }
}
activity_main.xml:

 

 




    

    

    

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved