Add a fixed button to the GridView or ListView (similar to the WeChat group chat invite friend button)

Add a fixed tail button to the GridView and listen to click events: (similar to the WeChat group chat invite friend button) The
effect is as shown in the figure below: If
Write picture description here
Write picture description here
you want to achieve this effect, it is very simple (I will not say more about the basic usage of GridView)
Paste key Code:

@Override
    public int getCount() {
        return friendsBeans.size() + 1;
    }

Only the return value in the getCount() method of the GridView adapter is +1, so that one more View will be created (that is, size plus 1 View).

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_data, null);
            viewHolder.ivPhoto = (ImageView) convertView.findViewById(R.id.iv1);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position < friendsBeans.size()) {
            FriendsBean friendsBean = (FriendsBean) getItem(position);
            viewHolder.tvName.setText(friendsBean.getName());
            Glide.with(context).load(friendsBean.getPhoto()).into(viewHolder.ivPhoto);
        } else {
            viewHolder.ivPhoto.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.add_btn));
            viewHolder.tvName.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView ivPhoto;
        TextView tvName;
    }

Mainly look at this part:

if (position < friendsBeans.size()) {
            FriendsBean friendsBean = (FriendsBean) getItem(position);
            viewHolder.tvName.setText(friendsBean.getName());
            Glide.with(context).load(friendsBean.getPhoto()).into(viewHolder.ivPhoto);
        } else {
            viewHolder.ivPhoto.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.add_btn));
            viewHolder.tvName.setVisibility(View.GONE);
        }

Here, in the getView() method of the GridView adapter, it is judged which View is currently being assigned. If it is within the range of the collection size, it will be assigned according to the normal method. It is outside the size, which is the View we just added 1. Set a special picture for ImageView (that is, picture number ➕).

The following is the click event listener for the plus sign:
I will not write a listener for the GridView. The main logic is in this code:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getChildCount() - 1 == position) {
   
   //点击了加号
            Toast.makeText(this, "邀请好友", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, position + "", Toast.LENGTH_SHORT).show();
        }
    }

Here it is judged whether the clicked child View is the last one, and then it is OK to write the required logic method in it.

Here is the full code:

Entity class:

public class FriendsBean {
    
    
    private String photo;
    private String name;
    private boolean check;

    public FriendsBean() {
    }

    public FriendsBean(String photo, String name, boolean check) {
        this.photo = photo;
        this.name = name;
        this.check = check;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean getCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }
}

Activity class:

public class GroupDataActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    
    
    private MyGridView gridView;
    private GroupDataAdapter groupDataAdapter;
    private GroupDataPresenter groupDataPresenter;


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

    private void setListeners() {
        gridView.setOnItemClickListener(this);
    }

    private void initData() {
        List<FriendsBean> fbs = new ArrayList<>();
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876543133&di=ed2dbfd1848aaeed780366dc0544224d&imgtype=0&src=http%3A%2F%2Fwww.jintang.cn%2Fdata%2Fattachment%2Fportal%2F201412%2F13%2F100108x6jj2s802h8j3cob.jpg", "小松", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876763302&di=e1cde7fea3dba1c35fde549918b84886&imgtype=0&src=http%3A%2F%2Fhiphotos.baidu.com%2Fdoc%2Fpic%2Fitem%2F7acb0a46f21fbe09f67d698062600c338744ad07.jpg", "小井", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876791845&di=af6700b8381f5e696348f3da37109397&imgtype=0&src=http%3A%2F%2Fpic23.nipic.com%2F20120914%2F10910345_100020812000_2.jpg", "高小松", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876791845&di=217c1bfedac7191511cefb44242b10fa&imgtype=0&src=http%3A%2F%2Fpic24.nipic.com%2F20120921%2F10910345_130417417000_2.jpg", "欧阳峰", false));
        fbs.add(new FriendsBean("https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=cfab9c942c9759ee5e5d6899d3922873/5d6034a85edf8db10c4284fd0323dd54564e743f.jpg", "东方不败", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876894676&di=fdc9301ca95f5d03b579b4b402892e0f&imgtype=0&src=http%3A%2F%2Fwww.qxjlm.com%2Ftupians%2Fbd12876292.jpg", "黄老邪", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876908257&di=61c2bbc1adecc3dd594c47e586fc599b&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2580324790%2C489579783%26fm%3D214%26gp%3D0.jpg", "松", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876894668&di=f017e4596cda6278fe7eb5380720f94f&imgtype=0&src=http%3A%2F%2Fimg.faxingw.cn%2F201509%2F51m_13.jpg", "我去", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876894723&di=0af8c5e2728936e89d9ef72f5746eb55&imgtype=0&src=http%3A%2F%2Fimg003.21cnimg.com%2Fphotos%2Falbum%2F20140114%2Fm320%2FE94AA721DE7DBF46E23E92EF0592849E.jpeg", "来来来来", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876894714&di=ec94f48b042accc87997d1c223d01604&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F151202%2F14-1512021A520Z2.jpg", "阿斯达撒上的", false));
        fbs.add(new FriendsBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502876894710&di=e4a616663913bad5fd8fecf969254ace&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201509%2F14%2F20150914191741_rJXCw.thumb.700_0.png", "多少啊", false));
        groupDataAdapter = new GroupDataAdapter(this, fbs);
        gridView.setAdapter(groupDataAdapter);
    }

    private void initView() {
        gridView = (MyGridView) findViewById(R.id.gridview);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getChildCount() - 1 == position) {
   
   //点击了加号
            Toast.makeText(this, "邀请好友", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, position + "", Toast.LENGTH_SHORT).show();
        }
    }
}

Adapter class:

public class GroupDataAdapter extends BaseAdapter {
    
    
    private Context context;
    private List<FriendsBean> friendsBeans;

    public GroupDataAdapter(Context context, List<FriendsBean> friendsBeans) {
        this.context = context;
        this.friendsBeans = friendsBeans;
    }

    @Override
    public int getCount() {
        return friendsBeans.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return friendsBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_data, null);
            viewHolder.ivPhoto = (ImageView) convertView.findViewById(R.id.iv1);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position < friendsBeans.size()) {
            FriendsBean friendsBean = (FriendsBean) getItem(position);
            viewHolder.tvName.setText(friendsBean.getName());
            Glide.with(context).load(friendsBean.getPhoto()).into(viewHolder.ivPhoto);
        } else {
            viewHolder.ivPhoto.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.add_btn));
            viewHolder.tvName.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView ivPhoto;
        TextView tvName;
    }

The above is all the code, (if there are omissions or errors, please leave a message to discuss).

Guess you like

Origin blog.csdn.net/qq77485042/article/details/77269834