編輯:關於Android編程
RecyclerView 是 android-support-v7-21 版本中新增的一個 Widgets。我們可以使用RecyclerView非常簡單的實現橫向豎向的ListView.GridView.以及瀑布流的效果.先看一下項目的實現的效果
是不是很炫?使用RecyclerView可以非常簡單的實現.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPlJlY3ljbGVyVmlldyDM2NDUPGJyIC8+DQoxLrK7udjQxEl0ZW3Kx7fxz9TKvtTa1f3It7XEzrvWw9LUvLDI57rOz9TKvjxiciAvPg0KMi6yu7nY0MRpdGVt1q685Mjnus631rjuPGJyIC8+DQozLrK7udjXokl0ZW3U9rzT0+vJvrP9tcS2r7ut0Ke5+zxiciAvPg0KNC7L/L32vfbWu8rHudjXosjnus672MrVus24tNPDaXRlbTxiciAvPg0KzPmz9rK/t9a0+sLro7o8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;">
mRecyclerView = (RecyclerView) findViewById(R.id.rv);
//設置它的Adapter
myRecyclerViewListAdapater = new MyRecyclerViewListAdapater(this,sourceDatas);
mRecyclerView.setAdapter(myRecyclerViewListAdapater);
//設置它的LayoutManage , 通過 LayoutManager去設置它的顯示方式
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(linearLayoutManager);
//設置分割線
// dividerItemDecoration = new DividerItemDecoration(this,LinearLayoutManager.VERTICAL);
// mRecyclerView.addItemDecoration(dividerItemDecoration);
//設置增加刪除Item的動畫
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
myRecyclerViewListAdapater.setOnItemClickListener(new MyRecyclerViewListAdapater.OnItemClickListener() {
@Override
public void onItemClickListener(View view, int position) {
Toast.makeText(MainActivity.this,click : + position,Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongItemClickListener(View view, int position) {
myRecyclerViewListAdapater.deleteItem(position);
return false;
}
});
Adapter代碼
public class MyRecyclerViewListAdapater extends RecyclerView.Adapter {
private Context context;
protected List sourceDatas;
private LayoutInflater layoutInflater;
public OnItemClickListener mOnItemClickListener;
public MyRecyclerViewListAdapater(Context context,List sourceDatas){
this.context = context;
this.sourceDatas = sourceDatas;
layoutInflater = LayoutInflater.from(context);
}
@Override
public MyRecyclerViewListAdapater.MyListHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = layoutInflater.inflate(R.layout.item,viewGroup,false);
MyListHolder myListHolder = new MyListHolder(view);
return myListHolder;
}
@Override
public void onBindViewHolder(final MyListHolder viewHolder, final int i) {
viewHolder.textView.setText(sourceDatas.get(i));
itemClick(viewHolder);
}
/**
* ItemView點擊事件
*/
protected void itemClick(final MyListHolder viewHolder){
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null != mOnItemClickListener){
int postion = viewHolder.getLayoutPosition();
mOnItemClickListener.onItemClickListener(viewHolder.itemView,postion);
}
}
});
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(null != mOnItemClickListener){
int position = viewHolder.getLayoutPosition();
return mOnItemClickListener.onLongItemClickListener(viewHolder.itemView,position);
}
return false;
}
});
}
/**
* 方法返回item的數量
* */
@Override
public int getItemCount() {
return sourceDatas.size();
}
/**
* 添加item
*/
public void addItem(int position){
sourceDatas.add(position,Add Item);
notifyItemInserted(position);
}
/**
* 刪除Item
*/
public void deleteItem(int positon){
sourceDatas.remove(positon);
notifyItemRemoved(positon);
}
/**
* 設置item點擊接口
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.mOnItemClickListener = onItemClickListener;
}
/**
* Recycler本身並沒有給我們提供ItemClick事件,這裡需要我們自己實現這個接口
* 點擊接口
* */
public interface OnItemClickListener{
//點擊事件
public void onItemClickListener(View view,int position);
//長按事件
public boolean onLongItemClickListener(View view,int position);
}
class MyListHolder extends RecyclerView.ViewHolder{
TextView textView;
//繼承 RecyclerView.ViewHolder 必須要實現它的構造方法
//可以看到在它的構造方法接收到一個View的參數,這個View就是我們創建的ItemView
public MyListHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.tv_list_item);
}
}
}
布局:
什麼是emoji表情emoji表情是一種表情符號,在代碼中它現在其實是一組遵循Unicode的編碼,即每一個表情符號都對應了一個Unicode編碼。更進一步說,emoji
本文實例介紹了Android實現粒子雨效果的實現過程,分享給大家供大家參考,具體內容如下先看看效果圖:具體實現方法:1.baseview主要是設定雨滴要實現的動作,只是先
經過多番測試實踐,實現了popupwindow 彈出在指定控件的下方。代碼上有注釋,有需要注意的地方。popupwindow 有自已的布局,裡面控件的監聽實現都有。接下來
先上圖,看看接下來我要向大家介紹的是個什麼東西,如下圖: 接下來要介紹的就是如何實現上述圖中的波紋效果,這種效果如果大家沒有體驗過的話,可以看看百度手機衛士或者360手機