編輯:關於Android編程
RecyclerView最強大的功能在於秒變功能,只需要改動很少的代碼就可以實現ListView,GridView及水平ListViw的切換功能
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private List
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
private List
public WaterFlowAdapter(Context mContext, List
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);
在平時開發中柱狀形狀偶爾會有項目需求,由於又一次開發需要,去找開源的組件都不好用,要不調用很不方便,要不它們類庫太笨重,下面介紹一個輕巧方便控件,原理就是用View 重新
說到工程模式,好多非技術流的玩家都很頭疼。手機工程模式給人的印象就是生硬的黑白屏,全屏的英文和代碼命令,就像視窗重新回到了DOS系統,雖然好奇但又怕手機變磚
高斯模糊、加載監聽、圓角圖片這些相信大家都很熟悉,那如何實現這些效果,請大家參考本文進行學習。1、引用compile com.github.bumptech.glide:
本文實例講述了Android簡單創建一個Activity的方法。分享給大家供大家參考,具體如下:1) 創建一個android項目填寫項目信息2) 創建一個新Activit