本文主要是介绍Android学习笔记:超能RecyclerView组件使用(中)---秒变,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RecyclerView最强大的功能在于秒变功能,只需要改动很少的代码就可以实现ListView,GridView及水平ListViw的切换功能
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private List<TestData> mDatas;
private TestAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
initViews();
}
private void initDatas() {
mDatas=new ArrayList<TestData>();
TestData data=new TestData();
for (int i = 'A'; i < 'z'; i++) {
data.setTitle("Title"+(char)i);
data.setContent("Content"+(char)i);
mDatas.add(data);
}
}
private void initViews() {
mRecyclerView=(RecyclerView) findViewById(R.id.test_rcv);
mAdapter=new TestAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
/************水平ListView效果****************************************/
LinearLayoutManager ll=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
mRecyclerView.setLayoutManager(ll);
/************垂直ListView效果****************************************/
LinearLayoutManager ll=new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);//实现ListView效果
mRecyclerView.setLayoutManager(ll);
/************水平滚动的GridView效果****************************************/
StaggeredGridLayoutManager ll=new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL);//水平GridView效果,5列展示
mRecyclerView.setLayoutManager(ll);
/************GridView效果****************************************/
GridLayoutManager ll=new GridLayoutManager(this, 4);//4列展示
mRecyclerView.setLayoutManager(ll);
}
}
下面就是实现瀑布流操作,主要改的就是我们的适配器,要在适配器中动态计算item的高度
public class WaterFlowAdapter extends Adapter<WaterFlowAdapter.TestViewHolder> {
private Context mContext;
private List<TestData> mDatas;
private List<Integer> heightArray;
public WaterFlowAdapter(Context mContext, List<TestData> mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
heightArray=new ArrayList<Integer>();
for (int i = 0; i < mDatas.size(); i++) {
heightArray.add((int)(100+Math.random()*300));
}
}
@Override
public WaterFlowAdapter.TestViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
View v = LayoutInflater.from(mContext).inflate(R.layout.lv_item, null);
TestViewHolder holder = new WaterFlowAdapter.TestViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(WaterFlowAdapter.TestViewHolder paramVH, int paramInt) {
LayoutParams lp=paramVH.itemView.getLayoutParams();
lp.height=heightArray.get(paramInt);
paramVH.itemView.setLayoutParams(lp);
paramVH.titleTv.setText(mDatas.get(paramInt).getTitle());
paramVH.contentTv.setText(mDatas.get(paramInt).getContent());
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return mDatas.size();
}
class TestViewHolder extends ViewHolder {
ImageView iconIv;
TextView titleTv;
TextView contentTv;
public TestViewHolder(View holder) {
super(holder);
iconIv = (ImageView) holder.findViewById(R.id.item_iv);
titleTv = (TextView) holder.findViewById(R.id.item_title);
contentTv = (TextView) holder.findViewById(R.id.item_content);
}
}
}
最后在Activity中:
mAdapter=new WaterFlowAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
StaggeredGridLayoutManager ll=new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);//水平GridView效果,5列展示
mRecyclerView.setLayoutManager(ll);
这篇关于Android学习笔记:超能RecyclerView组件使用(中)---秒变的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!