Android 接口回调示例

永远都是在忘记在巩固的东西,在这里记忆一下吧

我这个接口回调使用在recyclerviewAdapter跟activity中:

首先在adapter中写一个接口并且构造一下:

Android 接口回调示例_第1张图片

第二步public接口中写一个方法:

Android 接口回调示例_第2张图片

第三步:就是我们在使用的时候 一般就是点击事件的时候,一般情况下只需要拿到下标就行,看自己的需求了:

Android 接口回调示例_第3张图片

最后一步就是在activty的使用,就是在设置适配的时候new一下 重写一下接口的方法,做对应的操作就ok了:

AdministrativeRecycler类展示:

package com.jiuhao.jhjk.adapter.welcome;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiuhao.jhjk.R;
import com.jiuhao.jhjk.bean.DepartmentBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixiaona on 2019/8/23.
 */

public class AdministrativeRecycler extends RecyclerView.Adapter {

    private Context context;
    private List list = new ArrayList<>();
    private String departmentnamestr;
    private im_onlisten monlisten;

    private int flag = -1;


    public AdministrativeRecycler(Context context, List mDataList, String departmentnamestr,im_onlisten monlisten) {
        this.context = context;
        this.list = mDataList;
        this.departmentnamestr = departmentnamestr;
        this.monlisten=monlisten;
    }

    @NonNull
    @Override
    public AdministrativeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_administrative, viewGroup, false);
        return new AdministrativeViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull AdministrativeViewHolder administrativeViewHolder, int i) {
        DepartmentBean departmentBean = list.get(i);
        String departmentName = departmentBean.getDepartmentName();
        administrativeViewHolder.subject.setText(departmentName);
        administrativeViewHolder.administrative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flag = i;
                monlisten.imOnlisten(i);
                notifyDataSetChanged();
            }
        });
        if (departmentnamestr.equals(departmentName) && flag == -1) {
            administrativeViewHolder.administrative.setImageResource(R.mipmap.select);
        } else if (flag == i) {
            administrativeViewHolder.administrative.setImageResource(R.mipmap.select);
        } else {
            administrativeViewHolder.administrative.setImageResource(R.mipmap.select1);
        }
    }

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

    public interface im_onlisten {
         void imOnlisten(int postion);
    }

    class AdministrativeViewHolder extends RecyclerView.ViewHolder {

        public TextView subject;
        public ImageView administrative;

        @SuppressLint("WrongViewCast")
        public AdministrativeViewHolder(@NonNull View itemView) {
            super(itemView);

            subject = itemView.findViewById(R.id.subject);
            administrative = itemView.findViewById(R.id.administrative);
        }
    }
}

 

你可能感兴趣的:(Android 接口回调示例)