0引入Recyclerview的支持库
compile 'com.android.support:recyclerview-v7:23.4.0'
1效果图:
2代码:
2.1MainActivity中:
<span style="font-size:18px;">public class MainActivity extends Activity {
//
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
// RecyclerView设置展示的的样式(listView样子,gridView样子,瀑布流样子)
// listView纵向滑动样子
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
// 获取数据,向适配器传数据,绑定适配器
ArrayList<String> datas = initData();
HomeAdapter honmeAdapter = new HomeAdapter(MainActivity.this,datas);
mRecyclerView.setAdapter(honmeAdapter);
// 调用按钮返回事件回调的方法
honmeAdapter.buttonSetOnclick(new HomeAdapter.ButtonInterface() {
@Override
public void onclick(View view, int position) {
Toast.makeText(MainActivity.this, "点击条目上的按钮"+position, Toast.LENGTH_SHORT).show();
}
});
}
/**
* 编写一套假数据
*/
protected ArrayList<String> initData() {
ArrayList<String> mDatas = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
mDatas.add("我是条目" + i);
}
return mDatas;
}
}</span>
2.2布局文件:activity_main.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
</LinearLayout></span>
2.3条目的布局文件:item_home.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#44ff0000"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/id_num"
android:layout_width="0dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_weight="1"
/>
<Button
android:id="@+id/id_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按钮"
android:layout_weight="1"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000033"
/>
</LinearLayout></span>
3适配器中HomeAdapter
<span style="font-size:18px;">class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
private Context context;
private ArrayList<String> list;
private ButtonInterface buttonInterface;
public HomeAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
/**
*按钮点击事件需要的方法
*/
public void buttonSetOnclick(ButtonInterface buttonInterface){
this.buttonInterface=buttonInterface;
}
/**
* 按钮点击事件对应的接口
*/
public interface ButtonInterface{
public void onclick( View view,int position);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
context).inflate(R.layout.item_home, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv.setText(list.get(position));
holder.id_button.setText(list.get(position));
holder.id_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(buttonInterface!=null) {
// 接口实例化后的而对象,调用重写后的方法
buttonInterface.onclick(v,position);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
/**
* ViewHolder的类,用于缓存控件
*/
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
Button id_button;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.id_num);
id_button = (Button) itemView.findViewById(R.id.id_button);
}
}
}</span>
---------------------
作者:zhaihaohao1
来源:CSDN
原文:https://blog.csdn.net/zhaihaohao1/article/details/51992343
版权声明:本文为博主原创文章,转载请附上博文链接!