使用Data Binding for RecyclerView获取CardView位置

时间:2019-03-06 06:43:51

标签: android android-recyclerview android-databinding

我在应用程序中为DataBinding使用了RecyclerView。我已经使用数据绑定为OnClickListener设置了CardView。我无法获取哪个CardCard是CardView的位置点击了。
这是我的Adapter

class public class QueueAdapter extends RecyclerView.Adapter<QueueAdapter.ViewHolder> implements ClickCard {
    private ArrayList<CurrentStatusEntry> originalList;
    Context context;

    public QueueAdapter(ArrayList<CurrentStatusEntry> originalList, Context context) {
        this.originalList = originalList;
        this.context = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        QueueCardBinding binding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.queue_card,parent,false);
        ViewHolder vh=new ViewHolder(binding);
        return  vh;
    }
    @Override
    public void onBindViewHolder( ViewHolder holder, final int position) {
        CurrentStatusEntry entry=originalList.get(position);
        holder.qBinding.setQueue(entry);
        holder.qBinding.setClickHandler(this);      
    }
    @Override
    public int getItemCount() {
        return originalList.size();
    }
    @Override
    public void cardClicked(CurrentStatusEntry e) {       
        Toast.makeText(context,e.getName()+" "+e.getPeople(),Toast.LENGTH_SHORT).show();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        QueueCardBinding qBinding;
        private ViewHolder(QueueCardBinding itemView) {
            super(itemView.getRoot());
            this.qBinding=itemView;
        }
    }
}  

这是我的Activity

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding activityMainBinding;
    QueueAdapter adapter;
    ArrayList<CurrentStatusEntry> entries=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        activityMainBinding.queue.setLayoutManager(new LinearLayoutManager(this));
        activityMainBinding.queue.setHasFixedSize(true);
    }  

需要进行哪些更改才能单击当前卡?

1 个答案:

答案 0 :(得分:0)

//Please Check This Code To Work!?

//Write This Code Inside onBindViewHolder Method
holder.qBinding.your_card_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Enter Your Code
            }
      });