主页面
package com.example.rk_recycleview; import android.annotation.SuppressLint; import android.content.DialogInterface; import android.os.Message; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.widget.Toast; import com.google.gson.Gson; import java.io.IOException; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final int SUCCESS = 492; private View ll; private View bg; private View pb; private MyAdapter adapter; private List<SuperClass.DataBean> list; @SuppressLint("HandlerLeak") //使用handler进行gson解析 Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String json = (String) msg.obj; Gson gson = new Gson(); SuperClass superClass = gson.fromJson(json, SuperClass.class); list = superClass.getData(); adapter = new MyAdapter(list,MainActivity.this); rv.setAdapter(adapter); } }; private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 initView(); } private void initView() { ll = findViewById(R.id.ll); bg = findViewById(R.id.bg); pb = findViewById(R.id.pb); rv = findViewById(R.id.rv); ll.setOnClickListener(this); bg.setOnClickListener(this); pb.setOnClickListener(this); //长按删除 rv.addOnItemTouchListener(new RecyclerViewClickListener(this, new RecyclerViewClickListener.OnItemClickListener() { @Override public void onItemClick(View view, final int position) { //设置提示框 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //提示内容 builder.setMessage("是否删除?"); //提示头 builder.setTitle("提示"); //确定按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //吐司 Toast.makeText(MainActivity.this,"已删除",Toast.LENGTH_SHORT).show(); //关闭弹框 dialog.dismiss(); //删除 list.remove(position); //刷新适配器 adapter.notifyDataSetChanged(); } }); //取消按钮 builder.setNegativeButton("取消",null); //执行展示 builder.create().show(); } @Override public void onItemLongClick(View view, final int position) { } })); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ll: getpost(); //线性布局 rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); break; case R.id.bg: //表格布局 rv.setLayoutManager(new GridLayoutManager(this,3)); break; case R.id.pb: //瀑布式布局 rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); br