如何将视图ID从Recycler Adapter传递给Activity?

时间:2017-07-29 06:14:45

标签: android android-recyclerview android-view

当点击Recycler视图列表项时,我试图将视图ID从Recyclerview Adapter传递给Activity。

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> implements FastScrollRecyclerView.SectionedAdapter{

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(User data,int id);
    }

    List<User> dataSource;
    private OnRecyclerItemClickListener onRecyclerItemClickListener;
    private Context context;
    public ActiveBeepsAdapter(List<User> dataArgs, OnRecyclerItemClickListener onRecyclerItemClickListener,Context context) {
        this.dataSource = dataArgs;
        this.onRecyclerItemClickListener = onRecyclerItemClickListener;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_active_message, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, onRecyclerItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        User user = dataSource.get(position);
        holder.tvId.setText(user.getId());
        Picasso.with(context)
            .load(user.getProfilePicUrl())
            .placeholder(R.drawable.bg_logo)
            .error(R.drawable.bg_logo)
            .transform(new CircleTransform())
            .into(holder.ivUserProfile);   
    }

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

    @NonNull
    @Override
    public String getSectionName(int i) {
        return String.valueOf(dataSource.get(i).getId().charAt(0));
    }

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

        private OnRecyclerItemClickListener onRecyclerItemClickListener;
        User data = new User();
        TextView tvId;
        ImageView ivUserProfile;
        public ViewHolder(View itemView, OnRecyclerItemClickListener onRecyclerItemClickListener) {
            super(itemView);
            this.onRecyclerItemClickListener = onRecyclerItemClickListener;
            tvId = (TextView)itemView.findViewById(R.id.tvName);
            ivUserProfile = (ImageView)itemView.findViewById(R.id.ivProfilePic); 
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            User user = dataSource.get(position);
            int id = v.getId();
            onRecyclerItemClickListener.onRecyclerItemClick(user,id);
        }
    }
}

假设,如果单击了“配置文件图像”,则视图ID应发送到活动..

和我的活动

    @EActivity
    public class ActiveMessage extends AppCompatActivity implements Adapter.OnRecyclerItemClickListener{

        @ViewById(R.id.toolbar)
        Toolbar toolbar;

        @ViewById(R.id.rvactiveMessage)
        FastScrollRecyclerView rvmessage;

        List<User> allUser;

        @ViewById(R.id.toolbar_title)
        TextView tv_toolbar;

        @AfterViews
        protected void init(){

            toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_drop_down_black_24dp));
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            tv_toolbar.setText("Active Message");
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            allUser = new ArrayList<>();
            User user;
            String fullName[] = {"ajay","kamal","akhil"};
            String id[] = {"its_ajay","its_kamal","its_akhil"};
            for(int i=0;i<id.length;i++){
               user = new User();
               user.setId(id[i]);
               user.setFullName(fullName[i]);
               allUser.add(user);
            }
Adapter adapter = new Adapter(allUser,this,getApplicationContext());
rvmessage.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rvmessage.setAdapter(activeBeepsAdapter);

        }

        @Override
        public void onRecyclerItemClick(User data,int id) {
            if (!Utils.isNetworkAvailable(getApplicationContext())) {
                   noInternet();
            }
            // tvName has multiple Implementation  
            if (id == R.id.tvName){
                 Log.d("Id",data.getId());
            }
        }
        public void noInternet(){

            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.alert_dialog);
            dialog.show();
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_active_message);
        }
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            finish();
        }
    }

在我的Activity中,如果单击ProfileImage,则弹出对话框 此外,我需要在单击列表项时检查是否存在Internet连接。

1 个答案:

答案 0 :(得分:0)

  

在适配器onClick方法而不是id传递视图中:

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        User user = dataSource.get(position);
        int id = v.getId();
        onRecyclerItemClickListener.onRecyclerItemClick(user,v);
    }
  

现在进入活动

@Override
    public void onRecyclerItemClick(User data,View view) {
        if (!Utils.isNetworkAvailable(getApplicationContext())) {
               noInternet();
        }
        // tvName has multiple Implementation  
        if (view.getId() == R.id.tvName){
             Log.d("Id",data.getId());
        }
    }