例:
OkHttp网络请求数据
RecyclerView展示数据
三种展示(列表,表格,瀑布)
实现 添加,删除 功能
添加依赖:
dependencies { implementation 'com.android.support:recyclerview-v7:26.1.0' compile 'com.squareup.okhttp3:okhttp:3.4.2' compile 'com.google.code.gson:gson:2.2.4' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' }
添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity主要类:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final int SUCCESS = 492; private MyAdapter myAdapter; private List<LogoBean.DataBean.PcFeedFocusBean> list; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case SUCCESS: String json = (String) msg.obj; Gson gson = new Gson(); LogoBean logoBean = gson.fromJson(json, LogoBean.class); list = logoBean.getData().getPc_feed_focus(); myAdapter = new MyAdapter(list, MainActivity.this); rv.setAdapter(myAdapter); } } }; private RecyclerView rv; private Button ll; private Button bg; private Button pb; private Button add; private Button select; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 initadd(); rv = findViewById(R.id.rv);//RecyclerView } private void initadd() { ll = findViewById(R.id.ll);//线性布局 bg = findViewById(R.id.bg);//表格布局 pb = findViewById(R.id.pb);//瀑布流布局 add = findViewById(R.id.add);//添加方法 select = findViewById(R.id.select);//删除方法 //按钮的监听 ll.setOnClickListener(this); bg.setOnClickListener(this); pb.setOnClickListener(this); add.setOnClickListener(this); select.setOnClickListener(this); } //按钮的监听 @Override public void onClick(View view) { switch (view.getId()){ case R.id.ll: getpostll(); //线性布局管理器 rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); break; case R.id.bg: getpostll(); //表格布局管理器 rv.setLayoutManager(new GridLayoutManager(this,3)); break; case R.id.pb: getpostll(); //瀑布式布局管理器 rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.