Please quote source: Android Monkey’s blog (http://sunjiajia.com)

preface

The great Google for Android launched a series of compatible packages, the latest is the Design Support Library, here we combine v7 and V4 several controls, to learn the Design Support Library several new controls! A Demo learns to use them!

Effect GIF:

Learning content

You can learn the following from this example:

  • Drawerlayout and NavigationView implement elegant Google-like sidebars;
  • The use of new controls for CoordinatorLayout, AppBarLayout, Toolbar, FloatingActionButton, and the Toolbar’s gradient hidden animation effect;
  • The official Tabs component TabLayout and ViewPager combine to realize the main interface content area;
  • SwipeRefreshLayout and RecyclerView combined to achieve drop-down refresh, and RecyclerView data Adapter RecyclerView.Adapter usage, and RecyclerView item click event implementation method;
  • The use of a CardView;
  • Usage of Snackbar, a new control similar to Toast.

## Layout file

Learning Android in the source code is a kind of immersive feeling.

It doesn’t matter how much code you learn to explain Android, because explaining it still won’t work. Therefore, we will post the layout file XML source code for learning, rest assured, all the knowledge has been annotated in the source code.

# # # styles. The XML source

<! <item name="colorPrimary">@color/main_blue_light</item> <item name="colorPrimaryDark">@color/main_blue_dark</item> <item name="colorAccent">@color/main_blue_light</item> <! <item name=" Android :textColorPrimary">@color/main_white</item>Copy the code

ColorPrimary, colorPrimaryDark, colorAccent, textColorPrimary, see Android L+ Theme and Toolbar examples.

Main layout Activity_my.xml source (key)



    
    
    


    
    

    

    
    


   Copy the code

Content_main.xml source (key)





    
    


        

        
        

        
        

    

    
    


    
    


   Copy the code

Frag_main. XML source code (Fragment layout)







    
    



   Copy the code

Item_main.xml source code (RecyclerView item)

    


    


   Copy the code




    

        

        

        
        

    

   Copy the code



        
    

    

    

   Copy the code

Java code

Java code writing is relatively simple, here only give recyclerView. Adapter writing (including item click events).

###RecyclerView.Adapter write source code

package com.sunjiajia.androidnewwidgetsdemo.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.sunjiajia.androidnewwidgetsdemo.R; import java.util.ArrayList; import java.util.List; /** * Created by Monkey on 2015/6/29. */ public class MyRecyclerViewAdapter extends recyclerView. Adapter public interface OnItemClickListener { void onItemClick(View view, int position); void onItemLongClick(View view, int position); } public OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } public Context mContext; public List mDatas; public LayoutInflater mLayoutInflater; public MyRecyclerViewAdapter(Context mContext) { this.mContext = mContext; mLayoutInflater = LayoutInflater.from(mContext); // Here is the simulation data. mDatas = new ArrayList<>(); for (int i = 'A'; i <= 'z'; i++) { mDatas.add((char) i + ""); }} / / Override public myRecyviewholder onCreateViewHolder(ViewGroup parent) int viewType) { View mView = mLayoutInflater.inflate(R.layout.item_main, parent, false); MyRecyclerViewHolder mViewHolder = new MyRecyclerViewHolder(mView); return mViewHolder; } /** * bind ViewHoler, */ @override public void onBindViewHolder(final myRecyviewholder holder, Final int Position) {// Click events are implemented here, using RecyclerView to fill the layout controls can be clicked if (mOnItemClickListener! = null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onItemClick(holder.itemView, position); }}); / / long click event holder. ItemView. SetOnLongClickListener (new the OnLongClickListener () {@ Override public Boolean onLongClick(View v) { mOnItemClickListener.onItemLongClick(holder.itemView, position); return true; }}); } holder.mTextView.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); }}Copy the code

MyRecyclerViewHolder. Java source code

package com.sunjiajia.androidnewwidgetsdemo.adapter; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import com.sunjiajia.androidnewwidgetsdemo.R; /** * Created by Monkey on 2015/6/29. */ public class MyRecyclerViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public MyRecyclerViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.id_textview); }}Copy the code

conclusion

Learning Android in the source code is a kind of immersive feeling.

The whole Demo source code I put on GitHub, thank you star ~ in the process of looking at the source code if you find any problems, please leave a message, see certain reply.

Source address: AndroidNewWidgetsDemo

## Pure girl

About me

Review images

  • Wechat official account: Android Fantasy record (Android_amazing)