main.xml///
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baway.recylerviewdemo.MainActivity">
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网格" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
/MainAcitivity
public class MainActivity extends BaseActivity implements View.OnClickListener {
private List<ItemBean> list = new ArrayList<>();
private Button mBt;
private RecyclerView rv;
private SwipeRefreshLayout mSrl;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBt = (Button) findViewById(R.id.bt);
mBt.setOnClickListener(this);
mSrl = (SwipeRefreshLayout) findViewById(srl);
mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(MainActivity.this, "onRefresh开始", Toast.LENGTH_SHORT).show();
//网络请求数据,然后更新列表
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "数据请求完成,并且更新了列表", Toast.LENGTH_SHORT).show();
List<ItemBean> l = new ArrayList<ItemBean>();
for (int i = 0; i < 10; i++) {
ItemBean itemBean = new ItemBean("名字" + i, "年龄" + i);
l.add(itemBean);
}
adapter.refresh(l);
mSrl.setRefreshing(false);
}
}, 3000);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt:
/* String str = mBt.getText().toString().trim();
if ("网格".equals(str)) {
show(false);
mBt.setText("垂直");
} else {
show(true);
mBt.setText("网格");
}*/
//给该列表,填加一个新的条目
adapter.addItem(new ItemBean("新的", "心底"));
break;
}
}
private void refresh() {
}
/**
* 展示页面数据
*
* @param flag
*/
private void show(boolean flag) {
//设置布局管理器
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
// rv.setLayoutManager(new GridLayoutManager(this, 2));
//添加分割线
RecyclerView.LayoutManager layoutManager = null;
if (flag) {
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
} else {
layoutManager = new GridLayoutManager(this, 2);
}
// rv.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
rv.setLayoutManager(layoutManager);
//设置分割线
rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//设置动画
rv.setItemAnimator(new DefaultItemAnimator());
//设置适配器
for (int i = 0; i < 100; i++) {
ItemBean itemBean = new ItemBean("name" + i, "age" + i);
list.add(itemBean);
}
adapter = new MyAdapter(this, list);
rv.setAdapter(adapter);
adapter.setOnItemListener(new MyAdapter.OnItemListener() {
@Override
public void onItemClick(ItemBean itemBean, int position) {
Toast.makeText(MainActivity.this, "点击:" + position + " " + itemBean.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(ItemBean itemBean, int position) {
Toast.makeText(MainActivity.this, "长按:" + position + " " + itemBean.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
///适配器/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<ItemBean> list;
private OnItemListener onItemListener;
private static int TYPE1 = 0;
private static int TYPE2 = 1;
public interface OnItemListener {
public void onItemClick(ItemBean itemBean, int position);
public void onItemLongClick(ItemBean itemBean, int position);
}
public void setOnItemListener(OnItemListener onItemListener) {
this.onItemListener = onItemListener;
}
public MyAdapter(Context context, List<ItemBean> list) {
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载写好的item布局
RecyclerView.ViewHolder viewHolder = null;
if (viewType == TYPE1) {
View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
viewHolder = new Type1Viewholder(view1);
} else {
View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
viewHolder = new Type2Viewholder(view2);
}
return viewHolder;
}
@Override
public int getItemViewType(int position) {
return position % 2 == 0 ? TYPE1 : TYPE2;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//给item布局里的组件设置值
if (holder instanceof Type1Viewholder) {
final ItemBean itemBean = list.get(position);
Type1Viewholder type1Viewholder = (Type1Viewholder) holder;
type1Viewholder.tvName.setText(itemBean.getName());
type1Viewholder.tvAge.setText(itemBean.getAge());
type1Viewholder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemListener != null) {
onItemListener.onItemClick(itemBean, position);
}
}
});
type1Viewholder.ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (onItemListener != null) {
onItemListener.onItemLongClick(itemBean, position);
}
return true;
}
});
} else {
Type2Viewholder type2Viewholder = (Type2Viewholder) holder;
}
}
@Override
public int getItemCount() {
return list.size();
}
private class Type1Viewholder extends RecyclerView.ViewHolder {
//类似listview的adapter的viewholder
private TextView tvName;
private TextView tvAge;
private LinearLayout ll;
public Type1Viewholder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvAge = itemView.findViewById(R.id.tv_age);
ll = itemView.findViewById(R.id.ll);
}
}
private class Type2Viewholder extends RecyclerView.ViewHolder {
public Type2Viewholder(View itemView) {
super(itemView);
}
}
public void refresh(List<ItemBean> l) {
list.clear();
list.addAll(l);
notifyDataSetChanged();
}
public void addItem(ItemBean itemBean) {
list.add(0, itemBean);
notifyItemInserted(0);
}
}