SuperSwipeRefreshLayout和RecycleView

1.SuperSwipeRefreshLayout

GitHub:https://github.com/nuptboyzhb/SuperSwipeRefreshLayout

allprojects {
    repositories {
        jcenter()
        maven {
            url  "http://dl.bintray.com/nuptboyzhb/maven"
        }
    }
}

dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.github.nuptboyzhb.lib.uikit:superswiperefreshlayout:1.0.0'
compile 'com.android.support:recyclerview-v7:25.3.1'
}

代码

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerview;
    private SuperSwipeRefreshLayout swipeRefreshLayout;

    // Header View
    private ProgressBar progressBar;
    private TextView textView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }
    private void init(){
        System.out.println("哈喽沃德");
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        LinearLayoutManager layout = new LinearLayoutManager(this);//listview的用法
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layout);
        //recyclerview.setLayoutManager(new GridLayoutManager(this,4));//gridview的用法
        //recyclerview.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//4列,竖向滑动
        Dividerlios divider= new Dividerlios(MainActivity.this);//分割线 需要自己添加
        recyclerview.addItemDecoration(divider);
        MainAdapter mainAdapter = new MainAdapter();
        recyclerview.setAdapter(mainAdapter);

        //点击事件
        mainAdapter.setOnItemClickListener(new MainAdapter.onRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, String tag) {
                Toast.makeText(MainActivity.this,"你好,我是"+tag, Toast.LENGTH_SHORT).show();
            }
        });

        // init SuperSwipeRefreshLayout
        swipeRefreshLayout = (SuperSwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRefreshLayout.setHeaderViewBackgroundColor(0xff888888);
        swipeRefreshLayout.setHeaderView(createHeaderView());// add headerView
        swipeRefreshLayout.setTargetScrollWithLayout(true);
        swipeRefreshLayout
                .setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {

                    @Override
                    public void onRefresh() {
                        textView.setText("正在刷新");
                        imageView.setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                        new Handler().postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                swipeRefreshLayout.setRefreshing(false);
                                progressBar.setVisibility(View.GONE);
                            }
                        }, 2000);
                    }

                    @Override
                    public void onPullDistance(int distance) {
                        // pull distance
                    }

                    @Override
                    public void onPullEnable(boolean enable) {
                        textView.setText(enable ? "松开刷新" : "下拉刷新");
                        imageView.setVisibility(View.VISIBLE);
                        imageView.setRotation(enable ? 180 : 0);
                    }
                });
    }

    private View createHeaderView() {
        View headerView = LayoutInflater.from(swipeRefreshLayout.getContext())
                .inflate(R.layout.layout_head, null);
        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
        textView = (TextView) headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        imageView = (ImageView) headerView.findViewById(R.id.image_view);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(R.drawable.down_arrow);
        progressBar.setVisibility(View.GONE);
        return headerView;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.givechenbin.MainActivity">

    <com.github.nuptboyzhb.lib.SuperSwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
    </com.github.nuptboyzhb.lib.SuperSwipeRefreshLayout>

</android.support.constraint.ConstraintLayout>

adapter

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private onRecyclerViewItemClickListener itemClickListener = null;

    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(parent.getContext(),R.layout.item,null);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemClickListener != null){
                    itemClickListener.onItemClick(v,(String)v.getTag());
                }
            }
        });
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
        holder.tv_item.setText("当前项是="+position);
        holder.itemView.setTag(holder.tv_item.getText());
    }

    @Override
    public int getItemCount() {
        return 100;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_item;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_item = (TextView) itemView.findViewById(R.id.tv_text);
        }
    }

    public interface onRecyclerViewItemClickListener{
        void onItemClick(View v, String tag);
    }

    public void setOnItemClickListener(onRecyclerViewItemClickListener l){
        this.itemClickListener = l;
    }
}
<?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">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_text"
        android:text=""/>
</LinearLayout>

分割线

public class Dividerlios extends RecyclerView.ItemDecoration{
    private final static int ARRAS[] = {android.R.attr.listDivider};//使用系统提供的分隔条   当然也可以自己绘制
    private Drawable drawDivider;

    public Dividerlios(Context context){
        TypedArray a = context.obtainStyledAttributes(ARRAS);
        drawDivider = a.getDrawable(0);
        a.recycle();
    }

    public void onDrawOver(Canvas canvas, RecyclerView recyclerView){ //在canvas上画分隔条,要获得分隔条的坐标

        int left = recyclerView.getPaddingLeft();
        int right = recyclerView.getWidth() - recyclerView.getPaddingRight();

        int childCount = recyclerView.getChildCount();

        for(int i=0;i<childCount;i++){  //绘制全部的分隔条
            View child = recyclerView.getChildAt(i);
            RecyclerView.LayoutParams parame = (RecyclerView.LayoutParams)child.getLayoutParams();

            int top = child.getBottom() + parame.bottomMargin;
            int bottom = top + drawDivider.getIntrinsicHeight();
            drawDivider.setBounds(left, top, right, bottom);
            drawDivider.draw(canvas);
        }
    }
}

 

posted on 2018-02-28 14:11  爱学习的亮亮  阅读(268)  评论(0)    收藏  举报

导航