Android开发之Android 5.0新特性
2015谷歌开发者大会的召开,将Android推向了一个新的时代! Android 5.0的推出更是大会的一大亮点,在此次大会中, Android推出了许多的新控件,下面我们看看都有什么: android.support.v4.widget.DrawerLayout(经常作为整个布局的终极父类)android.support.design.widget.CoordinatorLayout(控件协调器)
android.support.design.widget.AppBarLayout(状态栏)
android.support.v7.widget.Toolbar(与AppBarLayout结合使用,指针widget)
android.support.design.widget.NavigationView(类似于抽屉)
android.support.design.widget.CollapsingToolbarLayout(顾名思义,折叠ToolBar)
android.support.v4.widget.NestedScrollView
android.support.v7.widget.CardView(卡片类控件)
android.support.design.widget.FloatingActionButton(炫酷的悬浮按钮)
android.support.v4.widget.SwipeRefreshLayout(下拉刷新)
android.support.v7.widget.RecyclerView(替代ListView,GridView的不二控件)
android.support.design.widget.TextInputLayout(与EditText控件结合使用,效果炫酷)
经过本人一段时间的研究,现已将所用控件集成到了一个Demo中,下面废话少说,直接贴代码了,还望诸位大神批评指正,灰常感谢♪(^∇^*)
特别注意: 本Demo中还集成使用了一些开源库: 1.圆形图片 使用方法: <de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:border_width="2dp"
app:border_color="#FF000000"/>
添加类库支持: 在module的build.gradle中添加类库支持 compile 'de.hdodenhof:circleimageview:1.3.0'
2.图片加载框架 使用方法: Glide.with(this).load(“url或者是drawable下的图片”).into(imageView)
添加类库支持: 在module的build.gradle中添加类库支持compile 'com.github.bumptech.glide:glide:3.6.0'
3.Activity炫酷跳转框架:使用方法:a.Start Activity in first activity.
findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MainActivity.this, SubActivity.class);
ActivityTransitionLauncher.with(MainActivity.this).from(v).launch(intent);
}
});
b.Receive intent in second activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ActivityTransition.with(getIntent()).to(findViewById(R.id.sub_imageView)).start(savedInstanceState);
}
c.If you want the exit animation, you can do like this.
private ExitActivityTransition exitTransition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub2);
exitTransition = ActivityTransition.with(getIntent()).to(findViewById(R.id.sub_imageView)).start(savedInstanceState);
}
@Override
public void onBackPressed() {
exitTransition.exit(this);
}
添加类库支持:在module的build.gradle中添加类库支持compile 'com.kogitune:pre-lollipop-activity-transition:1.0.0'
4.特别注意事项: 使用Android 5.0中的控件,必须在module的build.gradle中添加类库支持compile 'com.android.support:design:22.2.0'
本Demo中的module.gradle的dependencies如下:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'com.telly:floatingaction:0.0.4'
compile 'com.kogitune:pre-lollipop-activity-transition:1.0.0'
}
先上效果图:
核心代码如下: MainActivity
package com.hxht.testnewfeatures;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.hxht.testnewfeatures.adapter.MyPagerAdapter;
import com.hxht.testnewfeatures.utils.ToastUtils;
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private AppBarLayout appBarLayout;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
navigationView = (NavigationView) findViewById(R.id.navigationview);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewpager = (ViewPager) findViewById(R.id.viewpager);
}
private void initData() {
//TabLayout部分:
tabLayout.addTab(tabLayout.newTab().setText("互信"));
tabLayout.addTab(tabLayout.newTab().setText("你我"));
tabLayout.addTab(tabLayout.newTab().setText("互通"));
tabLayout.addTab(tabLayout.newTab().setText("世界"));
//设置Tab的颜色第一个参数表示普通Tab的颜色,第二个参数表示选中的Tab参数的颜色
tabLayout.setTabTextColors(Color.WHITE, Color.RED);
//ToolBar部分:
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("CSDN");
//设置actionbar可用
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
//actionBar.setIcon(R.mipmap.ic_menu);
actionBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
//ViewPager部分:
PagerAdapter adapter = new MyPagerAdapter(MainActivity.this, this);
viewpager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewpager);
//NavigationView部分:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int itemId = menuItem.getItemId();
switch (itemId) {
case R.id.navigation_item_1:
ToastUtils.showSafeToast(MainActivity.this, "Item01");
break;
case R.id.navigation_item_2:
ToastUtils.showSafeToast(MainActivity.this, "Item02");
break;
case R.id.navigation_item_3:
ToastUtils.showSafeToast(MainActivity.this, "Item03");
break;
case R.id.navigation_item_4:
ToastUtils.showSafeToast(MainActivity.this, "Item04");
break;
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
ToastUtils.showSafeToast(MainActivity.this, "CSDN");
break;
case R.id.action01:
ToastUtils.showSafeToast(MainActivity.this, "互信");
break;
case R.id.action02:
ToastUtils.showSafeToast(MainActivity.this, "你我");
break;
case R.id.action03:
ToastUtils.showSafeToast(MainActivity.this, "互通");
break;
case R.id.action04:
ToastUtils.showSafeToast(MainActivity.this, "世界");
break;
}
return super.onOptionsItemSelected(item);
}
}
DetailActivity
package com.hxht.testnewfeatures;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.hxht.testnewfeatures.utils.ToastUtils;
import com.kogitune.activity_transition.ActivityTransition;
public class DetailActivity extends AppCompatActivity {
private ImageView iv;
private Toolbar toolBar;
private CollapsingToolbarLayout collapsingToolbarLayout;
private FloatingActionButton floatingActionButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
inintView();
initData();
ActivityTransition.with(getIntent()).to(findViewById(R.id.main_content)).start(savedInstanceState);
ActivityTransition.with(getIntent()).to(findViewById(R.id.backdrop)).start(savedInstanceState);
}
private void inintView() {
iv = (ImageView) findViewById(R.id.backdrop);
toolBar = (Toolbar) findViewById(R.id.toolbar);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingactionbutton);
}
private void initData() {
//即可加载本地图片,亦可加载网络图片
//加载本地
Glide.with(this).load(R.mipmap.detail_bg_iv).into(iv);
//加载网络
//Glide.with(this).load("http://p0.so.qhimg.com/t013971ecacef25bf25.jpg").into(iv);
setSupportActionBar(toolBar);
Intent intent = getIntent();
String value = intent.getStringExtra("value");
collapsingToolbarLayout.setTitle(value);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showSafeToast(DetailActivity.this,"FloatingActionButton");
}
});
}
}
MyPagerAdapter
package com.hxht.testnewfeatures.adapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.hxht.testnewfeatures.DetailActivity;
import com.hxht.testnewfeatures.R;
import com.hxht.testnewfeatures.custom.DividerItemDecoration;
import com.hxht.testnewfeatures.utils.ToastUtils;
import com.kogitune.activity_transition.ActivityTransitionLauncher;
public class MyPagerAdapter extends PagerAdapter {
private Context context;
private AppCompatActivity activity;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
ToastUtils.showSafeToast(activity, "刷新完成");
if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
};
private SwipeRefreshLayout mSwipeRefreshLayout;
public MyPagerAdapter(Context context, AppCompatActivity activity) {
this.context = context;
this.activity = activity;
}
private String[] titles = new String[]{
"互信",
"你我",
"互通",
"世界"
};
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = null;
if (position == 0) {
view = initRecycleViewInFirstPosition();
} else if (position == 1) {
view = initViewInThirdPosition();
} else if (position == 2) {
view = initRecycleViewInSecondPosition();
} else {
view = View.inflate(context, R.layout.activity_viewpager_item04, null);
}
container.addView(view);
return view;
}
private View initViewInThirdPosition() {
View view;
view = View.inflate(context, R.layout.activity_viewpager_item03, null);
FloatingActionButton floatingactionbutton
= (FloatingActionButton) view.findViewById(R.id.floatingactionbutton);
floatingactionbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "互信你我,互通世界", Snackbar.LENGTH_LONG)
.setAction("互信互通", new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showSafeToast(activity, "FlyingSnow2211");
}
})
.show();
}
});
final TextInputLayout text_input_layout
= (TextInputLayout) view.findViewById(R.id.text_input_layout);
EditText editText = text_input_layout.getEditText();
text_input_layout.setHint("请输入4位数字:");
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (s.length() > 4) {
text_input_layout.setError("学号输入错误!");
text_input_layout.setErrorEnabled(true);
} else {
text_input_layout.setErrorEnabled(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
/**
* 初始化position=1位置上的view
*
* @return
*/
private View initRecycleViewInSecondPosition() {
View view;
view = View.inflate(context, R.layout.activity_viewpager_item02, null);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
mSwipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.RED,
Color.YELLOW, Color.BLACK);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.sendEmptyMessageDelayed(0, 5000);
}
});
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
final MyAdapter adapter = new MyAdapter(context);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("value", "英雄阅 -- ☞ " + adapter.names[position]);
ActivityTransitionLauncher.with(activity).from(view).launch(intent);
}
});
recyclerView.setAdapter(adapter);
//LinearLayoutManager manager = new LinearLayoutManager(context,
// LinearLayoutManager.HORIZONTAL, false);
GridLayoutManager manager = new GridLayoutManager(context, 2,
GridLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
//recyclerView.addItemDecoration(new DividerItemDecoration(context,
// DividerItemDecoration.HORIZONTAL_LIST));
recyclerView.addItemDecoration(new DividerItemDecoration(context,
DividerItemDecoration.VERTICAL_LIST));
return view;
}
/**
* 初始化position=0位置上的view
*
* @return
*/
private View initRecycleViewInFirstPosition() {
View view;
view = View.inflate(context, R.layout.activity_viewpager_item01, null);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
final MyAdapter adapter = new MyAdapter(context);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("value", "英雄阅 -- ☞ " + adapter.names[position]);
ActivityTransitionLauncher.with(activity).from(view).launch(intent);
}
});
recyclerView.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(context,
LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(context,
DividerItemDecoration.VERTICAL_LIST));
return view;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
MyAdapter
package com.hxht.testnewfeatures.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hxht.testnewfeatures.R;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
public static final String[] names = new String[]{
"宋江",
"卢俊义",
"吴用",
"公孙胜",
"关胜",
"林冲",
"秦明",
"呼延灼",
"花荣",
"柴进",
"李应",
"朱仝",
"鲁智深",
"武松",
"董平",
"张清",
"杨志",
"徐宁",
"索超",
"戴宗",
"刘唐",
"李逵",
"史进",
"穆弘",
"雷横",
"李俊",
"阮小二",
"张横",
"阮小五",
"张顺",
"阮小七",
"杨雄",
"石秀",
"解珍",
"解宝 ",
"燕青",
"朱武",
"黄信",
"孙立",
"宣赞",
"郝思文",
"韩滔",
"彭玘",
"单廷圭",
"魏定国",
"萧让",
"裴宣",
"欧鹏",
"邓飞",
"燕顺",
"杨林",
"凌振",
"蒋敬",
"吕方",
"郭盛",
"安道全",
"皇甫端",
"王英",
"扈三娘",
"鲍旭",
"樊瑞",
"孔明",
"孔亮",
"项充",
"李衮",
"金大坚",
"马麟",
"童威",
"童猛",
"孟康",
"侯健",
"陈达",
"杨春",
"郑天寿",
"陶宗旺",
"宋清",
"乐和",
"龚旺",
"丁得孙",
"穆春",
"曹正",
"宋万",
"杜迁",
"薛永",
"李忠",
"周通",
"汤隆",
"杜兴",
"邹渊",
"邹润",
"朱贵",
"朱富",
"施恩",
"蔡福",
"蔡庆",
"李立",
"李云",
"焦挺",
"石勇",
"孙新",
"顾大嫂",
"孙二娘",
"王定六",
"郁保四",
"白胜",
"时迁",
"段景住"
};
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
private OnItemClickListener onItemClickListener;
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public MyAdapter(Context context) {
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_rv_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.tv_name.setText(names[position]);
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(holder.view, position);
}
}
});
}
@Override
public int getItemCount() {
return names.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_name;
View view;
public MyViewHolder(View itemView) {
super(itemView);
this.view = itemView;
tv_name = (TextView) itemView.findViewById(R.id.tv_name);
}
}
}
DividerItemDecoration
package com.hxht.testnewfeatures.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
ToastUtils
package com.hxht.testnewfeatures.utils;
import android.app.Activity;
import android.widget.Toast;
public class ToastUtils {
public static void showSafeToast(final Activity activity, final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
} else {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
}
代码已贴出,正所谓取之于社会,回报于社会,还望各路大神批评指正,不喜勿喷,灰常感谢♪(^∇^*)!!!