Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習筆記:超能RecyclerView組件使用(中)---秒變

Android學習筆記:超能RecyclerView組件使用(中)---秒變

編輯:關於Android編程

RecyclerView最強大的功能在於秒變功能,只需要改動很少的代碼就可以實現ListView,GridView及水平ListViw的切換功能

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);
}
}

下面就是實現瀑布流操作,主要改的就是我們的適配器,要在適配器中動態計算item的高度

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);
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);

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved