Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现

Demo展示图片

这里写图片描述

布局代码

  注:为了显示水波纹效果,minSdkVersion设置为21。代码没有适配5.0以下,如果要适配5.0以下,可参考:Android开发中的水波纹效果实现 http://blog.csdn.net/zxc514257857/article/details/73200900

// (layout)activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.text.recyclerviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"
        android:scrollbars="none"/>

</RelativeLayout>
----------------------------------------------------------------------------------------
// (layout)recyclerview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/recyclerview_bg">

    <TextView
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_margin="10dp"
        android:textColor="#fff"/>

    <TextView
        android:id="@+id/data"
        android:layout_toLeftOf="@+id/ll"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorPrimaryDark"/>

    <LinearLayout
        android:id="@+id/ll"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:background="@drawable/ripple_bg"
            android:clickable="true"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/del"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            android:textSize="20sp"
            android:layout_margin="10dp"
            android:background="@drawable/ripple_bg"
            android:clickable="true"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/up"
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:text="↑"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_margin="10dp"
            android:background="@drawable/ripple_bg"
            android:clickable="true"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/down"
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:text="↓"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_margin="10dp"
            android:background="@drawable/ripple_bg"
            android:clickable="true"
            android:textColor="#fff"/>

    </LinearLayout>
</RelativeLayout>
----------------------------------------------------------------------------------------
// (layout)recyclerview_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorAccent"/>
            <stroke android:width="1dp" android:color="@color/colorPrimaryDark"/>
        </shape>
    </item>
</selector>
----------------------------------------------------------------------------------------
// (layout)ripple_bg.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorPrimaryDark">

</ripple>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114

逻辑代码

  注:需在module中的dependencies节点下添加:

compile 'com.yanzhenjie:recyclerview-swipe:1.0.4'
  • 1
// MyAdapter 
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;
import java.util.List;

public class MyAdapter extends SwipeMenuAdapter<MyAdapter.MyViewHolder> {
    private List<String> strList;
    private OnItemClickListener mOnItemClickListener;

    public MyAdapter(List<String> strList){
        this.strList = strList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
    }

    @Override
    public MyAdapter.MyViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(realContentView);
        myViewHolder.mOnItemClickListener = mOnItemClickListener;
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
        holder.num.setText((position + 1) + "");
        holder.data.setText(strList.get(position));
    }

    @Override
    public int getItemCount() {
        return strList == null ? 0 : strList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        OnItemClickListener mOnItemClickListener;
        TextView num;
        TextView data;
        TextView add;
        TextView del;
        TextView up;
        TextView down;

        public MyViewHolder(View itemView) {
            super(itemView);
            num = (TextView) itemView.findViewById(R.id.num);
            data = (TextView) itemView.findViewById(R.id.data);
            add = (TextView) itemView.findViewById(R.id.add);
            del = (TextView) itemView.findViewById(R.id.del);
            up = (TextView) itemView.findViewById(R.id.up);
            down = (TextView) itemView.findViewById(R.id.down);
            add.setOnClickListener(this);
            del.setOnClickListener(this);
            up.setOnClickListener(this);
            down.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition() , v);
            }
        }
    }
}
----------------------------------------------------------------------------------------
// OnItemClickListener
import android.view.View;

public interface OnItemClickListener {
    void onItemClick(int position , View v);
}
----------------------------------------------------------------------------------------
// MainActivity
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private RecyclerView mRecyclerView;
    private Context mContext = MainActivity.this;
    private List<String> mStrList;
    private MyAdapter mMyAdapter;
    private int clickTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    public void initData(){
        mStrList = new ArrayList<>();
        for (int i = 0 ; i < 20 ; i++){
            mStrList.add(i + "");
        }
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mMyAdapter = new MyAdapter(mStrList);
        // 设置item及item中控件的点击事件
        mMyAdapter.setOnItemClickListener(onItemClickListener);
        mRecyclerView.setAdapter(mMyAdapter);
    }

    /**
     * Item点击监听
     */
    private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(int position , View v) {
            switch (v.getId()){
                case R.id.add:
                    clickTime++;
                    Toast.makeText(mContext, "增:" + position, Toast.LENGTH_SHORT).show();
                    mStrList.add(position+ 1 , "增" + clickTime);
                    mMyAdapter.notifyDataSetChanged();
                    Log.i(TAG , "mStrList:" + mStrList.toString());
                    break;

                case R.id.del:
                    Toast.makeText(mContext, "删:" + position, Toast.LENGTH_SHORT).show();
                    mStrList.remove(position);
                    mMyAdapter.notifyDataSetChanged();
                    break;

                case R.id.up:
                    if(position == 0){
                        Toast.makeText(mContext, "已经在顶部,无法移动!:" + position, Toast.LENGTH_SHORT).show();
                    }else if(position > 0 && position <= mStrList.size()-1){
                        Toast.makeText(mContext, "上:" + position, Toast.LENGTH_SHORT).show();
                        swap(mStrList , position , position-1);
                        mMyAdapter.notifyDataSetChanged();
                    }
                    break;

                case R.id.down:
                    if(position == mStrList.size()-1){
                        Toast.makeText(mContext, "已经在底部,无法移动!:" + position, Toast.LENGTH_SHORT).show();
                    }else if(position >= 0 && position < mStrList.size()-1){
                        Toast.makeText(mContext, "下:" + position, Toast.LENGTH_SHORT).show();
                        swap(mStrList , position , position+1);
                        mMyAdapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    /**
     * 集合中两个元素的交换操作
     * @param list
     * @param oldPosition
     * @param newPosition
     * @param <T>
     */
    public static <T> void swap(List<T> list, int oldPosition, int newPosition){
        if(null == list){
            throw new IllegalStateException("The list can not be empty...");
        }
        T tempElement = list.get(oldPosition);

        // 向前移动,前面的元素需要向后移动
        if(oldPosition < newPosition){
            for(int i = oldPosition; i < newPosition; i++){
                list.set(i, list.get(i + 1));
            }
            list.set(newPosition, tempElement);
        }
        // 向后移动,后面的元素需要向前移动
        if(oldPosition > newPosition){
            for(int i = oldPosition; i > newPosition; i--){
                list.set(i, list.get(i - 1));
            }
            list.set(newPosition, tempElement);
        }
    }
}


Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9878510 
发布了5 篇原创文章 · 获赞 7 · 访问量 7071

猜你喜欢

转载自blog.csdn.net/jokeYJW/article/details/78491787