SimpleRecyclerAdapter:RecyclerView адептер OnClick проблема
public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleRecyclerAdapter.SimpleItemViewHolder> {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public SimpleRecyclerAdapter(Context context,ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
public class SimpleItemViewHolder extends RecyclerView.ViewHolder {
TextView country;
TextView population;
ImageView flag;
public SimpleItemViewHolder(View itemView) {
super(itemView);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
flag = (ImageView) itemView.findViewById(R.id.flag);
}
}
@Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup parent, final int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.singleitemview, parent, false);
return new SimpleItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(SimpleItemViewHolder viewHolder, int position) {
resultp = data.get(position);
viewHolder.country.setText(resultp.get(MainFragment.COUNTRY));
viewHolder.population.setText(resultp.get(MainFragment.POPULATION));
imageLoader.DisplayImage(resultp.get(MainFragment.FLAG), viewHolder.flag);
}
@Override
public int getItemCount() {
return data.size();
}
}
Как добавить код в OnClick адаптер. Помогите мне пожалуйста
Türkçe: onClick kodunu nereye eklemeliyim.
OnClick Метод:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
DiziFragment myFragment3 = new DiziFragment();
Bundle bundle = new Bundle();
bundle.putString("gun",resultp.get(MainFragment.POPULATION));
bundle.putString("flag",resultp.get(MainFragment.FLAG));
myFragment3.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,myFragment3);
fragmentTransaction.commit();
}
});
Вы можете определить OnClick слушателя в вашей реализации ViewHolder. Класс ViewHolder предоставляет методы getLayoutPosition и getAdapterPosition для получения позиции элемента, на который вы нажали. – thetonrifles
Есть примеры? –
Я предоставил вам код в ответе – thetonrifles