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

Android開發-DesignDemo-AndroidStudio(八)CoordinatorDemo(3)

編輯:關於Android編程

實現ActionBar隱藏顯示,TextView懸停條目

記得styles.xml裡,把主題設置為NoActionBar

\

activity_main.xml:

 




    
    
    
        
        
    

    

    

    

    


跟上一篇博文相比,java部分沒有改變,以下是java部分。

 

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.support.v7.widget.Toolbar;
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);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}
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.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

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

    /**
     * 實現下面3行方法 則可以通過
     * app:layout_behavior="com.iwanghang.coordinatordemo.MyBehavior"
     * 來實現 MyBehavior
     */
    public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public MyBehavior() {
    }

    // Behavior 開始滾動
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return true; // 為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(); // 隱藏控件
        }
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved