我有一个recyclerview和一个edittext。除非未过滤,否则我可以从recyclerview选择正确的项目。在输入edittext时 当我从列表中选择任何项目时,Recyclerview列表将被过滤,它向我显示实际的列表项目而不是选定的项目。
我的片段类:
public class HomeFragment extends Fragment {
List<ActivityCell> lstActivity ;
public ActivityCell_RecyclerViewAdapter myAdapter;
public RecyclerView myrv;
ClockPieView clockPieView;
//Notification
private static final int NOTIFICATION_ID = 1;
private CharSequence notificationTitleText = "Hello Title";
private CharSequence notificationDescText = "This is custom Notification desc";
public long timeDifference = 0;
Chronometer chronometer;
public long pauseOffset;
LinearLayout snacklayout;
// Session Manager Class
SessionManager session;
//sessiondan alacağımız Stringlerimizi kaydedeceğimiz Stringler
String username, sifre;
String userID;
//TextViewlerimiz
TextView txt_deneme_sifre;
TextView main_head_txt;
String HeaderFirstStep;
//Fab
FloatingActionButton fabHomeBtn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
final DatabaseHelper mdb = new DatabaseHelper(getActivity());
lstActivity = new ArrayList<>();
Cursor cursor = mdb.getAction();
while (cursor.moveToNext()){
String draw_name = cursor.getString(1);
int id = getResources().getIdentifier(draw_name, "drawable",getActivity().getPackageName());
//int img_id = Integer.parseInt(cursor.getString(1));
lstActivity.add(new ActivityCell(cursor.getString(0),id,10));
}
myrv = (RecyclerView) view.findViewById(R.id.homefragment_recyclerview_id);
myAdapter = new ActivityCell_RecyclerViewAdapter(getActivity(),lstActivity);
myrv.setLayoutManager(new GridLayoutManager(getActivity(),2));
myrv.setAdapter(myAdapter);
EditText Search_edittext = (EditText) view.findViewById(R.id.Search_edittext);
Search_edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
return view;
}
private void filter(String text) {
ArrayList<ActivityCell> filteredList = new ArrayList<>();
for (ActivityCell item : lstActivity) {
if (item.getTitle().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
myAdapter.filterList(filteredList);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
snacklayout=(LinearLayout) getActivity().findViewById(R.id.home_Linear_layout);
//TextView tanımlamaları
main_head_txt = (TextView) getActivity().findViewById(R.id.main_head_txt);
txt_deneme_sifre = (TextView) getActivity().findViewById(R.id.txt_deneme_sifre);
final DatabaseHelper mdb1 = new DatabaseHelper(getActivity());
session = new SessionManager(getActivity());
// sessiondan kullanıcı verilerini almak için nesnemizi oluşturduk.
HashMap<String, String> user = session.getUserDetails();
//keylerine göre user nesnemizden verilerimizi çağırdık ve ekledik.
try {
userID = user.get(SessionManager.KEY_USERID);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
mdb1.getWritableDatabase();
Cursor cursor = mdb1.getProfile(userID);
while (cursor.moveToNext()){
String firstname = cursor.getString(0);
setHeaderMessage();
main_head_txt.setText(HeaderFirstStep+" "+ firstname);
}
我的适配器类
public class ActivityCell_RecyclerViewAdapter extends RecyclerView.Adapter<ActivityCell_RecyclerViewAdapter.MyViewHolder> {
private Context mContext ;
private List<ActivityCell> mData ;
public int activity_state = 0;
public LinearLayout snacklayout;
public String activity_name;
private android.support.v4.app.NotificationCompat.Builder builder;
private NotificationManager notificationManager;
private int notification_id=0;
private RemoteViews remoteViews;
private Context context;
PendingIntent pendingIntent;
// Session Manager Class
SessionManager session;
//sessiondan alacağımız Stringlerimizi kaydedeceğimiz Stringler
String userID;
public ActivityCell_RecyclerViewAdapter(Context mContext, List<ActivityCell> mData) {
this.mContext = mContext;
this.context=mContext;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_item_activity,parent,false);
return new MyViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final DatabaseHelper mdb = new DatabaseHelper(mContext);
mdb.getWritableDatabase();
final boolean[] isChronometerRunning = new boolean[100];
// session verileri için sınıfımızı çağırdık
session = new SessionManager(mContext.getApplicationContext());
// sessiondan kullanıcı verilerini almak için nesnemizi oluşturduk.
HashMap<String, String> user = session.getUserDetails();
//keylerine göre user nesnemizden verilerimizi çağırdık ve ekledik.
userID = user.get(SessionManager.KEY_USERID);
holder.activity_chr_id.setTextColor(mContext.getColor(R.color.white));
holder.activity_title_id.setText(mData.get(position).getTitle());
holder.activity_chr_id.setBase(elapsedRealtime());
holder.cardview_activity_img_id.setImageResource(mData.get(position).getThumbnail());
isChronometerRunning[position]= false;
Cursor cursor = mdb.getActionColor(mData.get(position).getTitle());
while (cursor.moveToNext()){
String color_icon = cursor.getString(0);
holder.cardview_activity_img_id.setColorFilter(Color.parseColor(color_icon));
}
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HomeFragment fragment=new HomeFragment();
//! holder.activity_chr_id.getText().toString().equals("00:00")
if ( isChronometerRunning[position] ){
activity_name=holder.activity_title_id.getText().toString();
holder.activity_chr_id.stop();
mdb.updateFinishDate(activity_name);
holder.activity_chr_id.setBase(elapsedRealtime());
holder.activity_chr_id.setTextColor(Color.parseColor("#ffffff"));
Cursor cursor = mdb.getActionColor(mData.get(position).getTitle());
while (cursor.moveToNext()){
String color_icon = cursor.getString(0);
holder.cardview_activity_img_id.setColorFilter(Color.parseColor(color_icon));
}
holder.cardView.setCardBackgroundColor(Color.parseColor("#ffffff"));
holder.activity_title_id.setTextColor(Color.parseColor("#2d2d2d"));
activity_state=activity_state-1;
isChronometerRunning[position]= false;
notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
builder = new android.support.v4.app.NotificationCompat.Builder(context);
Intent button_intent = new Intent("button_clicker");
button_intent.putExtra("idd", notification_id);
PendingIntent button_pending_event = PendingIntent.getBroadcast(context, notification_id,
button_intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button, button_pending_event);
Intent notification_intent = new Intent(context, Home.class);
pendingIntent = PendingIntent.getActivity(context, 0, notification_intent, 0);
notificationManager.cancel(position);
}
else if (activity_state <3 && ! isChronometerRunning[position] ){
//fragment.ShowNotification();
activity_name=holder.activity_title_id.getText().toString();
mdb.addData(activity_name,Integer.parseInt(userID.toString()));
holder.activity_chr_id.setBase(elapsedRealtime());
holder.activity_chr_id.start();
holder.cardview_activity_img_id.setColorFilter(Color.parseColor("#ffffff"));
holder.activity_chr_id.setTextColor(Color.parseColor("#ffffff"));
holder.cardView.setCardBackgroundColor(Color.parseColor(BackgroundColorSet(activity_name)));
holder.activity_title_id.setTextColor(Color.parseColor("#ffffff"));
boolean deneme2 = holder.activity_chr_id.isActivated();
isChronometerRunning[position]= true;
notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
builder = new android.support.v4.app.NotificationCompat.Builder(context);
remoteViews = new RemoteViews(context.getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon, mData.get(position).getThumbnail());
remoteViews.setChronometer(R.id.notif_Chronometer, SystemClock.elapsedRealtime(),
null, true); //pausing
remoteViews.setTextViewText(R.id.notif_title,holder.activity_title_id.getText().toString());
notification_id = (int) position;
builder.setSmallIcon(R.drawable.login_logo)
.setOngoing(true)
.setCustomContentView(remoteViews)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent);
ButtonListener test = new ButtonListener();
Intent button_intent = new Intent("button_clicker");
button_intent.putExtra("idd", notification_id);
PendingIntent button_pending_event = PendingIntent.getBroadcast(context, notification_id,
button_intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button, button_pending_event);
Intent notification_intent = new Intent(context, Home.class);
notification_intent.putExtra("idd", notification_id);
pendingIntent = PendingIntent.getActivity(context, 0, notification_intent, 0);
notificationManager.notify(notification_id, builder.build());
activity_state++;
}
else
{
Snackbar.make(v,"Aynı Anda 3 ten fazla etkinlik oluşturulamaz", Snackbar.LENGTH_LONG).show();
}
}
});
}
public String BackgroundColorSet(String activity_name)
{
final DatabaseHelper mdb = new DatabaseHelper(mContext);
mdb.getWritableDatabase();
String BackgroundColorname = "";
Cursor cursor = mdb.getActionColor(activity_name);
while (cursor.moveToNext()){
BackgroundColorname = cursor.getString(0);
}
return BackgroundColorname;
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView activity_title_id;
ImageView cardview_activity_img_id;
CardView cardView ;
Chronometer activity_chr_id;
public MyViewHolder(View itemView) {
super(itemView);
activity_title_id = (TextView) itemView.findViewById(R.id.activity_title_id) ;
cardview_activity_img_id = (ImageView) itemView.findViewById(R.id.cardview_activity_img_id);
cardView = (CardView) itemView.findViewById(R.id.item_cardview_id);
activity_chr_id = (Chronometer) itemView.findViewById(R.id.activity_chr_id);
}
}
public void filterList(ArrayList<ActivityCell> filteredList) {
mData = filteredList;
notifyDataSetChanged();
}
}
这是我的示例图片
答案 0 :(得分:0)
您的问题与此问题类似,RecyclerView item background alternating color out of order
“这是因为RecyclerView回收了旧视图以创建新视图。当向下滚动旧视图以创建新视图时,由于调用onBindViewHolder时已经设置了这些旧视图的背景色,在onBindViewHolder中指定else条件,它将改变颜色并维持交替的颜色顺序。”
可能ActivityCell
从mData.get(position)
返回,所以我的建议是,向您的ActivityCell对象名称ishighlighted / isSelected添加一个布尔字段,因此单击isSelected = true
时,最重要的是,当您绑定项目时,如果isSelected为true,则在单元格上应用突出显示/单击效果。