[Android Pro] 超能RecyclerView组件使用

RecyclerView最强大的功能在于秒变功能,只需要改动很少的代码就可以实现ListView,GridView及水平ListViw的切换功能

[Android Pro] 超能RecyclerView组件使用
public class MainActivity extends ActionBarActivity {
    private RecyclerView mRecyclerView;
    private List 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 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);
    }
}
[Android Pro] 超能RecyclerView组件使用

下面就是实现瀑布流操作,主要改的就是我们的适配器,要在适配器中动态计算item的高度

 

[Android Pro] 超能RecyclerView组件使用
public class WaterFlowAdapter extends Adapter {
    private Context mContext;
    private List mDatas;
    private List heightArray;


    public WaterFlowAdapter(Context mContext, List mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        heightArray=new ArrayList();
        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);   wrong

View v = LayoutInflater.from(mContext).inflate(R.layout.lv_item,paramViewGroup,false); right 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); } } }
[Android Pro] 超能RecyclerView组件使用

最后在Activity中:

mAdapter=new WaterFlowAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);

StaggeredGridLayoutManager ll=new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);//水平GridView效果,5列展示
mRecyclerView.setLayoutManager(ll);

 

分类: Android Pro
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/4827416.html如需转载请自行联系原作者

demoblog
上一篇:初创企业重新设计管理系统租用方案:添加ARM处理资源并将云计算作为主脑


下一篇:.NET快速开发实践中的IExtenderProvider扩展组件