、、、、、、、、、、、对应相关主布局<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); } }
RecyclerView增 删
最新推荐文章于 2023-04-24 14:59:57 发布