RecyclerView和XRecyclerView的布局和实现

需要导的依赖是:
1》 implementation(‘com.jcodecraeer:xrecyclerview:1.5.9’) {
exclude group: ‘com.android.support’
}
2》implementation ‘com.google.code.gson:gson:2.8.5’
3》 implementation ‘com.squareup.okhttp3:okhttp:3.12.0’
4》 implementation ‘com.mcxiaoke.next:recycler:1.5.3’
//RecyclerView和XRecyclerView的布局



////RecyclerView的MyAdapter
package com.example.day01.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day01.R;
import com.example.day01.bean.MyData;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter {
private ArrayList list;
private Context context;
private ViewHolder holder;

public MyAdapter(ArrayList list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.recy_list, null);
    holder = new ViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    MyData.DataBean dataBean=list.get(position);
    holder.textRecy.setText(dataBean.getName());
}


@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    private TextView textRecy;
    private ImageView imageRecy;
    public ViewHolder(View itemView) {
        super(itemView);
        textRecy=itemView.findViewById(R.id.textRecy);
        imageRecy=itemView.findViewById(R.id.imageRecy);

    }
}

}

XRecyclerView的Xadapter
package com.example.day01.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day01.R;
import com.example.day01.bean.MyData;

import java.util.ArrayList;

public class Xadapter extends RecyclerView.Adapter {

private ArrayList mlist;
private Context context;

public Xadapter(ArrayList mlist, Context context) {
    this.mlist = mlist;
    this.context = context;
}


@NonNull
@Override
public Xadapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view=LayoutInflater.from(context).inflate(R.layout.xrecy_list,null);
    ViewHolder holder=new ViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(@NonNull Xadapter.ViewHolder holder, int position) {
MyData.DataBean dataBean=mlist.get(position);
holder.textXrecy.setText(dataBean.getCreatetime());
}

@Override
public int getItemCount() {
    return mlist.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView textXrecy;
    private ImageView imageXrey;
    public ViewHolder(View itemView) {
        super(itemView);
        textXrecy=itemView.findViewById(R.id.textXrecy);
        imageXrey=itemView.findViewById(R.id.imageXrey);

    }
}

}

//RecyclerView和XRecyclerView的布局和实现的Mainactivity
public class MainActivity extends AppCompatActivity implements IVew {
private String mUrl = “http://www.zhaoapi.cn/product/getCatagory?page=”;
private ArrayList list = new ArrayList<>();
private ArrayList mlist = new ArrayList<>();
private RecyclerView recy;
private XRecyclerView xrecy;
private PerenerImpl perener;
private int i=1;
private MyAdapter adapter;
private GridLayoutManager manager;
private Xadapter xadapter;
private LinearLayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    perener = new PerenerImpl(this);
    perener.startRequest(mUrl,i);

    adapter = new MyAdapter(list,this);
    xadapter = new Xadapter(mlist,this);
    xrecy.setAdapter(xadapter);
    recy.setAdapter(adapter);

    manager = new GridLayoutManager(this,4);
    layoutManager = new LinearLayoutManager(this);
    xrecy.setLayoutManager(layoutManager);
    recy.setLayoutManager(manager);
}

@Override
public void setSuccess(MyData data) {
    list.addAll(data.getData());
    mlist.addAll(data.getData());
    xrecy.setLoadingListener(new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            mlist.clear();
            i=1;
            perener.startRequest(mUrl,i);
            xrecy.refreshComplete();
        }

        @Override
        public void onLoadMore() {

i++;
perener.startRequest(mUrl,i);
xrecy.refreshComplete();;
}
});
adapter.notifyDataSetChanged();
}

@Override
public void setError(String error) {

}

private void initView() {
    recy = (RecyclerView) findViewById(R.id.recy);
    xrecy = (XRecyclerView) findViewById(R.id.xrecy);
    xrecy.setPullRefreshEnabled(true);
    xrecy.setLoadingMoreEnabled(true);
}

}

你可能感兴趣的:(RecyclerView和XRecyclerView的布局和实现)