僅供參考學(xué)習(xí) 借鑒了部分網(wǎng)絡(luò)大神宵凌。 如果侵 刪。
Android 7.0已經(jīng)出了止后,感覺(jué)自己還在以前的技術(shù)上徘徊瞎惫,上班就越來(lái)越懶,用到了才會(huì)去查坯门,需要學(xué)的還有很多微饥。
Android 5.0 提供了RecyclerView和cardView 兩個(gè) 新的控件逗扒。
效果圖
以前l(fā)istview的寫法:
![Upload Paste_Image.png failed. Please try again.]
1. RecyclerView
基本寫法:
public class GalleryAdapter extends
RecyclerView.Adapter<GalleryAdapter.ViewHolder>
{
private LayoutInflater mInflater;
private List<Integer> mDatas;
public GalleryAdapter(Context context, List<Integer> datats)
{
mInflater = LayoutInflater.from(context);
mDatas = datats;
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public ViewHolder(View arg0)
{
super(arg0);
}
ImageView mImg;
TextView mTxt;
}
@Override
public int getItemCount()
{
return mDatas.size();
}
/**
* 創(chuàng)建ViewHolder
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View view = mInflater.inflate(R.layout.activity_index_gallery_item,
viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
viewHolder.mTxt= (TextView) view
.findViewById(R.id.id_index_gallery_item_text);
viewHolder.mImg = (ImageView) view
.findViewById(R.id.id_index_gallery_item_image);
return viewHolder;
}
/**
* 設(shè)置值
*/
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i)
{
viewHolder.mImg.setImageResource(mDatas.get(i));
viewHolder.mTxt.setText("XXXX");
}
代碼中:
//得到控件
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
//設(shè)置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
//設(shè)置適配器
mAdapter = new GalleryAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
RecycleView需要自己去實(shí)現(xiàn)點(diǎn)擊事件: 采用adapter去回調(diào)
public interface OnItemClickLitener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickListener(OnItemClickLitener mOnItemClickLitener)
{
this.mOnItemClickLitener = mOnItemClickLitener;
}
在onBindViewHolder中實(shí)現(xiàn)點(diǎn)擊單個(gè)事件古戴。
@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {
holder.mCusName.setText("小王");
if (mOnItemClickLitener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
Activity中adapter中實(shí)現(xiàn)單個(gè)點(diǎn)擊事件:
mAdapter.setOnItemClickLitener(new OnItemClickLitener() {
@Override public void onItemClick(View view, int position) {
Toast.makeText(HomeActivity.this, position + " click", Toast.LENGTH_SHORT).show();
}
@Override public void onItemLongClick(View view, int position) {
Toast.makeText(HomeActivity.this, position + " long click", Toast.LENGTH_SHORT).show(); mAdapter.removeData(position);
}
});
2.CardView
布局類似FrameLayout,但是加了很多特效:卡片的邊框矩肩、陰影现恼,duang,很酷黍檩、很炫叉袍。
基本寫法:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardBackgroundColor="@color/cardview_dark_background"
card_view:cardCornerRadius="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="5dp" >
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />
<TextView
android:clickable="true"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:gravity="right|bottom"
android:textColor="@android:color/white"
android:textSize="24sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
card_view:cardBackgroundColor="@color/cardview_dark_background" card_view:cardCornerRadius="5dp"
注意要使用card_view命名空間
設(shè)置CardView的背景顏色和外圍的圓角大小
3.結(jié)合使用實(shí)現(xiàn):
知乎類的 界面:
參考學(xué)習(xí): 超級(jí)棒