CoordinatorLayout与AppBarLayout
1.AppBarLayout 嵌套着TabLayout
要使用这2个控件需要在AndroidStudio中.gradle文件中添加2个依赖文件:
(注:版本要一致,否则运行的时候会报各种奇葩的错误)
**NoClassDefFoundError: Failed resolution of: Landroid/support/v7/internal/widget/TintManager
compile ‘com.android.support:recyclerview-v7:24.2.0’
compile ‘com.android.support:design:24.2.0’**
(1)首先来看布局文件;
布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="10dp"
android:src="@drawable/ic_add"
/>
</android.support.design.widget.CoordinatorLayout>
(2)再看Main界面的代码,很简单的实现
package com.example.myapplication;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fragment> listFragments;
private List<String> list_title;
private TabLayout mTabLayout;
private ViewPager mViewPager;
private FragmentAdapter fAdapter;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_menu);
toolbar.setTitle("My Coordinator");
setSupportActionBar(toolbar);
mTabLayout = (TabLayout)findViewById(R.id.tabs);
mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
list_title = new ArrayList<String>();
list_title.add("热门推荐");
list_title.add("热门收藏");
list_title.add("本月热榜");
mTabLayout.addTab(mTabLayout.newTab().setText(list_title.get(0)));
mTabLayout.addTab(mTabLayout.newTab().setText(list_title.get(1)));
mTabLayout.addTab(mTabLayout.newTab().setText(list_title.get(2)));
listFragments = new ArrayList<>();
for (int i= 0;i<list_title.size();i++){
OneFragment oneFragment = OneFragment.newInstance(list_title.get(i),i+"");
listFragments.add(oneFragment);
}
fAdapter = new FragmentAdapter(getSupportFragmentManager(),listFragments,list_title);
mViewPager.setAdapter(fAdapter);
mTabLayout.setupWithViewPager(mViewPager);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> list_fragment; //fragment列表
private List<String> list_Title; //tab名的列表
public FragmentAdapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_Title){
super(fm);
this.list_fragment = list_fragment;
this.list_Title= list_Title;
}
@Override
public Fragment getItem(int position) {
return listFragments.get(position);
}
@Override
public int getCount() {
return list_Title.size() > 0 ? list_Title.size():0;
}
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position % list_Title.size());
}
}
}
(3)下面是Fragment的代码
package com.example.myapplication;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link OneFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link OneFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class OneFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
public OneFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment OneFragment.
*/
// TODO: Rename and change types and number of parameters
public static OneFragment newInstance(String param1, String param2) {
OneFragment fragment = new OneFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
TextView textView = (TextView)view.findViewById(R.id.tv);
textView.setText(mParam1+mParam2);
String arr[] = new String[50];
for (int i = 0;i<50;i++){
arr[i] = i+"";
}
mRecyclerView = (RecyclerView)view.findViewById(R.id.recylerview);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
// 3,(可选)如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
// 4,设置Adapter
mRecyclerView.setAdapter( new RecyAdadapter(arr));
return view;
}
class RecyAdadapter extends RecyclerView.Adapter<RecyAdadapter.MyViewHolder>{
public String[] datas = null;
public RecyAdadapter(String[] datas) {
this.datas = datas;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextView.setText(datas[position]);
}
@Override
public int getItemCount() {
return datas.length > 0 ? datas.length:0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
private TextView mTextView;
public MyViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.item_tv);
}
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
// throw new RuntimeException(context.toString()
// + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
(4)隐藏ActionBar,用Toolbar来代替ActionBar,要在资源文件中加上这2句代码:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
(5)还有2个资源文件的代码drawable中
ic_add.xml:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
>
<path android:fillColor="#FFFFFF"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
ic_menu.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M3,18h18v-2H3v2zm0,-5h18v-2H3v2zm0,-7v2h18V6H3z" />
</vector>
以上2个不太懂,我也是在网上看的、
最后附上效果图:
如何制作.gif的动态效果图,求大神指导!
QQ:1119348287 微信: 15910469091