recycleview 滑动点击监听

recycleview 滑动点击监听

recycleview 滑动点击监听(直接用)

public class ListScrollerListener extends RecyclerView.OnScrollListener {
    int lastVisibleItemPos;
    int visibleItemCount;
    int itemCount;
    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayoutManager layoutManager=(LinearLayoutManager)recyclerView.getLayoutManager();
        lastVisibleItemPos=layoutManager.findLastVisibleItemPosition();
        visibleItemCount=layoutManager.getChildCount();
        itemCount=layoutManager.getItemCount();
    }

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (visibleItemCount>0&&lastVisibleItemPos==itemCount-1&& newState ==RecyclerView.SCROLL_STATE_IDLE){
            onPageNext();
        }
    }
    public void onPageNext(){
        
    }
}
public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText et1,et2;
    private Button b1,b2;

    private static final String url="http://115.28.211.9:8080/exame/login_user";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=findViewById(R.id.uesr_name);
        et2=findViewById(R.id.uesr_password);
        b1=findViewById(R.id.login);
        b2=findViewById(R.id.register);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                String name=et1.getText().toString();
                String password=et2.getText().toString();
                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
                    toast("请检查用户和密码");
                    return;
                }
                postrequest(name,password);
                break;
            case R.id.register:
                break;
        }
    }



    private void postrequest(String name, String password){

        OkhttpRequest okhttpRequest=new OkhttpRequest();
        HashMap map=new HashMap();
        map.put("name",name);
        map.put("password",password);
        okhttpRequest.sendPostRequest(map, url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                toast(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str=response.body().string();
                if (null!=str){
                    Gson gson=new Gson();
                    final Result result=gson.fromJson(str,new TypeToken(){}.getType());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if ("1".equals(result.getStatus())){
                                Util.saveToSp(LoginActivity.this,"token",result.getToken());
                                intent(MainActivity.class);
                                toast(result.getMessage());
                            }else {
                                toast(result.getMessage());
                            }
                        }
                    });
                }
            }
        });


    }


}

你可能感兴趣的:(android)