Android開発-DesignDemo-Android Studio(8)CoordinatorDemo(3)
9018 ワード
ActionBar非表示、TextViewサスペンションエントリの実装
覚えてるstyles.xmlで、テーマをNoActionBarに設定
activity_main.xml:
Mainactivity.java:
覚えてるstyles.xmlで、テーマをNoActionBarに設定
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iwanghang.coordinatordemo.MainActivity">
<!--enterAlwaysCollapsed ActionBar-->
<!--enterAlways ActionBar-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/Widget.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"/>
<TextView
android:text=" "
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_1"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
<TextView
android:clickable="true"
android:id="@+id/textView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="100dp"
android:textSize="20sp"
android:background="#f00"
android:text="textView_1
list
setBehavior " />
<TextView
android:clickable="true"
android:id="@+id/textView_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:background="#f00"
android:text="textView_2
" />
<TextView
app:layout_behavior="com.iwanghang.coordinatordemo.MyBehavior"
android:clickable="true"
android:id="@+id/textView_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:textSize="20sp"
android:background="#f00"
android:text="textView_3
list
layout_behavior " />
</android.support.design.widget.CoordinatorLayout>
前のブログに比べて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<View> 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<String> 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<RecyclerView.ViewHolder>{
private Context context;
private List<String> list;
public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public List<String> getList() {
return list;
}
public void setList(List<String> 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(); //
}
}
}