RecyclerView Adepter OnClick问题

时间:2016-02-03 20:12:09

标签: java android onclick android-recyclerview

SimpleRecyclerAdapter:

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适配器?

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();

        }
    });

2 个答案:

答案 0 :(得分:1)

您可以按如下方式实施ViewHolder

public class SimpleItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    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);
        // register listener
        itemView.setOnClickListener(this);
    }

    Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        // your code here
    }

}

答案 1 :(得分:0)

您应该使用onItemClickListener而不是onClickListener

在您Fragment

ActivityRecyclerView中执行此操作
myRecyclerView.setOnItemClickListener(new OnItemClickListener() // Use your recycler view here
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
    { 
        resultp = myRecyclerView.getAdapter().getItem(position)); // Use your recycler view here
        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();
    }
});