1) Card.xml
<?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"
android:orientation="vertical"
android:background="@drawable/card_bg"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/card_list">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/card_image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_name"
android:text="Name"
android:textColor="@color/colorAccent"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_email"/>
</LinearLayout>
2) custom diloag.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:layout_margin="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
android:textSize="35sp"
android:id="@+id/name"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close"
android:id="@+id/btn_close"/>
</LinearLayout>
</ScrollView><?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:layout_margin="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
android:textSize="35sp"
android:id="@+id/name"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close"
android:id="@+id/btn_close"/>
</LinearLayout>
</ScrollView>
3) MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
4) MainActivity.java
public class MainActivity extends AppCompatActivity {
Integer[] imageId = new Integer[]{
R.drawable.robot,
R.drawable.krish,
R.drawable.batman,
R.drawable.ironman,
R.drawable.doc_strange,
R.drawable.hulk,
R.drawable.thor,
R.drawable.mijas};
String[] title = new String[]{
"Robot 2.0",
"Krish",
"Batman",
"Ironman",
"Doctor Strange",
"Hulk",
"Thor",
"Mijas"
};
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,1);
recyclerView.setLayoutManager(mLayoutManager);
Cardss = new ArrayList<>();
load_data();
}
private void load_data() {
for (int i = 0; i <imageId.length; i++) {
Cardss.add(new Cards(
imageId[i],
title[i],
title[i] + "@test.com"
));
}
//adapter.notifyDataSetChanged();
adapter = new Adapter(Cardss, MainActivity.this);
recyclerView.setAdapter(adapter);
}
}
5) Cards.class
public class Cards {
int img;
String name;
String email;
public Cards(int img, String name, String email) {
this.img = img;
this.name = name;
this.email = email;
}
public int getImg() {
return img;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
6) Adapter.java
class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
List<Cards> list;
Context context;
Dialog dialog;
public Adapter(List<Cards> list, Context context) {
this.list = list;
this.context = context;
dialog = new Dialog(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card, viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(view);
dialog.setContentView(R.layout.custom_dialog);
viewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView img = (ImageView) dialog.findViewById(R.id.img);
TextView name = (TextView) dialog.findViewById(R.id.name);
Button btn_close = (Button) dialog.findViewById(R.id.btn_close);
img.setImageResource(list.get(viewHolder.getAdapterPosition()).getImg());
name.setText(list.get(viewHolder.getAdapterPosition()).getName());
dialog.show();
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
Cards card_post = list.get(i);
final int img = card_post.getImg();
final String content = card_post.getName();
final String count_like = card_post.getEmail();
viewHolder.card_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, viewHolder.card_list);
popup.inflate(R.menu.option_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
Toast.makeText(context, "Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(context, "Menu 2", Toast.LENGTH_SHORT).show();
break;
case R.id.menu3:
Toast.makeText(context, "Menu 3", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
popup.show();
}
});
viewHolder.post_image.setImageResource(img);
viewHolder.name.setText(content);
viewHolder.email.setText(count_like);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView post_image;
TextView name, email;
LinearLayout card_list;
public ViewHolder(View itemView) {
super(itemView);
card_list = (LinearLayout) itemView.findViewById(R.id.card_list);
post_image = (ImageView) itemView.findViewById(R.id.card_image);
name = (TextView)itemView.findViewById(R.id.card_name);
email = (TextView)itemView.findViewById(R.id.card_email);
}
}
}
No comments:
Post a Comment