Recycleview的使用

一:用Recycleview相比传统Listview,使用更方便,效率也更快,方式也多样化一些。recyclevie样式可以横向显示,还可以使用表格布局,在横向显示这里就能把Listview给搞废了。


二:使用1:设置
先Model设置里找到包,因为recycleview使用的是recycleview-v7包

直接上代码 demo
public class MainActivity extends Activity {
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


recyclerView = new RecyclerView(this);
setContentView(recyclerView);

//线性布局默认是竖直方向
// recyclerView.setLayoutManager(new LinearLayoutManager(this));
//区别于传统Listview的水平方向 第一个参数一样,第二个参数设置水平,第三个参数设置是否反转
// recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));


//表格布局 第二个参数,列数 同样默认是竖直方向的
// recyclerView.setLayoutManager(new GridLayoutManager(this,3));
//水平方向,参数与线性布局雷同,水平的时候就是行数
recyclerView.setLayoutManager(new GridLayoutManager(this,3,LinearLayoutManager.HORIZONTAL,false));
recyclerView.setAdapter(new MyAdapter());
}


}
public class MyAdapter extends RecyclerView.Adapter {

private TextBean[] tb = new TextBean[]{new TextBean("一一", "一一逗比"),
new TextBean("", "二二笔"),new TextBean("", "三都比"),
new TextBean("", "三都比"),new TextBean("", "三都比"),
new TextBean("", "三都比"),new TextBean("", "三都比"),
new TextBean("", "三都比"),new TextBean("", "三都比"),
new TextBean("", "三都比"),new TextBean("", "三都比"),
new TextBean("", "三都比"),new TextBean("", "三都比")};

class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_tittle, tv_content;

public ViewHolder(View view) {
super(view);
tv_tittle = (TextView) view.findViewById(R.id.tv_tittle);
tv_content = (TextView) view.findViewById(R.id.tv_content);
}

public TextView getTv_content() {
return tv_content;
}

public TextView getTv_tittle() {
return tv_tittle;
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//用布局填充器 null为布局更对象,这里传null
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclevie_list_item, null));
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder vh = (ViewHolder) holder
;
TextBean tb_temp=tb[position];
vh.getTv_tittle().setText(tb_temp.getTittle());
vh.getTv_content().setText(tb_temp.getContent());

}

@Override
public int getItemCount() {
return tb.length;
}


}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toos="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/tv_tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
toos:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
toos:text="New Text" />

</LinearLayout>




大家一起可以交流下,共同进步。安卓群号:513210053
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值