關(guān)于RecyclerView的介紹跟優(yōu)點(diǎn)網(wǎng)上已經(jīng)有很多了啊片,這里我就不在啰嗦只锻。
</p>
效果圖
1.配置gradle:
compile 'com.jakewharton:butterknife:5.1.1'
compile 'com.android.support:recyclerview-v7:23.1.0'
2.Activity中的xml:
activity_recycler_view_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
3.Activity代碼:
public class RecyclerViewDemo extends AppCompatActivity implements NormalRecyclerViewAdapter.IonSlidingViewClickListener, SwipeRefreshLayout.OnRefreshListener {
@InjectView(R.id.swipeRefreshLayout)
SwipeRefreshLayout mSwipeRefreshLayout;
private String TAG = "RecyclerViewDemo";
@InjectView(R.id.recycler_view)
RecyclerView mRecyclerView;
NormalRecyclerViewAdapter adapter;
private static final int REFRESH_STATUS = 0;
private int j = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view_demo);
ButterKnife.inject(this);
initView();
}
private void initView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//這里用線性顯示 類似于listview
//下面的兩種方式自己可以試試看下效果就知道了
//mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//這里用線性宮格顯示 類似于grid view
//mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,OrientationHelper.VERTICAL));//這里用線性宮格顯示 類似于瀑布流
adapter = new NormalRecyclerViewAdapter(this);
//設(shè)置Item增加、移除動(dòng)畫(huà)
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
}
});
onRefresh();
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(RecyclerViewDemo.this, "單擊" + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onDeleteBtnCilck(View view, int position) {
Toast.makeText(RecyclerViewDemo.this, "刪除" + position, Toast.LENGTH_SHORT).show();
adapter.removeData(position);
}
@Override
//下拉刷新的監(jiān)聽(tīng)
public void onRefresh() {
refreshHandler.sendEmptyMessageDelayed(REFRESH_STATUS, 2000);
}
private Handler refreshHandler = new Handler()
{
public void handleMessage(Message msg) {
switch (msg.what) {
case REFRESH_STATUS:
//下拉刷新執(zhí)行的操作紫谷,刷新數(shù)據(jù)
mSwipeRefreshLayout.setRefreshing(false);
List<String> strings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
j++;
strings.add("測(cè)試" + j);
}
adapter.updateData(strings);
break;
}
}
};
}
4.適配器代碼(NormalRecyclerViewAdapter)
public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.MyViewHolder> implements SlidingButtonView.IonSlidingButtonListener {
private Context mContext;
private IonSlidingViewClickListener mIDeleteBtnClickListener;
private List<String> mDatas = new ArrayList<String>();
private SlidingButtonView mMenu = null;
public NormalRecyclerViewAdapter(Context context) {
mContext = context;
mIDeleteBtnClickListener = (IonSlidingViewClickListener) context;
for (int i = 0; i < 20; i++) {
mDatas.add("第"+i+"個(gè)測(cè)試");
}
}
public void updateData( List<String> mDatas){
this.mDatas = mDatas;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mDatas.size();
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.textView.setText(mDatas.get(position)); //設(shè)置內(nèi)容布局的寬為屏幕寬度
holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(mContext);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判斷是否有刪除菜單打開(kāi)
if (menuIsOpen()) {
closeMenu();//關(guān)閉菜單
} else {
int n = holder.getLayoutPosition();
mIDeleteBtnClickListener.onItemClick(v, n);
}
}
});
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n = holder.getLayoutPosition();
mIDeleteBtnClickListener.onDeleteBtnCilck(v, n);
}
});
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item, arg0,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView btn_Delete;
public TextView textView;
public ViewGroup layout_content;
public MyViewHolder(View itemView) {
super(itemView);
btn_Delete = (TextView) itemView.findViewById(R.id.tv_delete);
textView = (TextView) itemView.findViewById(R.id.text);
layout_content = (ViewGroup) itemView.findViewById(R.id.layout_content);
((SlidingButtonView) itemView).setSlidingButtonListener(NormalRecyclerViewAdapter.this); } }
public void removeData(int position){
mDatas.remove(position);
notifyItemRemoved(position);
}
/** * 刪除菜單打開(kāi)信息接收 */
@Override
public void onMenuIsOpen(View view) {
mMenu = (SlidingButtonView) view;
}
/**
* 滑動(dòng)或者點(diǎn)擊了Item監(jiān)聽(tīng)
* @param slidingButtonView
*/
@Override
public void onDownOrMove(SlidingButtonView slidingButtonView) {
if(menuIsOpen()){
if(mMenu != slidingButtonView){
closeMenu();
}
}
}
/** * 關(guān)閉菜單 */
public void closeMenu() {
mMenu.closeMenu();
mMenu = null;
}
/** * 判斷是否有菜單打開(kāi) */
public Boolean menuIsOpen() {
if(mMenu != null){
return true;
}
return false;
}
public interface IonSlidingViewClickListener {
void onItemClick(View view,int position);
void onDeleteBtnCilck(View view,int position);
}
}
5.適配器的布局
layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.littledemomarshal.littledemomarshal.SlidingButtonView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="@android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/btn_click_black_havebackground"
android:gravity="center_vertical"
android:textColor="#DD000000"
android:textSize="16dp" />
</RelativeLayout>
<TextView
android:id="@+id/tv_delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/layout_content"
android:background="@drawable/btn_click_red_havebackground"
android:gravity="center"
android:text="刪 除"
android:textColor="#DDFFFFFF"/>
</RelativeLayout>
</com.example.littledemomarshal.littledemomarshal.SlidingButtonView>
6.適配器布局里面我們使用了一個(gè)自定義的HorizontalScrollView用來(lái)實(shí)現(xiàn)左滑刪除(下面的代碼是網(wǎng)上找的一個(gè)大神寫(xiě)的齐饮,自己稍微修改了一下)
SlidingButtonView.java
public class SlidingButtonView extends HorizontalScrollView {
private TextView mTextView_Delete;
private int mScrollWidth;
private IonSlidingButtonListener mIonSlidingButtonListener;
private Boolean isOpen = false;
private Boolean once = false;
public SlidingButtonView(Context context) {
this(context, null);
}
public SlidingButtonView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOverScrollMode(OVER_SCROLL_NEVER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(!once){
mTextView_Delete = (TextView) findViewById(R.id.tv_delete);
once = true;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(changed){
this.scrollTo(0,0);
//獲取水平滾動(dòng)條可以滑動(dòng)的范圍捐寥,即右側(cè)按鈕的寬度
mScrollWidth = mTextView_Delete.getWidth();
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
mIonSlidingButtonListener.onDownOrMove(this);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
changeScrollx();
return true;
default:
break;
}
return super.onTouchEvent(ev); }
/**
* 按滾動(dòng)條被拖動(dòng)距離判斷關(guān)閉或打開(kāi)菜單
*/
public void changeScrollx(){
if(getScrollX() >= (mScrollWidth/2)){
this.smoothScrollTo(mScrollWidth, 0);
isOpen = true;
mIonSlidingButtonListener.onMenuIsOpen(this);
}else{
this.smoothScrollTo(0, 0);
isOpen = false;
}
}
/**
* 打開(kāi)菜單
*/
public void openMenu() {
if (isOpen){
return;
}
this.smoothScrollTo(mScrollWidth, 0);
isOpen = true;
mIonSlidingButtonListener.onMenuIsOpen(this);
}
/**
* 關(guān)閉菜單
*/
public void closeMenu() {
if (!isOpen){
return;
}
this.smoothScrollTo(0, 0);
isOpen = false;
}
public void setSlidingButtonListener(IonSlidingButtonListener listener){
mIonSlidingButtonListener = listener;
}
public interface IonSlidingButtonListener{
void onMenuIsOpen(View view);
void onDownOrMove(SlidingButtonView slidingButtonView);
}
個(gè)人覺(jué)得,有時(shí)候沈矿,直接上代碼比語(yǔ)言描述有時(shí)候更有效上真。
雖然是個(gè)簡(jiǎn)單的Demo,但希望對(duì)你的開(kāi)發(fā)能有所幫助。