RecyclerView多条目

MianActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView mImageView;
    private TextView mTextView;
    private Button mButton;
    private RecyclerView mRecyclerView;
    String urlString="http://www.xieast.com/api/news/news.php";

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

        findViews();
        initData();
    }

    private void initData() {
        OkHttps.getInstance().doGet(urlString, new OkHttps.OnCallBack() {
            @Override
            public void onFail() {

            }

            @Override
            public void onResponse(String json) {

                ResponseBean responseBean = new Gson().fromJson(json, ResponseBean.class);
                final List<ResponseBean.DataBean> data = responseBean.getData();

                final MyAdapter adapter = new MyAdapter(getApplicationContext(), data);
                mRecyclerView.setAdapter(adapter);

                mRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
                mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));

                adapter.setOnLongClickListener(new MyAdapter.OnLongClickListener() {
                    @Override
                    public void OnLongClick(final int position1) { //长按

                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("你确定要删除吗?");
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                adapter.remove(position1);
                            }
                        });
                        builder.show();
                    }

                    @Override
                    public void OnClick(ImageView imageView) {

                        AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
                        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 3f,1f);
                        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 3f,1f);

                        animatorSetsuofang.setDuration(2000);
                        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
                        animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
                        animatorSetsuofang.start();

                    }

                });


            }
        });
    }

    private void findViews() {
        mImageView = findViewById(R.id.main_img);
        mTextView = findViewById(R.id.main_text);
        mButton = findViewById(R.id.main_btn);
        mRecyclerView = findViewById(R.id.mian_rv);

        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_btn:

                break;
        }
    }
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;

    private List<ResponseBean.DataBean> list;
    public MyAdapter(Context context, List<ResponseBean.DataBean> data) {
        mContext = context;
        list=data;
    }

    public void remove(int position1) {
        list.remove(position1);
        notifyItemRemoved(position1);
        notifyDataSetChanged();
    }

    public static final int ITEM_ONE=0;
    public static final int ITEM_TWO=1;
    public static final int ITEM_THREE=2;


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==ITEM_ONE){
            View view = View.inflate(parent.getContext(), R.layout.item_onepicture, null);
            ViewHodler1 viewHodler1 = new ViewHodler1(view);
            return viewHodler1;
        }else if (viewType==ITEM_TWO){
            View view = View.inflate(parent.getContext(), R.layout.item_twopicture, null);
            ViewHodler2 viewHodler2 = new ViewHodler2(view);
            return viewHodler2;
        }else if (viewType==ITEM_THREE){
            View view = View.inflate(parent.getContext(), R.layout.item_threepicture, null);
            ViewHodler3 viewHodler3 = new ViewHodler3(view);
            return viewHodler3;
        }
        return null;
    }



    public interface OnLongClickListener{
        void OnLongClick(int position1);
        void OnClick(ImageView imageView);
    }

    OnLongClickListener mOnLongClickListener;

    public void setOnLongClickListener(OnLongClickListener onLongClickListener){
        mOnLongClickListener=onLongClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

        int itemViewType = holder.getItemViewType();
        if (itemViewType==ITEM_ONE){
            final ViewHodler1 viewHodler1 = (ViewHodler1) holder;
            viewHodler1.mTextView.setText(list.get(position).getTitle());
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s()).into(viewHodler1.mImageView);

            ((ViewHodler1) holder).mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnLongClickListener.OnClick(viewHodler1.mImageView);
                }
            });
        }else if (itemViewType==ITEM_TWO){
            ViewHodler2 viewHodler2 = (ViewHodler2) holder;
            viewHodler2.mTextView.setText(list.get(position).getTitle());
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s()).into(viewHodler2.mImageView);
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s02()).into(viewHodler2.mImageView02);
        }else if (itemViewType==ITEM_THREE){
            ViewHodler3 viewHodler3 = (ViewHodler3) holder;
            viewHodler3.mTextView.setText(list.get(position).getTitle());
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s()).into(viewHodler3.mImageView);
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s02()).into(viewHodler3.mImageView02);
            Glide.with(mContext).load(list.get(position).getThumbnail_pic_s03()).into(viewHodler3.mImageView03);
        }

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                int position1 = holder.getLayoutPosition();
                mOnLongClickListener.OnLongClick(position1);
                return false;
            }
        });

    }


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


    @Override
    public int getItemViewType(int position) {

        String pic_s02 = list.get(position).getThumbnail_pic_s02();
        String pic_s03 = list.get(position).getThumbnail_pic_s03();

        if (pic_s02==null&&pic_s03==null){
            return ITEM_ONE;
        }else if (pic_s02!=null&&pic_s03==null){
            return ITEM_TWO;
        }else {
            return ITEM_THREE;
        }
    }

    public class ViewHodler1 extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;
        public ViewHodler1(View itemView) {
            super(itemView);

            mTextView = itemView.findViewById(R.id.item_text);
            mImageView = itemView.findViewById(R.id.item_image);
        }
    }

    public class ViewHodler2 extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;
        ImageView mImageView02;
        public ViewHodler2(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.item_text);
            mImageView = itemView.findViewById(R.id.item_image01);
            mImageView02 = itemView.findViewById(R.id.item_image02);
        }
    }

    public class ViewHodler3 extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;
        ImageView mImageView02;
        ImageView mImageView03;
        public ViewHodler3(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.item_text);
            mImageView = itemView.findViewById(R.id.item_image01);
            mImageView02 = itemView.findViewById(R.id.item_image02);
            mImageView03 = itemView.findViewById(R.id.item_image03);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值