• Android-----下拉刷新整合到项目中


    在app.gradle中添加依赖:compile 'in.srain.cube:ultra-ptr:1.0.11' //下拉刷新

    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
    
        <in.srain.cube.views.ptr.PtrFrameLayout
            android:id="@+id/mPtrFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_View"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            </android.support.v7.widget.RecyclerView>
    
        </in.srain.cube.views.ptr.PtrFrameLayout>
    
    
    </LinearLayout>

    逻辑处理:

    public class RxJavaActivity extends AppCompatActivity {
    
        private BootstrapButton login , exit;
        private AlertDialogUtils utils;
    
        private PtrFrameLayout ptrFrame;    //下拉刷新
        //private TextView textView;
        //private ListView listView;
        List<Reception> listData = new ArrayList<>();
    
        private RecyclerView recyclerView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rx_java);
            initData();
        }
    
        public void initData(){
    
            /*【获取对象数据】*/
            TransferData userData = (TransferData) getIntent().getSerializableExtra("userData");
            userData.show();
    
             /*【获取对象数组数据】*/
            List<TransferData> resultList = (List<TransferData>) getIntent().getSerializableExtra("list");
            resultList.get(0).show();
            resultList.get(1).show();
    
            Reception reception0 = new Reception();
            reception0.setImgPath(R.drawable.ic_launcher);
            reception0.setScope("我是第0条数据。");
    
            Reception reception1 = new Reception();
            reception1.setImgPath(R.drawable.ic_launcher);
            reception1.setScope("我是第1条数据。");
            listData.add(reception1);
    
            Reception reception2 = new Reception();
            reception2.setImgPath(R.drawable.ic_launcher);
            reception2.setScope("我是第2条数据。");
            listData.add(reception2);
    
            Reception reception3 = new Reception();
            reception3.setImgPath(R.drawable.ic_launcher);
            reception3.setScope("我是第3条数据。");
            listData.add(reception3);
    
            Reception reception4 = new Reception();
            reception4.setImgPath(R.drawable.ic_launcher);
            reception4.setScope("我是第4条数据。");
            listData.add(reception4);
    
            Reception reception5 = new Reception();
            reception5.setImgPath(R.drawable.ic_launcher);
            reception5.setScope("我是第5条数据。");
            listData.add(reception5);
    
            initUI();
        }
    
    
        private void initUI(){
    
            recyclerView = findViewById(R.id.recycler_View);
            /*【默认为纵向排列】*/
            //LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            //recyclerView.setLayoutManager(layoutManager);
    
            /*【调用 LinearLayoutManager 的 setOrientation() 方法来设置布局的排列方向】*/
            //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    
    
            /*【瀑布流布局】*/
            //StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            //recyclerView.setLayoutManager(manager);
    
            /*【网格布局】*/
            GridLayoutManager gridlayout = new GridLayoutManager(this,2);
            recyclerView.setLayoutManager(gridlayout);
    
            RecyclerViewAdapter adapter = new RecyclerViewAdapter(listData);
            recyclerView.setAdapter(adapter);
    
    
    
    //        ReceptionAdapter adapter = new ReceptionAdapter(this,R.layout.header,listData);
            ptrFrame = findViewById(R.id.mPtrFrame);    //下拉刷新实例化
    
    //        listView = findViewById(R.id.ListRefresh);  //数据list
    //        listView.setAdapter(adapter);               //与适配器绑定
    //        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    //            @Override
    //            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    //                Reception rece = listData.get(i);
    //                AlertDialogUtils utils = new AlertDialogUtils(RxJavaActivity.this);
    //                utils.AlertTitle(rece.getScope(),"success");
    //            }
    //        });
    
    
            //textView = findViewById(R.id.TestRefresh);
            //final PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(this);          //经典的头部风格
            final MaterialHeader header = new MaterialHeader(this);                     //Material头部风格
            header.setPadding(0, PtrLocalDisplay.dp2px(15),0,0);
            // ptrFrame.setPinContent(true);//刷新时,保持内容不动,仅头部下移,默认,false
            // ptrFrame.setKeepHeaderWhenRefresh(true);//刷新时保持头部的显示,默认为true
            // ptrFrame.disableWhenHorizontalMove(true);//如果是ViewPager,设置为true,会解决ViewPager滑动冲突问题。
            ptrFrame.setHeaderView(header);
            ptrFrame.addPtrUIHandler(header);
            ptrFrame.setPtrHandler(new PtrHandler() {
                /**【检查是否可以执行下来刷新,比如列表为空或者列表第一项在最上面时】**/
                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                }
                /**【加载数据时触发,可在这里做线程获取数据等操作】**/
                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    //textView.setText("下拉刷新完成");
    //                ReceptionAdapter adapter = new ReceptionAdapter(RxJavaActivity.this,R.layout.header,listData);
    //                listView.setAdapter(adapter);               //与适配器绑定
    
                    ptrFrame.refreshComplete();             //刷新完成
                    //ptrFrame.autoRefresh();//自动刷新
                }
            });
    
    
        }
    }

    子项布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center_horizontal">
    
        <ImageView
            android:id="@+id/image_View"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
    
        <TextView
            android:id="@+id/text_View"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello Word"
            android:textSize="18sp"/>
    
    </LinearLayout>

    适配器编写:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
    
        private List<Reception> receptionList = new ArrayList<>();
    
        /**【定义一个内部类,展示的数据由内部类决定】**/
        static class viewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView textView;
    
            public viewHolder(View view) {
                super(view);
                imageView =  view.findViewById(R.id.image_View);
                textView = view.findViewById(R.id.text_View);
            }
        }
    
        /**【构造函数初始化数据】**/
        public RecyclerViewAdapter(List<Reception> list){
            this.receptionList = list;
        }
    
    
        @Override
        public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false);
            final viewHolder holder = new viewHolder(view);
            /*【实现点击事件】*/
            holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = holder.getAdapterPosition();
                    Reception reception = receptionList.get(position);
                    Toast.makeText(view.getContext(),reception.getScope(),Toast.LENGTH_LONG).show();
                    return false;
                }
            });
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getAdapterPosition();
                    Reception reception = receptionList.get(position);
                    Log.e("拿到的数据",reception.getScope());
                }
            });
            return holder;
        }
    
        @Override
        public void onBindViewHolder(viewHolder holder, int position) {
            Reception reception = receptionList.get(position);
            holder.imageView.setImageResource(reception.getImgPath());
            holder.textView.setText(reception.getScope());
        }
    
        @Override
        public int getItemCount() {
            return receptionList.size();
        }
    }
    Reception只是一个普通的实体类,里面写有 set 和 get 方法

    看下我注释掉的代码,你会发现我这里也附有
    RecyclerView 的使用
  • 相关阅读:
    ASIX配置vlan tag能被wireshark获取
    翻译-cmake教程
    Manjaro打造开发环境
    Frida入门
    安卓应用启动底层执行逻辑
    集合框架
    oracle清除日志内存
    flutter widgets目录
    给设备添加udid
    5.class
  • 原文地址:https://www.cnblogs.com/xiobai/p/13524947.html
Copyright © 2020-2023  润新知