Search This Blog

Loading...
Support my cause by downloading

Tuesday, 31 March 2015

RecyclerView example, Source code with onItemClickListener 

What is RecyclerView?


Android official site states “The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views ”

RecyclerView is generally used when elements changes at run time on user action or network events

You will need android-support-v7-recyclerview.jar for RecyclerView to integrate in your projects. This article will explain you a way to implement onItemClickListener in RecyclerView.

Lets implement RecyclerView steps by steps. Source code  is here in case you may want to skip theory or unable to find android-support-v7-recyclerview.jar

Steps 1- Create android xml layouts

 <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/idRecyclerView"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.recyclerview.MainActivity" >  
 </android.support.v7.widget.RecyclerView>  

Steps 2- Create Activity and RecyclerView.Adapter

MainActivity.java

 package com.recyclerview;  
 import android.os.Bundle;  
 import android.support.v7.app.ActionBarActivity;  
 import android.support.v7.widget.LinearLayoutManager;  
 import android.support.v7.widget.RecyclerView;  
 import android.widget.Toast;  
 public class MainActivity extends ActionBarActivity implements OnItemRecycleViewClickListener {  
      RecyclerView mRecyclerView;  
      private String[] mData = { "Tofeeq AHmad", "Recycler View", "Layout managers for positioning items", "Default animations for common", "Tofeeq AHmad", "Recycler View",  
                "Layout managers for positioning items", "Default animations for common", "Tofeeq AHmad", "Recycler View", "Layout managers for positioning items",  
                "Default animations for common" };  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           mRecyclerView = (RecyclerView) findViewById(R.id.idRecyclerView);  
           LinearLayoutManager mLinearManager = new LinearLayoutManager(this);  
           mRecyclerView.setLayoutManager(mLinearManager);  
           //          StaggeredGridLayoutManager mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);  
           //          mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);  
           //  
           //          GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);  
           //          mRecyclerView.setLayoutManager(mGridLayoutManager);  
           mRecyclerView.setHasFixedSize(true);  
           mRecyclerView.setAdapter(new RecyclerAdapter(mData, this));  
      }  
      @Override  
      public void onItemClicked(int position, RecyclerAdapter mAdapter) {  
           Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();  
      }  
 }  

RecyclerAdapter.java
 package com.recyclerview;  
 import android.support.v7.widget.RecyclerView;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {  
      private String[] mDataset;  
      OnItemRecycleViewClickListener mOnItemRecycleViewClickListener;  
      public RecyclerAdapter(String[] myDataset, OnItemRecycleViewClickListener mOnItemRecycleViewClickListener) {  
           mDataset = myDataset;  
           this.mOnItemRecycleViewClickListener = mOnItemRecycleViewClickListener;  
      }  
      public static class ViewHolder extends RecyclerView.ViewHolder {  
           public TextView mTextView;  
           public ImageView mImageView;  
           public ViewHolder(View v) {  
                super(v);  
                mTextView = (TextView) v.findViewById(R.id.txt1);  
                mImageView = (ImageView) v.findViewById(R.id.imageView);  
           }  
      }  
      @Override  
      public int getItemCount() {  
           return mDataset.length;  
      }  
      @Override  
      public void onBindViewHolder(ViewHolder arg0, int arg1) {  
           arg0.mTextView.setText(mDataset[arg1]);  
           arg0.itemView.setTag(arg1);  
           arg0.itemView.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     mOnItemRecycleViewClickListener.onItemClicked(Integer.parseInt(v.getTag().toString()), RecyclerAdapter.this);  
                }  
           });  
      }  
      @Override  
      public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {  
           View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);  
           ViewHolder vh = new ViewHolder(v);  
           return vh;  
      }  
 }  


Steps 3- To Implment onItemClickListener create one interface and attach it with adapter. RecyclerAdapter adapter already have this attach with it in step 2.

 package com.recyclerview;  
 public interface OnItemRecycleViewClickListener {  
      public void onItemClicked(int position, RecyclerAdapter mAdapter);  
 }  

Wednesday, 25 February 2015

Motorola surprise with moto e 2nd generation with android lollipop and 4G LTE

After giving high end phone like Nexus 6, Moto X 2nd generation and low end phone like Moto E and Moto G, Motorola come up with surprising budget phone Moto E second generation.Motorola delivered the new Moto E smart phone to some select media organizations overseas. It was  previously announced that Motorola would hand-deliver a box to members of the press although it wasn't clear what would be in the box.

Sepcification


The second generation Moto E has a 4G LTE speed, a 4.5-inch qHD display with 540×960 245 ppi IPS resolution and Corning Gorilla Glass 3,1.2GH quad-core 64-bit Qualcomm Snapdragon 410 processor and a 400MHz Adreno 306 GPU. It comes with 8GB storage space and 1GB of RAM. Moto E 2nd  has a VGA camera in the front and a 5 megapixel rear camera with autofocus and 720p video recording capabilities. The storage space can be expanded via a MicroSD slot with 32GB limit. Other connectivity options include Wi-Fi, Bluetooth 4.0LE, GPS/ A-GPS, GLONASS, FM radio, and Micro-USB. Phone loaded with  2390 mAh battery and come with customizable back cover with lot of option.

Starting today, the new Moto E will begin rolling out in more than 50 countries in North America, Latin America, Europe and Asia. It will be available unlocked in the U.S. at www.motorola.com for $119.99 (3G) and $149.99 (LTE). 


Android News and source code