如何使用点击侦听器使用“回收”视图项打开不同的活动

时间:2018-10-10 06:48:14

标签: android android-recyclerview onclick onclicklistener

如何使用单击侦听器使用“回收”视图项打开不同的活动。帮助我,我是初学者。  我的代码是

主要活动: Main Activity

Adapter

4 个答案:

答案 0 :(得分:0)

viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(countries.get(i).equalsIgnoreCase("India")){

               // open activiy here
            }

            else if(countries.get(i).equalsIgnoreCase("Germany")){
               // open activiy here
            }

            .
            .
            .
    });

答案 1 :(得分:0)

在适配器数据列表中发送活动名称。然后在OnClickListener中通过调用startMyActivity方法启动活动,如下所示:

holder.itemView.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
              startMyActivity(dataSet.get(listPosition));
       }
                    });

这里数据集是我的活动名称列表。下面是按名称调用活动的代码

 private void startMyActivity(String ActivityName){
        String activityToStart = ActivityName;
        try {
            Class<?> c = Class.forName(activityToStart);
            Intent intent = new Intent(mContext, c);
            mContext.startActivity(intent);
        } catch (ClassNotFoundException ignored) {
            Log.e("no activity","Activity not found");
        }
    }

答案 2 :(得分:0)

在您的视图(活动/片段)中实现一个接口,并将其传递给RecyclerView构造函数。然后,在views onclick事件上,只需调用接口的方法并处理您的活动或片段中的单击即可。

例如-

public YourAdapter( ArrayList<RESULT> list, IRecyclerItemClickListener listener) {
    this.list = list;
    this.listener = listener;
}

在ViewHolder中

yourView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    listener.onclik(yourview,position);
}

在已实施的“活动”或“片段”中

@Override
public void onViewClicked(View view,int position) {

    switch(view.getId()){
     case R.id.yourview:startActivity(new Intent(this,ActivityB.class))
 }
}

答案 3 :(得分:0)

解决方案。

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> countries;

public DataAdapter(ArrayList<String> countries) {
    this.countries = countries;
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardlay, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

    viewHolder.tv_country.setText(countries.get(i));
    viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            if(countries.get(i).equalsIgnoreCase("India")){
                Intent intent= new Intent(context, First.class);
                context.startActivity(intent);
            }

            else if(countries.get(i).equalsIgnoreCase("Germany")){
                Intent intent= new Intent(context, Second.class);
                context.startActivity(intent);
            }
        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView tv_country;
    public ViewHolder(View view) {
        super(view);

        tv_country = (TextView)view.findViewById(R.id.tv_country);
    }
}

}