自定义View搜索框以及SwipyRefreshLayout,RecyclerView上拉加载,下拉刷新

![在这里插入图片描述](https://img-blog.csdnimg.cn/201902251909536.png)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190225191057680.png)

1.

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bt_1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:background="@drawable/search" />
        <!-- 输入的搜索信息 -->
        <EditText
            android:id="@+id/et_search"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight="4"
            android:background="#ffff"
            android:gravity="center_vertical"
            android:hint="输入内容进行搜索"
            android:imeOptions="actionSearch"
            android:singleLine="true"
            android:textColor="#0e0e0e"
            android:textColorHint="#b0c6ce"
            android:textSize="17sp" />


        <Button
            android:id="@+id/bt_clear"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:background="@drawable/delete" />
    </LinearLayout>
2.
package com.example.zzrk2019222;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * @Auther: luck
 * @Date: 2019/2/22 08:57:20
 * @Description:
 */
public class MyView extends LinearLayout implements TextWatcher, View.OnClickListener {

    private Button bt_1;

    public interface OnEditListnner{

        void getNames(String string);

    }
    private OnEditListnner onEditListnner;

    public void setOnEditListnner(OnEditListnner onEditListnner) {
        this.onEditListnner = onEditListnner;
    }

    private EditText et_search;
    /**
     * 输入框后面的那个清除按钮
     */
    private Button bt_clear;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context,AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.myview, this, true);
        /***找出控件*/

        bt_1 = findViewById(R.id.bt_1);
        et_search = (EditText) findViewById(R.id.et_search);
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_clear.setVisibility(GONE);
        et_search.addTextChangedListener(this);
        bt_clear.setOnClickListener(this);


        bt_1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = et_search.getText().toString();
                if (onEditListnner!=null){
                    onEditListnner.getNames(input);
                }

            }
        });
    }

    public MyView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String input = et_search.getText().toString().trim();
        if (input.isEmpty()) {
            bt_clear.setVisibility(GONE);
        } else {
            bt_clear.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        et_search.setText("");
    }
}
3.这是Fragment的代码
package com.example.zzrk2019222.Fragments;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout;
import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayoutDirection;
import com.example.zzrk2019222.Adapter.MyAdapter;
import com.example.zzrk2019222.MainActivity;
import com.example.zzrk2019222.MyView;
import com.example.zzrk2019222.Presenter.Showpresenter;
import com.example.zzrk2019222.R;
import com.example.zzrk2019222.View.ShowView;

import org.json.JSONArray;

import static com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayoutDirection.*;

/**
 * @Auther: luck
 * @Date: 2019/2/22 13:58:37
 * @Description:
 */
public class Fragment_Head extends Fragment implements ShowView {
    private View view;
    private MyView myview;
    private RecyclerView recyclerView;

    private  String string="板鞋";
    private Showpresenter showpresenter;
    private SwipyRefreshLayout swipyRefreshLayout;
    private static int count=5;
    private Handler handler=new Handler();
    private MyAdapter myAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_head, null, false);
     	 initView(view);
        swipyRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark);
        swipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        showpresenter = new Showpresenter(this);
        showpresenter.send(string,count);
        showpresenter.attachView(view);
///将拿到的值设置给p层
        myview.setOnEditListnner(new MyView.OnEditListnner() {
            @Override
            public void getNames(String string) {
                showpresenter.send(string,count);
            }
        });





        swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(int index) {

                count=5;

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipyRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }

            @Override
            public void onLoad(int index) {

                count++;
                showpresenter.send(string,count);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipyRefreshLayout.setRefreshing(false);
                    }
                },3000);

            }
        });





        return view;

    }

    private void initView(View view) {
        swipyRefreshLayout = view.findViewById(R.id.swipyRefreshLayout);

        myview = (MyView) view.findViewById(R.id.myview);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        }

    @Override
    public void view(JSONArray jsonArray) {

        myAdapter = new MyAdapter(getContext(), jsonArray);
        recyclerView.setAdapter(myAdapter);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        showpresenter.detachView();
    }
}

4.这是页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <com.example.zzrk2019222.MyView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/myview"
        />

    <com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout
        android:id="@+id/swipyRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

    </com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout>





</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值