RecyclerView的使用和样式
来源:互联网 发布:github tensorflow 编辑:程序博客网 时间:2024/06/03 04:38
今天写了个demo,发现了很多问题。。
小小的RecyclerView也不是那么好用的。。
Java代码:
public class MainActivity extends AppCompatActivity { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// rv = new RecyclerView(this);// setContentView(rv); setContentView(R.layout.activity_main); rv=(RecyclerView) findViewById(R.id.recycler); /** * 无论是new RecyclerView对象还是使用XML布局,都要用布局管理器。 * 如下代码,不使用管理器就不显示数据 */// rv.setLayoutManager(new LinearLayoutManager(this)); //第二项是滚动方向,第三项是是否反转,就是是不是从右到左。 rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)); rv.setAdapter(new MyAdapter()); }}
XML布局:
<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.qihao.day621.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
适配器adapter:
/** * Created by qihao on 2016/6/21. */public class MyAdapter extends RecyclerView.Adapter { class viewHolder extends RecyclerView.ViewHolder { private View root; private TextView title ,text; public viewHolder(View root) { super(root); title=(TextView) root.findViewById(R.id.title_view); text=(TextView) root.findViewById(R.id.text_cont); } public TextView getText() { return text; } public TextView getTitle() { return title; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new viewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,null)); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { viewHolder vh = (viewHolder) holder; vh.getText().setText("内容"+position); vh.getTitle().setText("标题"+position); } @Override public int getItemCount() { return 30; } //private String[] data = new String[]{"111", "222", "3333"};}
item布局:
<?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"> <TextView android:id="@+id/title_view" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_cont" android:textSize="14sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
demo源码下载:http://download.csdn.net/detail/gfg156196/9555621
1 0
- RecyclerView的使用和样式
- Android RecyclerView 详解(四) RecyclerView分割线的使用和分割线样式
- RecyclerView样式布局文件的简单使用
- RecyclerView的布局样式
- RecyclerView的样式
- RecyclerView和SwipeRefreshLayout的使用
- RecyclerView 的研究和使用
- RecyclerView和CardView的使用
- RecyclerView的使用和封装
- RecyclerView的使用和优化
- Android样式的使用和样式继承
- RecyclerView+RecyclerView.Adapter+RecyclerView.ViewHolder的使用
- Android RecyclerView和CardView的结合使用
- SwipeRefreshLayout和RecyclerView使用的小问题
- RecyclerView和CardView的使用以及注意
- ScrolView和RecyclerView嵌套使用的问题
- RecyclerView和CardView的结合使用
- SwipeRefreshLayout和RecyclerView的结合使用
- Android之测试代码实现步骤:
- Redux各知识点介绍
- MySQL性能优化的最佳21条经验
- jquery代码实现通用选项卡特效功能
- JHTP小结_第五章_控制语句Part 2-逻辑运算符
- RecyclerView的使用和样式
- Android蓝牙开发
- flask学习笔记(-结构)
- 国内源安装 nodejs与npm
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
- jquery选择器
- --获取约束信息
- js判断节点是否存在
- 单链表插入(完整版程序 在表头和表尾插入算法)