在 RecyclerAdapter 中单击按钮时增加徽章值
Increment a badge value when button clicked in RecyclerAdapter
我正在开发一个应用程序,它允许用户通过单击 AlertDialog 的 "OK" 按钮来增加通知标记的值。该值将存储在 MySQL 数据库中。
AlertDialog 在 RecyclerAdapter 中设置,通知 Badge 在 Main activity 中设置,我使用 Utils class 作为 Badge 计数。
请问最好的实现方式是什么?
更新
计数器class
public interface Counter {
public void setCounter(int count);}
适配器class:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
Context c;
ArrayList<criteria> critere;
private Counter counter;
public RecyclerAdapter(Context c, ArrayList<criteria> critere, Counter counter) {
this.c = c;
this.critere = critere;
this.counter = counter;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Integer count;
TextView criteria, update;
ImageButton btn1, btn2, btn3, btn4;
Boolean click1, click2, click3, click4;
ImageView image;
RelativeLayout layout;
LinearLayout ranked;
ImageView check;
private ItemClickListener itemClickListener;
public ClipData.Item currentItem;
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view = itemView;
btn1 = itemView.findViewById(R.id.btn1);
btn2 = itemView.findViewById(R.id.btn2);
btn3 = itemView.findViewById(R.id.btn3);
btn4 = itemView.findViewById(R.id.btn4);
layout = itemView.findViewById(R.id.check_view);
ranked = itemView.findViewById(R.id.rank_layout);
check = itemView.findViewById(R.id.check_image);
criteria = itemView.findViewById(R.id.criteria_text);
image = itemView.findViewById(R.id.criteria_pic);
}
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.recycler_model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final criteria crit = critere.get(position);
final String name = crit.getName();
//BIND
holder.criteria.setText(name);
holder.layout.setVisibility(View.GONE);
PicassoClient.downloadImage(c,crit.getImageurl(),holder.image);
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.custom_dialog_rank, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setView(dialogView);
dialog.setIcon(R.drawable.btn_press_rank1);
dialog.setCancelable(false);
dialog.setTitle(R.string.crit1);
dialog.setMessage("The " + name + " was very unsatisfiying");
final EditText userInput = (EditText) dialogView
.findViewById(R.id.comment_field);
dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//checked.setVisibility(View.VISIBLE);
holder.layout.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.bringToFront();
holder.ranked.setVisibility(View.INVISIBLE);
counter.setCounter(1);
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//checked.setVisibility(View.GONE);
holder.layout.setVisibility(View.GONE);
holder.ranked.setVisibility(View.VISIBLE);
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
}
@Override
public int getItemCount() {
return critere.size();
}}
主要活动class:
public class RankActivity extends AppCompatActivity {
String jsonURL="xx/criteria.php";
RecyclerView rv;
private int mNotificationsCount = 0;
private Counter counter;
TextView notif;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
notif = (TextView) findViewById(R.id.badge_notification_1);
new RankActivity.FetchCountTask().execute();
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
notif.setText(count + "");
Toast.makeText(RankActivity.this, "Called", Toast.LENGTH_SHORT).show();
}
};
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(RankActivity.this, 2));
new Downloader_recycler(RankActivity.this,jsonURL, rv, counter).execute();
}
public void OnSave(MenuItem item){
Toast.makeText(this, "Saved !", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
MenuItemCompat.setActionView(item, R.layout.notification_update);
//notificationCount = (RelativeLayout) MenuItemCompat.getActionView(item);
//LayerDrawable icon = (LayerDrawable) item.getIcon();
//Utils.setBadgeCount(this, icon, mNotificationsCount);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_notifications) {
// TODO: display unread notifications.
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateNotificationsBadge(int count) {
mNotificationsCount = count;
invalidateOptionsMenu();
}
class FetchCountTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
return 5;
}
@Override
public void onPostExecute(Integer count) {
updateNotificationsBadge(count);
}
}}
按照这个步骤
1.create一个interface
像这样
public interface Counter
{
public void setCounter(int count);
}
2.than 在您的 Activity
中添加此代码
private Counter counter;
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
tvCountFav.setText(count + "");
Toast.makeText(SearchResultClass.this, "Called", Toast.LENGTH_SHORT).show();
}
};
3.pass 这个 interface
作为适配器的参数 class 像这样
YourAdapteradapter = new YourAdapter(SearchResultClass.this, ArrayList, counter);
4.add counter
适配器中的代码 class 构造函数
private Counter counter;
public SearchResultAdapter(Context context, ArrayList<FilterDataModel> filterDataModelArrayList, Counter counter) {
this.context = context;
this.filterDataModelArrayList = filterDataModelArrayList;
this.counter = counter;
}
5.fire 侧面点击侦听器中的计数器接口方法像这样
holder.addLikeImagevView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter.setCounter(1);
}
});
我就是这样修复的(没有界面)
在主体 activity 中,我更改了实现徽章的方式并添加了两个方法:
public void update() {
if (count>=5)
{count = count +1;}
display(count);
}
和
private void display(int count) {
notif = (TextView) findViewById(R.id.badge_notification_1);
notif.setText(""+ count);
}
这是新的 Main Activity class :
public class RankActivity extends AppCompatActivity {
String jsonURL="http://xx/load_criteria.php";
RecyclerView rv;
TextView notif;
int count = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(RankActivity.this, 2));
new Downloader_recycler(RankActivity.this,jsonURL, rv).execute();
}
public void OnSave(MenuItem item){
Toast.makeText(this, "Saved !", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
MenuItemCompat.setActionView(item, R.layout.notification_update);
//notificationCount = (RelativeLayout) MenuItemCompat.getActionView(item);
//LayerDrawable icon = (LayerDrawable) item.getIcon();
//Utils.setBadgeCount(this, icon, mNotificationsCount);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_notifications) {
// TODO: display unread notifications.
return true;
}
return super.onOptionsItemSelected(item);
}
public void update() {
if (count>=5)
{count = count +1;}
display(count);
}
private void display(int count) {
notif = (TextView) findViewById(R.id.badge_notification_1);
notif.setText(""+ count);
}}
在适配器 class 中,我在 Onclick 方法中添加了这些行:
if(c instanceof RankActivity){
((RankActivity)c).update();
}
我从其他帖子了解到,在这里强制转换 class 是为了避免因为它可能导致运行时异常,但它对我有用。
这是新的适配器 class :
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
Context c;
ArrayList<criteria> critere;
public RecyclerAdapter(Context c, ArrayList<criteria> critere) {
this.c = c;
this.critere = critere;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView criteria, plus, update;
ImageButton btn1, btn2, btn3, btn4;
Boolean click1, click2, click3, click4;
ImageView image, crown;
RelativeLayout layout;
LinearLayout ranked;
ImageView check;
private ItemClickListener itemClickListener;
public ClipData.Item currentItem;
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view = itemView;
btn1 = itemView.findViewById(R.id.btn1);
btn2 = itemView.findViewById(R.id.btn2);
btn3 = itemView.findViewById(R.id.btn3);
btn4 = itemView.findViewById(R.id.btn4);
layout = itemView.findViewById(R.id.check_view);
ranked = itemView.findViewById(R.id.rank_layout);
check = itemView.findViewById(R.id.check_image);
criteria = itemView.findViewById(R.id.criteria_text);
image = itemView.findViewById(R.id.criteria_pic);
crown = itemView.findViewById(R.id.crown_rank);
plus = itemView.findViewById(R.id.plus);
}
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.recycler_model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final criteria crit = critere.get(position);
final String name = crit.getName();
holder.criteria.setText(name);
holder.layout.setVisibility(View.GONE);
holder.crown.setVisibility(View.GONE);
holder.plus.setVisibility(View.GONE);
PicassoClient.downloadImage(c,crit.getImageurl(),holder.image);
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.custom_dialog_rank, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setView(dialogView);
dialog.setIcon(R.drawable.btn_press_rank1);
dialog.setCancelable(false);
dialog.setTitle(R.string.crit1);
dialog.setMessage("The " + name + " was very unsatisfiying");
final EditText userInput = (EditText) dialogView
.findViewById(R.id.comment_field);
dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//checked.setVisibility(View.VISIBLE);
holder.layout.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.bringToFront();
holder.ranked.setVisibility(View.INVISIBLE);
holder.crown.setVisibility(View.VISIBLE);
holder.plus.setVisibility(View.VISIBLE);
if(c instanceof RankActivity){
((RankActivity)c).update();
}
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//checked.setVisibility(View.GONE);
holder.layout.setVisibility(View.GONE);
holder.ranked.setVisibility(View.VISIBLE);
holder.crown.setVisibility(View.GONE);
holder.plus.setVisibility(View.GONE);
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
}
@Override
public int getItemCount() {
return critere.size();
}}
对于徽章:
Menu_user布局
在菜单文件夹中:
<menu 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"
tools:context="com.journaldev.collapsingtoolbarlayout.ScrollingActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Rank"
android:onClick="OnSave"
app:showAsAction="always" />
<!--android:icon="@drawable/ic_ok"-->
<item
android:id="@+id/action_notifications"
android:orderInCategory="200"
android:icon="@drawable/main_menu_notification"
android:title="Notifications"
app:showAsAction="always" />
</menu>
Notification_update布局
在布局文件夹中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
>
<RelativeLayout
android:id="@+id/badge_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/relative_layout_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="@drawable/crown1" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/relative_layout_item_count"
android:background="@android:drawable/ic_notification_overlay"
android:text="5"
android:textColor="#FFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
希望对其他人有所帮助。
我正在开发一个应用程序,它允许用户通过单击 AlertDialog 的 "OK" 按钮来增加通知标记的值。该值将存储在 MySQL 数据库中。
AlertDialog 在 RecyclerAdapter 中设置,通知 Badge 在 Main activity 中设置,我使用 Utils class 作为 Badge 计数。
请问最好的实现方式是什么?
更新
计数器class
public interface Counter {
public void setCounter(int count);}
适配器class:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
Context c;
ArrayList<criteria> critere;
private Counter counter;
public RecyclerAdapter(Context c, ArrayList<criteria> critere, Counter counter) {
this.c = c;
this.critere = critere;
this.counter = counter;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Integer count;
TextView criteria, update;
ImageButton btn1, btn2, btn3, btn4;
Boolean click1, click2, click3, click4;
ImageView image;
RelativeLayout layout;
LinearLayout ranked;
ImageView check;
private ItemClickListener itemClickListener;
public ClipData.Item currentItem;
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view = itemView;
btn1 = itemView.findViewById(R.id.btn1);
btn2 = itemView.findViewById(R.id.btn2);
btn3 = itemView.findViewById(R.id.btn3);
btn4 = itemView.findViewById(R.id.btn4);
layout = itemView.findViewById(R.id.check_view);
ranked = itemView.findViewById(R.id.rank_layout);
check = itemView.findViewById(R.id.check_image);
criteria = itemView.findViewById(R.id.criteria_text);
image = itemView.findViewById(R.id.criteria_pic);
}
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.recycler_model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final criteria crit = critere.get(position);
final String name = crit.getName();
//BIND
holder.criteria.setText(name);
holder.layout.setVisibility(View.GONE);
PicassoClient.downloadImage(c,crit.getImageurl(),holder.image);
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.custom_dialog_rank, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setView(dialogView);
dialog.setIcon(R.drawable.btn_press_rank1);
dialog.setCancelable(false);
dialog.setTitle(R.string.crit1);
dialog.setMessage("The " + name + " was very unsatisfiying");
final EditText userInput = (EditText) dialogView
.findViewById(R.id.comment_field);
dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//checked.setVisibility(View.VISIBLE);
holder.layout.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.bringToFront();
holder.ranked.setVisibility(View.INVISIBLE);
counter.setCounter(1);
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//checked.setVisibility(View.GONE);
holder.layout.setVisibility(View.GONE);
holder.ranked.setVisibility(View.VISIBLE);
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
}
@Override
public int getItemCount() {
return critere.size();
}}
主要活动class:
public class RankActivity extends AppCompatActivity {
String jsonURL="xx/criteria.php";
RecyclerView rv;
private int mNotificationsCount = 0;
private Counter counter;
TextView notif;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
notif = (TextView) findViewById(R.id.badge_notification_1);
new RankActivity.FetchCountTask().execute();
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
notif.setText(count + "");
Toast.makeText(RankActivity.this, "Called", Toast.LENGTH_SHORT).show();
}
};
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(RankActivity.this, 2));
new Downloader_recycler(RankActivity.this,jsonURL, rv, counter).execute();
}
public void OnSave(MenuItem item){
Toast.makeText(this, "Saved !", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
MenuItemCompat.setActionView(item, R.layout.notification_update);
//notificationCount = (RelativeLayout) MenuItemCompat.getActionView(item);
//LayerDrawable icon = (LayerDrawable) item.getIcon();
//Utils.setBadgeCount(this, icon, mNotificationsCount);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_notifications) {
// TODO: display unread notifications.
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateNotificationsBadge(int count) {
mNotificationsCount = count;
invalidateOptionsMenu();
}
class FetchCountTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
return 5;
}
@Override
public void onPostExecute(Integer count) {
updateNotificationsBadge(count);
}
}}
按照这个步骤
1.create一个interface
像这样
public interface Counter
{
public void setCounter(int count);
}
2.than 在您的 Activity
中添加此代码private Counter counter;
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
tvCountFav.setText(count + "");
Toast.makeText(SearchResultClass.this, "Called", Toast.LENGTH_SHORT).show();
}
};
3.pass 这个 interface
作为适配器的参数 class 像这样
YourAdapteradapter = new YourAdapter(SearchResultClass.this, ArrayList, counter);
4.add counter
适配器中的代码 class 构造函数
private Counter counter;
public SearchResultAdapter(Context context, ArrayList<FilterDataModel> filterDataModelArrayList, Counter counter) {
this.context = context;
this.filterDataModelArrayList = filterDataModelArrayList;
this.counter = counter;
}
5.fire 侧面点击侦听器中的计数器接口方法像这样
holder.addLikeImagevView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter.setCounter(1);
}
});
我就是这样修复的(没有界面)
在主体 activity 中,我更改了实现徽章的方式并添加了两个方法:
public void update() {
if (count>=5)
{count = count +1;}
display(count);
}
和
private void display(int count) {
notif = (TextView) findViewById(R.id.badge_notification_1);
notif.setText(""+ count);
}
这是新的 Main Activity class :
public class RankActivity extends AppCompatActivity {
String jsonURL="http://xx/load_criteria.php";
RecyclerView rv;
TextView notif;
int count = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(RankActivity.this, 2));
new Downloader_recycler(RankActivity.this,jsonURL, rv).execute();
}
public void OnSave(MenuItem item){
Toast.makeText(this, "Saved !", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
MenuItemCompat.setActionView(item, R.layout.notification_update);
//notificationCount = (RelativeLayout) MenuItemCompat.getActionView(item);
//LayerDrawable icon = (LayerDrawable) item.getIcon();
//Utils.setBadgeCount(this, icon, mNotificationsCount);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_notifications) {
// TODO: display unread notifications.
return true;
}
return super.onOptionsItemSelected(item);
}
public void update() {
if (count>=5)
{count = count +1;}
display(count);
}
private void display(int count) {
notif = (TextView) findViewById(R.id.badge_notification_1);
notif.setText(""+ count);
}}
在适配器 class 中,我在 Onclick 方法中添加了这些行:
if(c instanceof RankActivity){
((RankActivity)c).update();
}
我从其他帖子了解到,在这里强制转换 class 是为了避免因为它可能导致运行时异常,但它对我有用。
这是新的适配器 class :
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
Context c;
ArrayList<criteria> critere;
public RecyclerAdapter(Context c, ArrayList<criteria> critere) {
this.c = c;
this.critere = critere;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView criteria, plus, update;
ImageButton btn1, btn2, btn3, btn4;
Boolean click1, click2, click3, click4;
ImageView image, crown;
RelativeLayout layout;
LinearLayout ranked;
ImageView check;
private ItemClickListener itemClickListener;
public ClipData.Item currentItem;
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view = itemView;
btn1 = itemView.findViewById(R.id.btn1);
btn2 = itemView.findViewById(R.id.btn2);
btn3 = itemView.findViewById(R.id.btn3);
btn4 = itemView.findViewById(R.id.btn4);
layout = itemView.findViewById(R.id.check_view);
ranked = itemView.findViewById(R.id.rank_layout);
check = itemView.findViewById(R.id.check_image);
criteria = itemView.findViewById(R.id.criteria_text);
image = itemView.findViewById(R.id.criteria_pic);
crown = itemView.findViewById(R.id.crown_rank);
plus = itemView.findViewById(R.id.plus);
}
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.recycler_model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final criteria crit = critere.get(position);
final String name = crit.getName();
holder.criteria.setText(name);
holder.layout.setVisibility(View.GONE);
holder.crown.setVisibility(View.GONE);
holder.plus.setVisibility(View.GONE);
PicassoClient.downloadImage(c,crit.getImageurl(),holder.image);
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.custom_dialog_rank, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setView(dialogView);
dialog.setIcon(R.drawable.btn_press_rank1);
dialog.setCancelable(false);
dialog.setTitle(R.string.crit1);
dialog.setMessage("The " + name + " was very unsatisfiying");
final EditText userInput = (EditText) dialogView
.findViewById(R.id.comment_field);
dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//checked.setVisibility(View.VISIBLE);
holder.layout.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.bringToFront();
holder.ranked.setVisibility(View.INVISIBLE);
holder.crown.setVisibility(View.VISIBLE);
holder.plus.setVisibility(View.VISIBLE);
if(c instanceof RankActivity){
((RankActivity)c).update();
}
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//checked.setVisibility(View.GONE);
holder.layout.setVisibility(View.GONE);
holder.ranked.setVisibility(View.VISIBLE);
holder.crown.setVisibility(View.GONE);
holder.plus.setVisibility(View.GONE);
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
}
@Override
public int getItemCount() {
return critere.size();
}}
对于徽章:
Menu_user布局 在菜单文件夹中:
<menu 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"
tools:context="com.journaldev.collapsingtoolbarlayout.ScrollingActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Rank"
android:onClick="OnSave"
app:showAsAction="always" />
<!--android:icon="@drawable/ic_ok"-->
<item
android:id="@+id/action_notifications"
android:orderInCategory="200"
android:icon="@drawable/main_menu_notification"
android:title="Notifications"
app:showAsAction="always" />
</menu>
Notification_update布局 在布局文件夹中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
>
<RelativeLayout
android:id="@+id/badge_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/relative_layout_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="@drawable/crown1" />
</RelativeLayout>
<TextView
android:id="@+id/badge_notification_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/relative_layout_item_count"
android:background="@android:drawable/ic_notification_overlay"
android:text="5"
android:textColor="#FFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
希望对其他人有所帮助。