ViewPager显示多张图片和滑动删除

ViewPager显示多张图片

ViewPager显示多张图片和滑动删除_第1张图片
XML




    
    


Activity

public class MainActivity extends AppCompatActivity {

    private ViewPager vpId;
    private List data=new ArrayList<>();
    private LinearLayout lin;

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

        vpId = (ViewPager) findViewById(R.id.vp_id);
        lin = (LinearLayout) findViewById(R.id.lin);


        int[] images = new int[]{R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,R.drawable.t5,R.drawable.t6};
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(images[i]);
            data.add(imageView);
        }

        vpId.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view==o;
            }

            @NonNull
            @Override//产生一个新视图
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(data.get(position));//添加
                return data.get(position);//返回添加成功的控件
            }

            @Override//移除一张图片
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(data.get(position));//移除
            }
    });
        vpId.setOffscreenPageLimit(3);
        vpId.setPageMargin(10);

        lin.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return vpId.dispatchTouchEvent(event);
            }
        });
    }
}

滑动删除

ViewPager显示多张图片和滑动删除_第2张图片
添加依赖

implementation “com.daimajia.swipelayout:library:1.2.0@aar”
implementation ‘com.android.support:recyclerview-v7:28.0.0’

XML




    
    


MainActivity

public class Main2Activity extends AppCompatActivity {

    private static final String TAG = "Main2Activity";
    private RecyclerView rvId;

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

        rvId = (RecyclerView) findViewById(R.id.rv_id);

        List data=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add("哒哒哒"+i);
        }

        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvId.setLayoutManager(manager);

        RVAdapter adapter = new RVAdapter(data, this);
        rvId.setAdapter(adapter);
    }
}

Swip.xml




    
        

适配器

public class RVAdapter extends RecyclerSwipeAdapter {

    private List strings;
    private Context mContext;

    public RVAdapter(List strings, Context context) {
        this.strings = strings;
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.swip_layout,null));
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, final int position) {
        viewHolder.mSwipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);//设置滑动模式
        viewHolder.textView.setText(strings.get(position));
        viewHolder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除数据
                strings.remove(position);
                //删除动画
                notifyItemRemoved(position);
                //刷新适配器
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return position;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private SwipeLayout mSwipeLayout;
        private Button mButton;
        private TextView textView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mSwipeLayout=itemView.findViewById(R.id.swip_id);
            mButton=itemView.findViewById(R.id.delete);
            textView=itemView.findViewById(R.id.text_content);
        }
    }
}

你可能感兴趣的:(ViewPager显示多张图片和滑动删除)