Complete Solution for every problem | Mobile Technology | Web Technology | Software | Mobile Apps | Scripts | Tricks | Latest Technology | Wordpress Plugins


Line Post
Showing posts with label CardView. Show all posts
Showing posts with label CardView. Show all posts

10 October, 2015

Recycler view and Card View in Andorid Development



Recyclerview is more advanced and flexible version of Listview added in Lollipop. It also Support in lower API of Android. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.

Application Development
Recycler view

The RecyclerView class simplifies the display and handling of large data sets by providing:
·         Layout managers for positioning items
·         Default animations for common item operations, such as removal or addition of items

To  add RecyclerView in your Project. you have to add Library of Recyclerview.

If you are using Android Studio and you are Supporting lower APIs,  You just need to add Gradle URL into your Module's Gradle file.

dependencies {

compile 'com.android.support:recyclerview-v7:23.0.1'

}

Once you sync  gradle lib from google will be downloaded into your project and you can use it.

Add RecyclerView into your activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

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

Now you have added RecyclerView widget into your layout file, Obtain a handle to the object and connect it to layout manager and attach an adapter to show data.

public class MainActivity extends AppCompatActivity {



    ArrayList<String> arrayList = new ArrayList<>();



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        for (int i = 0; i < 15; i++) {

            arrayList.add(String.valueOf(i));

        }



        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);



        /**

         *To Show RecyclerView as a List of data, LayoutManager is important to set.

         *  For ListView : LinearLayoutManager is used.

         *  For GridView : GridLayoutManager is used.

         *  For Staggered GridView: StaggeredGridLayoutManager is used

         */

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);

        recyclerView.setLayoutManager(manager);



        MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(MainActivity.this, arrayList);

        recyclerView.setAdapter(myRecyclerViewAdapter);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, 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();



        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }



        return super.onOptionsItemSelected(item);

    }

}

RecyclerView Adpter :

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



    Context context;

    ArrayList<String> strings;



    /**

     * public Constructor

     */

    public MyRecyclerViewAdapter(Context context, ArrayList<String> arrayList) {



        this.context = context;

        this.strings = arrayList;

    }



    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);

        return new ViewHolder(view);

    }



    @Override

    public void onBindViewHolder(ViewHolder holder, int position) {



        holder.textView.setTag(position);

        holder.cardView.setTag(position);



        holder.textView.setText(strings.get(position));



        if (position % 2 == 0) {

            holder.cardView.setBackgroundColor(context.getResources().getColor(R.color.colorzero, null));

        } else {

            holder.cardView.setBackgroundColor(context.getResources().getColor(R.color.colorone, null));

        }



    }



    @Override

    public int getItemCount() {

        return strings.size();

    }



    public class ViewHolder extends RecyclerView.ViewHolder {



        TextView textView;

        CardView cardView;



        public ViewHolder(View itemView) {

            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.textview);

            cardView = (CardView) itemView.findViewById(R.id.card_view);

        }

    }

}

CardView :

CardView is a extension of FrameLayout which lets you show data into card. It can have Shadows and Corners.

To show shadow use "cardElevation" attribute. In lollipop devices cardview uses real elevation and dynamic shadows.

Use these properties to customize the appearance of the CardView widget:

·         To set the corner radius in your layouts, use the card_view:cardCornerRadius attribute.
·         To set the corner radius in your code, use the CardView.setRadius method.
·         To set the background color of a card, use the card_view:cardBackgroundColor attribute.  

If you are using Android Studio and you are Supporting lower APIs,  You just need to add Gradle URL into your Module's Gradle file.

dependencies { compile 'com.android.support:cardview-v7:23.0.1'}





<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    android:orientation="vertical">



    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"

        android:id="@+id/card_view"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center_vertical"

        android:layout_margin="10dp"

        android:foregroundGravity="center"

        android:minHeight="50dp"

        card_view:cardCornerRadius="4dp"

        card_view:cardElevation="3dp">



        <TextView

            android:id="@+id/textview"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_gravity="center_vertical"

            android:text="TextView"

            android:textColor="#000"

            android:textSize="25sp" />

    </android.support.v7.widget.CardView>

</LinearLayout>


To Check Full code please Go to Below Link.
Read more ...

Comments

Designed By