首先定義一個自定義組件:android
import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; /** * Created by zhangda on 2018/1/28. */ public class MyRecyclerViewItem extends HorizontalScrollView { public static boolean isLeft=true;//在左邊 public MyRecyclerViewItem(Context context) { super(context); init(); } public MyRecyclerViewItem(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyRecyclerViewItem(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private int buttonWidth; private DisplayMetrics scale; private void init() { scale = getContext().getResources().getDisplayMetrics(); buttonWidth = dp2px(100);// 佈局的寬度 } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); Log.i(getClass().getSimpleName(), l+"-"+t+"-"+r+"-"+b); //調整佈局 LinearLayout linearLayout= (LinearLayout) getChildAt(0); LinearLayout linearLayoutLeft = (LinearLayout) linearLayout.getChildAt(0); LinearLayout linearLayoutRight = (LinearLayout) linearLayout.getChildAt(1); linearLayout.layout(linearLayout.getLeft(), linearLayout.getTop(), linearLayout.getLeft()+getMeasuredWidth()+ buttonWidth, linearLayout.getBottom()); linearLayoutLeft.layout(linearLayoutLeft.getLeft(), linearLayoutLeft.getTop(),linearLayoutLeft.getLeft()+getMeasuredWidth(), linearLayoutLeft.getBottom()); linearLayoutRight.layout(linearLayoutLeft.getLeft()+getMeasuredWidth(), linearLayoutLeft.getTop(), linearLayoutLeft.getLeft()+getMeasuredWidth()+ buttonWidth, linearLayoutLeft.getBottom()); } //恢復狀態 public void reset(){ isLeft=true; scrollTo(0,0); } @Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction()== MotionEvent.ACTION_DOWN) { Log.i(getClass().getSimpleName(), "down"); return true; } if (ev.getAction()==MotionEvent.ACTION_CANCEL || ev.getAction()== MotionEvent.ACTION_UP) { Log.i(getClass().getSimpleName(), "up"); int range=70; if (isLeft) { if (getScrollX()>range) { isLeft=false; smoothScrollTo(buttonWidth, 0); }else { smoothScrollTo(0,0); } }else { if (getScrollX()< (buttonWidth-range)) { isLeft=true; smoothScrollTo(0,0); }else { smoothScrollTo(buttonWidth, 0); } } return true; } Log.i(getClass().getSimpleName(), "end"); return super.onTouchEvent(ev); } private int dp2px(float dpValue){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, scale); } }
RecyclerView 列表佈局代碼:app
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" />
對應item佈局代碼:ide
<?xml version="1.0" encoding="utf-8"?> <yourpackage.component.MyRecyclerViewItem xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll_item" android:scrollbars="none" android:focusable="true" android:layout_width="match_parent" android:layout_height="50dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="2dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/content_layout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <!--內容區域放置佈局--> <TextView android:id="@+id/item1" android:text="魚香肉絲" android:textSize="18sp" android:gravity="center" android:background="@color/colorWhite" android:textColor="@color/colorFontColor" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="50dp" /> <TextView android:id="@+id/item2" android:text="15:30" android:textSize="18sp" android:gravity="center" android:background="@color/colorWhite" android:textColor="@color/colorFontColor" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout> <LinearLayout android:background="@color/colorDelete" android:gravity="center" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--自定義側邊菜單佈局--> <TextView android:id="@+id/deleteSample" android:text="刪除" android:textColor="#ffffff" android:textSize="22sp" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> </yourpackage.component.MyRecyclerViewItem>
其中item的點擊事件和刪除按鈕的點擊時間在RecyclerView的Adapter的onBindViewHolder方法中來寫,以下:佈局
@Override public void onBindViewHolder(final Hodler holder, final int position) { holder.item1.setWidth(screenWidth/2); holder.item2.setWidth(screenWidth/2); holder.deleteSample.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(KeepSampleListActivity.this, "刪除:"+position, Toast.LENGTH_LONG).show(); } }); holder.content_layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(holder.recyclerViewItem.isLeft){ Intent i = new Intent(); i.setClass(KeepSampleListActivity.this, SampleDetailActivity.class); startActivity(i); }else{ holder.recyclerViewItem.reset(); } } }); //恢復狀態 holder.recyclerViewItem.reset(); }