Friday 12 December 2014

RecyclerView Tutorial


Android has introduced a new widget for displaying data collections which is a more advanced and flexible version of ListView. RecyclerView incorporates ListView and ViewHolder reusability concept very efficiently.

RecyclerView is very useful with large data collections whose content changes at runtime.

RecyclerView includes:

  1. Layout managers for positioning items. 
  2. Default animatios for common item operations , such as removal or addition of items.


Custom animation and layout managers can be defined as per the requirement.



Layout Manager:


RecyclerView provides 3 built-in layout managers:

  1. LinearLayoutManager shows items in a vertical or horizontal scrolling list. 
  2. GridLayoutManage shows items in a grid. 
  3. StaggeredGridLayoutManager shows items in a staggered grid. 

For custom layout manager, extend the RecyclerView.LayoutManager class.

Animation:



  1. Animations for adding and removing items are enabled by default.
  2. For custom animation, extend the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method.



RecyclerView Demo:

  1. Create a new Android project.
  2. Add the v7 support library, for eclipse follow the link and for gradle add: 
dependencies{
        compile com.android.support:recyclerview-v7:21.0.+
}
     3.  Add RecyclerView in xml:


<android.support.v7.widget.RecyclerView  
     android:id="@+id/recylcer_view"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:scrollbars="vertical" />


   4. Create Adapter file MyRecyclerViewAdapter.java
      by extending RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>


public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{

 private String[] mData;

 public MyRecyclerViewAdapter(String[] mData) {
  this.mData = mData;
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  View v = LayoutInflater.from(viewGroup.getContext())
    .inflate(R.layout.list_item_layout, viewGroup, false);

  ViewHolder vh = new ViewHolder(v);
  return vh;

 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int i) {
  viewHolder.mTextView.setText(mData[i]);
 }

 @Override
 public int getItemCount() {
  if (mData != null)
   return mData.length;
  return 0;
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {
  // each data item is just a string in this case
  public TextView mTextView;

  public ViewHolder(View itemView) {
   super(itemView);
   mTextView = (TextView)itemView.findViewById(R.id.txt);;
  }
 }
}


   5. Create Activity MyActivity.java

public class MyActivity extends Activity implements View.OnClickListener{

    private RecyclerView mRecyclerView;
    private MyRecyclerViewAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private GridLayoutManager mGridLayoutManager;
    private StaggeredGridLayoutManager mStaggeredGridLayoutManager;

    private String[] mSampleData = {"What is Android Lollipop?", "What is Android 5.0?",                     "What is RecyclerView?", "What is difference between RecyclerView and ListView?", "How does this demo app help?"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mRecyclerView = (RecyclerView) findViewById(R.id.recylcer_view);
        mRecyclerView.setHasFixedSize(true);

        mAdapter = new MyRecyclerViewAdapter(mSampleData);
        mRecyclerView.setAdapter(mAdapter);
        applyLinearLayoutManager();
    }

    private void applyLinearLayoutManager(){
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }

    private void applyGridLayoutManager(){
        mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
    }


    private void applyStaggeredGridLayoutManager(){
        mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_linear)
            applyLinearLayoutManager();
        else if (id == R.id.action_grid)
            applyGridLayoutManager();
        else if (id == R.id.action_staggered)
            applyStaggeredGridLayoutManager();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        int pos = mRecyclerView.getChildPosition(v);
        Toast.makeText(getApplicationContext(), mSampleData[pos], Toast.LENGTH_LONG).show();
    }
}



Get ready for Jack and Jill !!!

Android is working over new toolchain and it has introduced Jack and Jill available for testing with non-production versions of the apps, at the core of the new toolchain.

JACK - Java Android Compiler Kit

Jack is a new Android toolchain that comprises a compiler from Java programming language source to the Android dex file format. Jack has its own .jack library format and provides most tool chain features as part of a single tool: repackaging, shrinking, obfuscation and multidex.


JILL - Jack Intermediate Library Linker

Jill is a tool that translates existing .jar files to the .jack library format.


Working


With the new tool chain enabled, Jill will translate any libraries you are referencing to a new Jack library file (.jack). This prepares them to be quickly merged with other .jack files. The Android Gradle plugin and Jack collect any .jack library files, along with your source code, and compiles them into a set of dex files. During the process, Jack also handles any requested code minification. The output is then assembled into an APK file as normal. Support for multiple dex files is also included.



Jack using Gradle


Jack and Jill are available in Build Tools version 21.1.1, and greater, via the SDK Manager. Complementary Gradle and Android Studio support is also already available in the Android 1.0.0+ Gradle plugin.

Using Gradle, add useJack in your build config.

android {
    ...
    buildToolsRevision '21.1.1'
    defaultConfig {
      // Enable the experimental Jack build tools.
      useJack = true
    }
    ...
}


Official Blog: http://android-developers.blogspot.in/2014/12/hello-world-meet-our-new-experimental.html

Tool Doc: http://tools.android.com/tech-docs/jackandjill