使用我的recyclerview的searchview后,我无法返回正确的视图。
当我在项目点击时仅使用回收器时,我膨胀了正确的视图,但是,一旦我进行搜索并显示结果,则错误的视图会膨胀。
我认为这是因为我使用position
并不是使用searchview时保持原始Recyclerview位置的最佳方式。
我已经看到getAdapterPosition()
是通过搜索查看维护项目位置的最佳方式。
如果有人可以帮助我实现在搜索后保持膨胀的最佳方式,我将非常感激!我还将在特定场景的代码之后提供一个示例。
另外,为了澄清,我在适配器的 onBindViewHolder 方法中扩展了RecyclerAdapter的视图。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Data> listData = new ArrayList<Data>();
private Context context;
Dialog myDialog;
public RecyclerViewAdapter(List<Data> listData, Context context) {
this.listData = listData;
this.context = context;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item,parent,false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.itemz.setImageResource(listData.get(position).getitemz());
holder.itemName.setText(listData.get(position).getitemName());
holder.itemPrice.setText(listData.get(position).getitemPrice());
holder.itemVal.setText(listData.get(position).getitemVal());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
final SwipeDismissDialog swipeDismissDialog;
Button btnOK;
if (position == 0) {
myDialog = new Dialog(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_cards, null);
swipeDismissDialog = new SwipeDismissDialog.Builder(myDialog.getContext()).setView(itemView).build().show();
TextView words = (TextView)itemView.findViewById(R.id.words);
words.setText("Success1");
btnOK = (Button)itemView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeDismissDialog.dismiss();
}
}
);
}
else if (position == 1){
myDialog = new Dialog(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_cards, null);
swipeDismissDialog = new SwipeDismissDialog.Builder(myDialog.getContext()).setView(itemView).build().show();
TextView words = (TextView)itemView.findViewById(R.id.words);
words.setText("Success2");
btnOK = (Button)itemView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeDismissDialog.dismiss();
}
}
);
}
}
});
}
@Override
public int getItemCount() {
return listData.size();
}
public void filterList(ArrayList<Data> filteredList){
listData = filteredList;
notifyDataSetChanged();
}
}
发生了什么:
success1
会按预期显示。item2
时,该项会按预期返回,但是,如果我点击该项,则会显示success1
而不是success2
。 最好的解决办法是什么?
任何帮助将不胜感激!
答案 0 :(得分:0)
我认为当你进行搜索时,你在适配器中设置了一个新的数据列表,这个新列表包含位置0的item2。
因此,当您点击该项目时,您点击了位置0处的项目,并且在您的代码中您有类似的内容:
所以你应该在物品上使用条件而不是位置。 这是最好的方法:
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from
'@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {AuthenticationService} from '../_services/authentication.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
private authService: AuthenticationService;
constructor(private inj: Injector) {
}
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
if (req.url.includes('token')) {
return next.handle(req);
}
this.authService = this.inj.get(AuthenticationService);
const cloned = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' +
this.authService.getAccessToken())
});
return next.handle(cloned); // If the JWT is not present, then the
request goes through to the server unmodified
}
}
然后:
public Data getDataAtPosition(int position) {
if (listData!= null && listData.size() > position)
return listData.get(position);
else
return null;
}
PS:您每次持有者更改时都会创建一个新的侦听器,也许您可以查看有关如何在RecyclerView上创建ClickListener的说明:https://stackoverflow.com/a/26196831/9434800