okhttp的依赖 compile 'com.squareup.okhttp3:okhttp:3.4.2' implementation 'com.android.support:recyclerview-v7:26.+'
public class MainActivity extends AppCompatActivity {
private String path = "https://www.zhaoapi.cn/product/getCatagory";
private RecyclerView recyclerView;
private Button pubu;
private Button listview;
private Button gridview;
private Button add;
private Button delect;
private List<ReaBean.DataBean> data;
private Handler handler = new Handler() {
@Override public void handleMessage(Message msg) {
super.handleMessage( msg );
data = (List<ReaBean.DataBean>) msg.obj;
myRecyclerAdapter = new MyRecyclerAdapter( data, MainActivity.this );
recyclerView.setAdapter( myRecyclerAdapter );
//点击条目 myRecyclerAdapter.setOnItemClickListener( new MyRecyclerAdapter.OnItemClickListener() {
@Override public void onClick(int position) {
Toast.makeText( MainActivity.this, "点击" + position + "行", Toast.LENGTH_SHORT ).show(); }//点击条目删除 @Override
public void onLongClick(int position) {
myRecyclerAdapter.delect( position );
myRecyclerAdapter.notifyDataSetChanged(); } } ); } };
private MyRecyclerAdapter myRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
initByid();
initData();
recyclerView.setLayoutManager( new LinearLayoutManager( MainActivity.this, LinearLayoutManager.VERTICAL, false ) ); }
private void initData() {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url( path ).build();
Call call = okHttpClient.newCall( request );
call.enqueue( new Callback() {
@Override public void onFailure(Call call, IOException e) {
} @Override
public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Gson gson = new Gson();
ReaBean reaBean = gson.fromJson( string, ReaBean.class );
List<ReaBean.DataBean> data = reaBean.getData();
Message obtain = Message.obtain();
obtain.obj = data;
handler.sendMessage( obtain );
} } ); }
private void initByid() {
add = findViewById( R.id.add );
delect = findViewById( R.id.delect );
listview = findViewById( R.id.list );
gridview = findViewById( R.id.grid );
pubu = findViewById( R.id.pubu );
recyclerView = findViewById( R.id.recycler );
//垂直布局 listview.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) { recyclerView.setLayoutManager( new LinearLayoutManager( MainActivity.this, LinearLayoutManager.VERTICAL, true ) ); } } );
//表格 gridview.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) { recyclerView.setLayoutManager( new GridLayoutManager( MainActivity.this, 2 ) ); } } ); //瀑布
pubu.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.setLayoutManager( new StaggeredGridLayoutManager( 3, StaggeredGridLayoutManager.VERTICAL ) ); } } );
//添加
add.setOnClickListener( new View.OnClickListener() {
@Override public void onClick(View v) {
myRecyclerAdapter.addItem( 1 ); } } );
//删除 delect.setOnClickListener(
new View.OnClickListener() { @Override public void onClick(View v) {
myRecyclerAdapter.delect( 1 );
} } ); }}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { List<ReaBean.DataBean> list; Context context; private final DisplayImageOptions options; public MyRecyclerAdapter(List<ReaBean.DataBean> list, Context context) { this.list = list; this.context = context; options = new DisplayImageOptions.Builder().cacheInMemory( true )//使用内存缓存 .cacheOnDisk( true )//使用磁盘缓存 .bitmapConfig( Bitmap.Config.RGB_565 )//设置图片色彩模式 .imageScaleType( ImageScaleType.EXACTLY )//设置图片的缩放模式 .showImageOnLoading( R.mipmap.ic_launcher )//设置正在下载的图片 .showImageForEmptyUri( R.mipmap.ic_launcher )//url为空或请求的资源不存在时 .showImageOnFail( R.mipmap.ic_launcher )//下载失败时显示的图片 // .displayer(new RoundedBitmapDisplayer(50))//设置圆角 .build(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate( context, R.layout.item, null ); ViewHolder viewHolder = new ViewHolder( inflate ); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { ViewHolder viewHolder = holder; String name = list.get( position ).getName(); viewHolder.textView.setText( name ); viewHolder.textView1.setText( list.get( position ).getCreatetime() ); ImageLoader.getInstance().displayImage( list.get( position ).getIcon(), viewHolder.img, options ); if (mOnItemClickListener != null) { viewHolder.itemView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onClick( position ); } } ); viewHolder.itemView.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mOnItemClickListener.onLongClick( position ); return true; } } ); } } @Override public int getItemCount() { return list.size(); } private OnItemClickListener mOnItemClickListener;//声明接口 public interface OnItemClickListener { void onClick(int position); void onLongClick(int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.mOnItemClickListener = onItemClickListener; } class ViewHolder extends RecyclerView.ViewHolder { private final TextView textView; private final ImageView img; private final TextView textView1; public ViewHolder(View itemView) { super( itemView ); textView = itemView.findViewById( R.id.tv ); textView1 = itemView.findViewById( R.id.tvv ); img = itemView.findViewById( R.id.img ); } } public void addItem(int position) { list.add( position, list.get( position ) ); notifyItemInserted( position ); } public void delect(int position) { list.remove( position ); notifyItemRemoved( position ); } }
public interface OnItemClickListener { void onItemClick(View view, int position); void onItemLongClick(int position); }
<LinearLayout 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" android:orientation="vertical" tools:context="com.example.recycleview.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add" android:text="添加"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/delect" android:text="删除"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="垂直布局" android:id="@+id/list"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="表格布局" android:id="@+id/grid"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="瀑布布局" android:id="@+id/pubu"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler"></android.support.v7.widget.RecyclerView> </LinearLayout>item布局简单,没有复制