I have set up a recyclerview, in which there are there views, Countryname, currency, and checkbox. I dont need to provide dataset for checkboxes because i can get the corresponding countryname anyway when they are clicked. But the problem is, i have implemented a searchview, and it works fine. But whey i check the corresponding checkbox, it checks that box at positioned relative to TOP.
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.ViewHolder> {
Context mcontext;
Data dataclass;
List<alldata> Rclist=new ArrayList<>();
private int lastCheckedPosition = -1;
List<String> countries= new ArrayList<>();
RecyclerViewAdapter.ViewHolder HOLDER;
View itemV;
int pos;
public RecyclerViewAdapter(List<alldata> list, Context context) {
this.Rclist= list;
this.mcontext=context;
for (int i=0; i<list.size(); i++) {
countries.add(list.get(i).country);
}
}
@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_row,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerViewAdapter.ViewHolder holder, final int position) {
holder.country.setText(Rclist.get(position).country);
holder.currency.setText(Rclist.get(position).currency);
holder.checkBox.setChecked(position == lastCheckedPosition);
this.HOLDER=holder;
this.pos=position;
}
@Override
public int getItemCount() {
return Rclist.size();
}
public void updatelist(List<alldata> newList) {
lastCheckedPosition = HOLDER.getAdapterPosition();
notifyDataSetChanged();
Rclist=new ArrayList<>();
Rclist.addAll(newList);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView country;
TextView currency;
CheckBox checkBox;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
country=itemView.findViewById(R.id.country);
currency=itemView.findViewById(R.id.currency);
checkBox=itemView.findViewById(R.id.checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemV=itemView;
lastCheckedPosition = getAdapterPosition();
//because of this blinking problem occurs so
//i have a suggestion to add notifyDataSetChanged();
// notifyItemRangeChanged(0, list.length);//blink list problem
notifyDataSetChanged();
CharSequence text = currency.getText() + " is now the selected currency";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(v.getContext(), text, duration);
toast.show();
}
});
}
}
// in activity
public class selectcurrency extends AppCompatActivity implements SearchView.OnQueryTextListener {
List<String> countries= new ArrayList<>();
private static String TAG="";
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
List<alldata> list=new ArrayList<>();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.dashboard, menu);
MenuItem searchmenu= menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchmenu.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcurrency);
// sharedPref = this.getSharedPreferences( MY_PREFS_NAME, Context.MODE_PRIVATE); editor = sharedPref.edit();
Data obj=new Data();
list=obj.getData();
Collections.sort(list, new Comparator<alldata>() {
@Override
public int compare(alldata o1, alldata o2) {
return o1.country.compareTo(o2.country);
}
});
for (int i=0; i<list.size(); i++) {
countries.add(list.get(i).country);
}
initRecyclerView(list,this);
}
private void initRecyclerView(List<alldata> list, Context context1) {
List<alldata> List=list;
Context context=context1;
recyclerView = findViewById(R.id.recyclerView);
adapter = new RecyclerViewAdapter(List ,context);
recyclerView.setAdapter(adapter);
LinearLayoutManager gridLayoutManager= new LinearLayoutManager(context);
recyclerView.setLayoutManager(gridLayoutManager);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String userinput= newText.toLowerCase();
List<alldata> newList=new ArrayList<>();
for (String country: countries) {
if(country.toLowerCase().contains(userinput)) {
newList.add(new alldata(country,""));
}
}
adapter.updatelist(newList);
return true;
}
}
images are at https://imgur.com/a/1xAw7xi
Aucun commentaire:
Enregistrer un commentaire