RecyclerView增 删
来源:互联网 发布:诸葛亮 司马懿 知乎 编辑:程序博客网 时间:2024/06/05 10:51
、、、、、、、、、、、对应相关主布局<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/b2" android:layout_toRightOf="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/b3" android:layout_toRightOf="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="list" /> <Button android:id="@+id/b4" android:layout_toRightOf="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group" /> <Button android:id="@+id/b5" android:layout_toRightOf="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PB" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_below="@+id/one" android:id="@+id/id_recyclerview" android:divider="#ffff0000" android:dividerHeight="10dp" android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>public class MainActivity extends AppCompatActivity implements View.OnClickListener{ String uri = "http://api.tianapi.com/social/?key=71e58b5b2f930eaf1f937407acde08fe&num=20"; TextView id_num ,tv; ImageView img; private RecyclerView mRecyclerView; private HomeAdapter mAdapter; List<News.NewslistBean> mDatas; ImageLoader imagerloder; Button b1,b2,b3,b4,b5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); id_num=(TextView)findViewById(R.id.id_num); img= (ImageView)findViewById(R.id.img); tv=(TextView)findViewById(R.id.tv); b1=(Button)findViewById(R.id.b1) ; b2=(Button)findViewById(R.id.b2) ; b3=(Button)findViewById(R.id.b3) ; b4=(Button)findViewById(R.id.b4) ; b5=(Button)findViewById(R.id.b5) ; mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); //listview的效果 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //下划线 mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); //groupview的效果 /* mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));*/ //瀑布效果 /* mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL));*/ //网络判断 boolean netWorkAvailable = NetWorkUtils.isNetWorkAvailable(this); if(!netWorkAvailable){ Toast.makeText(MainActivity.this, "联网:" + netWorkAvailable, Toast.LENGTH_SHORT).show(); } getData(); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); b4.setOnClickListener(this); b5.setOnClickListener(this); } public void getData() { OkHttp3Utils.getInstance().doGet(uri, new GsonObjectCallback<News>() { @Override public void onUi(News news) { mDatas = news.getNewslist(); mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); } @Override public void onFailed(Call call, IOException e) { } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.b1: mAdapter.addData(1); break; case R.id.b2: mAdapter.removeData(1); break; case R.id.b3: mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); break; case R.id.b4: mRecyclerView.setLayoutManager(new GridLayoutManager(this,3)); break; case R.id.b5: mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL)); break; } } class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( MainActivity.this).inflate(R.layout.item, parent, false)); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.id_num.setText(mDatas.get(position).getDescription()); holder.tv.setText(mDatas.get(position).getTitle()); imagerloder =ImageLoader.getInstance(); imagerloder.displayImage(mDatas.get(position).getPicUrl(),holder.img); // 获得该控件的所有属性 ViewGroup.LayoutParams layoutParams = holder.tv.getLayoutParams(); Random random = new Random(); layoutParams.height = random.nextInt(200)+50; holder.tv.setLayoutParams(layoutParams); } @Override public int getItemCount() { return mDatas.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView id_num; ImageView img; TextView tv; public MyViewHolder(View view) { super(view); tv = (TextView)view.findViewById(R.id.tv); id_num = (TextView) view.findViewById(R.id.id_num); img = (ImageView)view.findViewById(R.id.img); } } /*添加和删除的方法*/ public void addData(int position) { mDatas.add(position, new News.NewslistBean("sss","sss",null)); notifyItemInserted(position); } public void removeData(int position) { mDatas.remove(position); notifyItemRemoved(position); } }
阅读全文
0 0
- RecyclerView增 删
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- RecyclerView
- 学习MINA
- 15. 机器学习基石
- Spring boot 入门程序详解
- struts2简介
- 获取iframe中的内容、查找获取指定元素(关于用c++调用WEBBROWSER控件,使用相关接口操作web页面元素的一些方法)
- RecyclerView增 删
- 移动端滚动穿透问题完美解决方案
- oracle的锁和锁的分类
- Oracle从创建用户到导入导出数据
- 51 nod 1022 石子归并 V2(dp决策单调性)
- Python 读取.xml数据集
- 多线程MFC处理视频
- Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with multiple
- hdu 4004 The Frog's Games