美文网首页Android
安卓 recycleview中嵌套gridview

安卓 recycleview中嵌套gridview

作者: deast | 来源:发表于2020-07-02 13:28 被阅读0次

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.mylibrary.utils.GlideUtils;
import com.mylibrary.utils.ToastUtil;
import com.stategrid.echarge.R;
import com.stategrid.echarge.module.charge.activity.ColumnsDetalisActivity;
import com.stategrid.echarge.module.charge.bean.ColumnsBean;
import com.stategrid.echarge.utils.CustomeGridView;

import java.io.Serializable;
import java.util.List;

/**

  • @author geting
    */

public class ColumnsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<ColumnsBean> list;
private Context context;
private LayoutInflater inflater;

public ColumnsAdapter(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public void setData(List<ColumnsBean> list) {
    if (null != list) {
        this.list = list;
        notifyDataSetChanged();
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.frg_columns_item, parent, false);
    return new MyViewHolder(view);

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    final MyViewHolder myViewHolder = (MyViewHolder) holder;

    final ColumnsBean columnsBean = list.get(position);

    GlideUtils.loadImageView(context, columnsBean.getImg(), myViewHolder.mHead);
    myViewHolder.mUserName.setText(columnsBean.getName());
    myViewHolder.mTvSignature.setText(columnsBean.getQianming());

    //显示评价的内容
    myViewHolder.mTvColumnsContent.setText(columnsBean.getContent());
    //设置评价星级
    myViewHolder.mBar.setRating(columnsBean.getSum());
    //设置汽车品牌
    GlideUtils.loadImageView(context, columnsBean.getPinpai(), myViewHolder.mCarLogo);
    //设置汽车车牌号
    myViewHolder.mCarName.setText(columnsBean.getChepai());
    //设置时间
    myViewHolder.mTvTime.setText(columnsBean.getTime());

    //设置gridview适配器
    myViewHolder.mGv.setAdapter(myViewHolder.columnsImageAdapter);
    myViewHolder.columnsImageAdapter.setData(columnsBean.getList());

    //点击内容弹框显示是否删除
    myViewHolder.mTvColumnsContent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //创建提示框
            AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.Dialog_FS);
            View view = View.inflate(context, R.layout.dialog_columns_isdelete, null);
            final AlertDialog alertDialog = dialog.create();

            //设置布局并展示
            alertDialog.setView(view);
            alertDialog.show();

            //设置在底部弹出
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);

            //设置指定的宽高,如果不设置的话,弹出的对话框可能不会显示全整个布局,当然在布局中写死宽高也可以
            WindowManager windowManager = ((Activity) context).getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            WindowManager.LayoutParams params = alertDialog.getWindow().getAttributes();
            params.width = (int) (display.getWidth());
            alertDialog.getWindow().setAttributes(params);

            //获取id
            LinearLayout delete = view.findViewById(R.id.delete_columns);
            LinearLayout cancle = view.findViewById(R.id.cancel_columns);

            //设置点击事件
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.showToast(context, "删除");
                    //判断是否是自己的评论

                    //删除该条目
                    list.remove(position);
                    notifyDataSetChanged();
                    alertDialog.dismiss();
                }
            });

            //点击取消
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                }
            });
        }
    });

    //设置点击事件
    myViewHolder.mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //跳转到详情界面
            Intent intent = new Intent(context, ColumnsDetalisActivity.class);
            intent.putExtra("images", (Serializable) columnsBean.getList());
            intent.putExtra("pos", position);
            context.startActivity(intent);
        }
    });

}

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

class MyViewHolder extends RecyclerView.ViewHolder {
    private ImageView mHead, mCarLogo;
    private TextView mUserName, mTvSignature, mTvColumnsContent, mCarName, mTvTime;
    private CustomeGridView mGv;
    private LinearLayout main;
    private RatingBar mBar;
    private ColumnsImageAdapter columnsImageAdapter;

    public MyViewHolder(View itemView) {
        super(itemView);
        mHead = itemView.findViewById(R.id.iv_head_image);
        mUserName = itemView.findViewById(R.id.tv_user_name);
        mTvSignature = itemView.findViewById(R.id.tv_signature);
        mTvColumnsContent = itemView.findViewById(R.id.tv_columns_content);
        mCarName = itemView.findViewById(R.id.car_name);
        mTvTime = itemView.findViewById(R.id.tv_time);
        mCarLogo = itemView.findViewById(R.id.car_logo);
        mGv = itemView.findViewById(R.id.gv_columns);
        mBar = itemView.findViewById(R.id.total_ratingbar);
        //gridview的适配器
        columnsImageAdapter = new ColumnsImageAdapter(context);
        main = itemView.findViewById(R.id.main);
    }
}

}

相关文章

网友评论

    本文标题:安卓 recycleview中嵌套gridview

    本文链接:https://www.haomeiwen.com/subject/ftxrqktx.html