RecyclerView在项目更改时单击

时间:2018-02-04 07:19:23

标签: java android android-recyclerview

在我的RecyclerView中,searchview中有一个过滤数据。单击项目在ViewHolder中实现 现在我希望OnItemClickListener通过界面,一切都有效,直到排序时项目的顺序被排序,例如,当搜索第20个位置时,它成为第一个并被视为第一个。 5该位置变为1位但处理为1.在这种情况下如何正确绑定ID?

snachala bylo tak

起初就是这样:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

constructor(public navCtrl: NavController) {
    this.generateTopics();
 }

topics: string[];

    generateTopics(){
this.topics = [
'Dumaguete city',
'bacong february 1 2018',
'Dumaguete february 2 2018',
'Sibulan february 3 2018',
'bacong february 4 2018',
'ajong february 5 2018',
'dauin february 6 2018',
'zamboanguita february 7 2018',
];
}


    getTopics(ev: any) {
this.generateTopics();
let serVal = ev.target.value;
if( serVal && serVal.trim() != ''){
    this.topics == this.topics.filter((topic) =>{
        return (topic.toLowerCase().indexOf(serVal.toLowerCase()) > -1);
    })
 }
}
}   

并发现了一个事件:

ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView name;

    StationViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
        name = v.findViewById(R.id.name);
    }
    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Activity activity = (Activity) context;
        Intent i;
        i = new Intent(context, DetailStation.class);
        i.putExtra("pos1",listStations.get(getAdapterPosition()).getName());

        context.startActivity(i);

现在怎么办?

1 个答案:

答案 0 :(得分:0)

我这样做:searchView。片段本身。如果它不起作用,那么我输出带有铭文的视图文本 - 未找到数据

这是我的片段

// is my fragment
public class MyFragment
        extends Fragment
        implements SearchView.OnQueryTextListener
//
//фрагепмнт


 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.menu_items, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(this);
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        searchView.clearFocus();
        return false;
    }

    private List<ItemListStation>
    filter(List<ItemListStation> models, String query) {
        query = query.toLowerCase();
        final List<ItemListStation> filteredModelList = new ArrayList<>();
        for (ItemListStation model : models)     {
            final String text;
            text = model.getName().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        final List<ItemListStation> filteredModelList = filter(listStationItemList, newText);
        adapter.setFilter(filteredModelList);
        textViewSearch.setVisibility(View.GONE);
        if (filteredModelList.isEmpty()) {
            View myView = textViewSearch;
            slideToBottom(myView);
        }
        return true;
    }

    public void slideToBottom(View view){
        Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
        if (view.getVisibility() == View.GONE) {
            view.setVisibility(View.VISIBLE);
            view.startAnimation(shake);
        }
    }

这是我的适配器

 class Bus2Adapter extends RecyclerView.Adapter<Bus2Adapter.StationViewHolder> {

private List<ItemBusStation> listStations;

Bus2Adapter(List<ItemBusStation> listStations) {
    this.listStations = listStations;    }

@Override
public StationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view =
            LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_bus_station, parent, false);
    return new StationViewHolder(view);
}
@Override
public int getItemViewType(int position) {
    return position;    }

@Override
public void onBindViewHolder(StationViewHolder holder, int position) {
    holder.name.setText(listStations.get(position).getName());
}

@Override
public int getItemCount() {
    return listStations.size();
}

class StationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView name;

    StationViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
        name = v.findViewById(R.id.name);
    }
    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Activity activity = (Activity) context;
        Intent i;
        i = new Intent(context, DetailStation.class);
        i.putExtra("pos1", listStations.get(getAdapterPosition()).getName());
        context.startActivity(i);
    }
}

public void setFilter(List<ItemBusStation> listStationLists)    {
    listStations = new ArrayList<>();
    listStations.addAll(listStationLists);
    notifyDataSetChanged();    }

}

这是我的项目

ItemBusStation implements Serializable {
private String name;

public ItemBusStation(String name) {
    this.name = name;
}

public String getName() {
    return name;
}