- recycleView常用的几种样式布局:
* LinearLayoutManager 线性布局管理器 (ListView效果)
* GridLayoutManager 网格布局管理器 (GridView效果)
* StaggeredGridLayoutManager 瀑布流管理器
- 关于recycleview 的使用方法
首先自定义一个recycleViewAdapter 如下:
package com.example.think.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.think.myapplication.R;
import java.util.ArrayList;
/**
* Created by HuangMei on 2016/8/3.
*/
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
Context context;
ArrayList<String> arrayList = new ArrayList<>();
public RecycleViewAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.recycler_item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(arrayList.get(position));
//以下几行是为瀑布流设置高度使用的(备注:如果不是瀑布流的时候不需要添加)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,100+(int) (Math.random() * 50));
holder.imageView.setLayoutParams(lp);
}
@Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
}
然后在自己的活动里面调用自己的自定义适配器
public class MainActivity extends Activity {
ArrayList<String> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recylcer = (RecyclerView) findViewById(R.id.recyclerView);
//ListView效果的 LinearLayoutManager
// LinearLayoutManager mgr = new LinearLayoutManager(this); //备注:listview样式和九宫格样式公用
// //VERTICAL纵向,类似ListView,HORIZONTA 横向,类似Gallery
// mgr.setOrientation(LinearLayoutManager.HORIZONTAL);//备注:也可以是VERTICAL
//GridLayout 3列
// GridLayoutManager mgr=new GridLayoutManager(this,3);
StaggeredGridLayoutManager mgr = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recylcer.setLayoutManager(mgr);
//设置适配器
recylcer.setAdapter(new RecycleViewAdapter(this, setData(10)));
}
ArrayList<String> setData(int n){
for(int i = 0; i < n; i ++){
arrayList.add(i + "" + i + "" + i + "" + i + "" + i);
}
return arrayList;
}
}