RecyclerView-点击切换多种布局
来源:互联网 发布:金扎软件多少钱 编辑:程序博客网 时间:2024/05/17 02:17
布局main
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_tianjia" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="Click" android:text="添加" /> <Button android:id="@+id/button_shanchu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="Click" android:text="删除" /> <Button android:id="@+id/button_xianxing" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="Click" android:text="线性" /> <Button android:id="@+id/button_wangge" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="Click" android:text="网格" /> <Button android:id="@+id/button_pubu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="Click" android:text="瀑布流" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>
布局 itme
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" /></RelativeLayout>
代码 MainActivity
private RecyclerView mRecyclerView;private List<String> mList;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); adapter = new MyAdapter(); mRecyclerView.setAdapter(adapter); mList = new ArrayList<>(); for (int i = 0; i < 130; i++) { mList.add("数据" + i); }}public void Click(View view){ switch (view.getId()){ case R.id.button_tianjia: mList.add("添加一个"); adapter.notifyDataSetChanged(); break; case R.id.button_shanchu: mList.remove(1); adapter.notifyDataSetChanged(); break; case R.id.button_xianxing: mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); break; case R.id.button_wangge: mRecyclerView.setLayoutManager(new GridLayoutManager(this,2)); break; case R.id.button_pubu: mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)); break; }}
适配器MyAdapterprivate class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(MainActivity.this, R.layout.item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.textview.setText(mList.get(position)); } @Override public int getItemCount() { return mList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private final TextView textview; public MyViewHolder(View itemView) { super(itemView); textview = (TextView) itemView.findViewById(R.id.textView); } }}线性布局上拉加载更多//上拉加载mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); //设置加载的状态 // adapter.changeMoreStatus(RecyclerViewAdapter.LOADING_MORE); //判断到底部的条件 if (newState == RecyclerView.SCROLL_STATE_IDLE && linearLayoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) { Log.e("--------->", "到底"); //可以直接addAll for (int i = 0; i < 30; i++) { mList.add("测试数据hhhhhhh:" + i); } //刷新数据 adapter.notifyDataSetChanged(); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); }});
阅读全文
0 0
- RecyclerView-点击切换多种布局
- RecyclerView点击切换布局
- RecyclerView点击切换布局
- RecyclerView使用(二)多种Item布局、添加点击事件
- RecyclerView添加多种布局
- RecyclerView中的多种布局
- RecyclerView中的多种布局
- RecyclerView中的多种布局
- RecyclerView展示多种布局
- RecyclerView多种item布局
- RecyclerView+ButterKnife点击按钮进行布局切换与添加删除
- RecyclerView实现点击切换
- RecyclerView实现多种item布局
- 优雅实现RecyclerView多种布局
- RecyclerView显示多种Item布局
- RecyclerView实现多种布局样式
- RecyclerView的多种布局MultiItemTypeAdapter
- RecyclerView使用、上拉加载、局部刷新、多种布局、点击事件和坑
- 使用Java实现Xmodem协议
- easyui-textbox与easyui-combobox的onChange事件
- 弹出层随屏滚动
- 视频质量测试
- 详解spring 每个jar的作用
- RecyclerView-点击切换多种布局
- minicom无法输入问题
- yaf框架学习文件配置
- leetcode 27
- Java中new Thread的弊端及Java四种线程池的使用
- overview_java(十三)-多线程
- 后台out.println向jsp页面推送中文时乱码
- adb 命令记录
- linux 外部访问不了 处理思路