1) activity_main.xml
<?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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:contentScrim="?attr/colorPrimary"
app:title="Toolbar Title"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:background="@drawable/toolbar">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
2) Create card_layout.cml file on res/layout folder
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_layout"
android:orientation="vertical">
<!--Todo: CardView-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardElevation="6dp"
app:cardMaxElevation="6dp"
app:contentPadding="25dp"
app:cardBackgroundColor="@android:color/white"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/card_img"
android:padding="10dp"
android:src="@drawable/home_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/card_title"
android:text="Category Name"
android:textSize="18dp"
android:textColor="@android:color/black"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
3) Create Cards.java class file and past this code
public class Cards {
Integer Img;
String category;
public Cards(Integer img, String category) {
Img = img;
this.category = category;
}
public Integer getImg() {
return Img;
}
public String getCategory() {
return category;
}
}
4) Create CardsAdapter.java class file and past this code
/*Todo: Recyclerview Adapter*/
public class CardsAdapter extends RecyclerView.Adapter<CardsAdapter.ViewHolder>
{
/*Todo: Recyclerview Adapter class*/
List<Cards> list;
Context context;
public CardsAdapter(List<Cards> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
final ViewHolder viewHolder = new ViewHolder(view);
/*Todo: adding Click Events Of Each Menu On Dashboard by title*/
viewHolder.card_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, list.get(viewHolder.getAdapterPosition()).getCategory(), Toast.LENGTH_SHORT).show();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cards Cards = list.get(position);
final int img = Cards.getImg();
final String categiry = Cards.getCategory();
holder.card_img.setImageResource(img);
holder.card_title.setText(categiry);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView card_img;
TextView card_title;
LinearLayout card_layout;
public ViewHolder(View itemView) {
super(itemView);
card_img = (ImageView)itemView.findViewById(R.id.card_img);
card_title = (TextView) itemView.findViewById(R.id.card_title);
card_layout = (LinearLayout) itemView.findViewById(R.id.card_layout);
}
}
}
5) MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<Cards> Cardss;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(MainActivity.this,2);
recyclerView.setLayoutManager(mLayoutManager);
Cardss = new ArrayList<>();
loaddata();
}
private void loaddata()
{
String[] nameArray = {
"Home",
"Taxi",
"Boat",
"Train",
"Navigation",
"Stop"
};
int[] drawableArray = {
R.drawable.home_icon,
R.drawable.taxi_icon,
R.drawable.boat_icon,
R.drawable.train_icon,
R.drawable.navigation_icon,
R.drawable.stop_icon
};
for (int i = 0; i < nameArray.length; i++) {
Cardss.add(new Cards(
drawableArray[i],
nameArray[i]
));
}
adapter = new CardsAdapter(Cardss, MainActivity.this);
recyclerView.setAdapter(adapter);
}
No comments:
Post a Comment