如何在适配器类中添加处理程序

时间:2017-08-25 07:35:51

标签: android android-recyclerview

我有一个带有RecyclerView的Android应用程序,其中包括:

  • EditText
  • Button

单击Button时,应将数据发送到服务器。

我想在3秒的时间间隔内添加一个处理程序来执行一个getthode我怎样才能实现In适配器。

4 个答案:

答案 0 :(得分:1)

要在适配器中使用Handler,您可以在适配器中使用以下代码段:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // your task
    }
},3000);

答案 1 :(得分:0)

适配器类的示例:

检查下面的课程

 mHolder.tvUserFrom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       // Execute a comethode in the intervel 3sec
                    }
                },3000);
            }
        });

MyAdapter类

public class MyAdapter extends BaseAdapter {

    private ArrayList<Model> mItems = new ArrayList<>();
    private BaseActivity activity;

    public MyAdapter(BaseActivity activity, ArrayList<Model> users) {
        this.activity = activity;
        this.mItems = users;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public int getViewTypeCount() {

        if (getCount() < 1){
            return 1;
        } else {
            return getCount();
        }
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }


    @Override
    public MessageModel getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        View v = convertView;
        Context context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        if (v == null) {
            v = layoutInflater.inflate(R.layout.row_item_chat_message, null);

            mHolder = new ViewHolder();

            mHolder.tvUserFrom = (TextView) v.findViewById(R.id.tvUserFrom);
            mHolder.tvUserTo = (TextView) v.findViewById(R.id.tvUserTo);

            v.setTag(mHolder);

        } else {
            mHolder = (ViewHolder) v.getTag();
        }

        Model model = mItems.get(position);

        mHolder.tvUserFrom.setText("John");


        mHolder.tvUserFrom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       // Execute a comethode in the intervel 3sec
                    }
                },3000);
            }
        });




        return v;
    }

    static class ViewHolder {
        public ImageView ivUser;
        public TextView tvUserFrom,tvUserTo;
    }
}

<强> import android.os.Handler;

答案 2 :(得分:0)

导入android.os.Handler;

然后才会起作用

答案 3 :(得分:0)

在getView中尝试一下,可以100%工作 我添加了延迟乐趣来延迟活动的启动

kotlin:

        var mycon=context
        val secondsDelayed = 1
        android.os.Handler().postDelayed({
            mycon.startActivity(Intent(mycon, ListInvests::class.java))
        }, secondsDelayed * 1000.toLong())