当前位置:主页 > 移动开发 > Android代码 >

android中GridView实现点击查看更多功能示例

时间:2021-07-16 10:43:16 | 栏目:Android代码 | 点击:

首先来看一下效果图;

 

先说一下我的需求:查看群成员,如果超过15人则全部显示,如果大于15人则先加载15人,其余的不显示,点击查看更多则加载全部。再来说一下我实现的逻辑:首先呢要判断群成员的人数,如果小于或者等于15就用GridView加载全部数据,隐藏查看更多的按钮。如果大于15人,则显示加载更多的按钮,先加载15条数据,其余的不加载,点击按钮之后获取全部数据放到自己写好的adapter里然后加载刷新,再隐藏加载更多的按钮。

好了,来看代码:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  android:background="@color/white"
  android:orientation="vertical">
 
  <GridView
    android:id="@+id/grid_member"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="15dp"
    android:layout_weight="1"
    android:horizontalSpacing="15dp"
    android:numColumns="5"
    android:verticalSpacing="15dp"></GridView>
 
  <LinearLayout
    android:id="@+id/llayout_look_more"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_white_selector"
    android:gravity="center">
 
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:drawableRight="@mipmap/arrow_down_qun"
      android:gravity="center"
      android:paddingBottom="30dp"
      android:paddingTop="15dp"
      android:text="查看更多群成员 "
      android:textSize="@dimen/small_middle_text_size" />
  </LinearLayout>
</LinearLayout>

然后是activity中部分判断的核心代码:

首先第一次判断和加载

if (mListData.size() > 15) {
  mGroupManageAdapter.setNumber(false);
  mLlayoutLookMore.setVisibility(View.VISIBLE);
} else {
  mGroupManageAdapter.setNumber(true);
  mLlayoutLookMore.setVisibility(View.GONE);
}
mGroupManageAdapter.replace(mListData);

点击按钮之后的代码:

mGroupManageAdapter.setNumber(true);
mLlayoutLookMore.setVisibility(View.GONE);
mGroupManageAdapter.replace(mListData);

再贴上我的adapter部分代码:

public class GroupManageAdapter extends BaseAdapter {
  private Context mContext;
  private List<PublicEntity> mList = new ArrayList<>();
  private boolean mListType = true;//是否显示全部成员,默认显示
 
  public void setNumber(boolean mListType) {
    this.mListType = mListType;
  }
 
  public GroupManageAdapter(Context mContext) {
    this.mContext = mContext;
  }
public void replace(List<PublicEntity> itemDataTypes) {
  mList.clear();
  if (itemDataTypes.size() > 0) {
    mList.addAll(itemDataTypes);
    notifyDataSetChanged();
  }
}
 
 @Override
 public int getCount() {
 return mListType ? mList.size() : 15;
 }
 
 @Override
 public Object getItem(int position) {
 return mList.get(position);
 }
 
 @Override
 public long getItemId(int position) {
 return position;
 }
 
 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
 final ViewHolder holder;
 if (convertView == null) {
 convertView = View.inflate(mContext, R.layout.item_group_manage_head, null);
 holder = new ViewHolder(convertView);
 convertView.setTag(holder);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }
 String mPic = mList.get(position).getTextOne();
 if (mPic != null && mPic.length() > 0) {
 Picasso.with(mContext).load(mPic)
 .placeholder(R.mipmap.s_hearder_user_nor)
 .error(R.mipmap.s_hearder_user_nor)
 .transform(new CircleTransform()).into(holder.mImgHead);
 } else {
 holder.mImgHead.setImageResource(R.mipmap.s_hearder_user_nor);
 }
 
//这里是删除成员的标记,不管。
 if (mList.get(position).ismIsShowDeleteIcon()) {
 holder.mImgIconDelete.setVisibility(View.VISIBLE);
 } else {
 holder.mImgIconDelete.setVisibility(View.GONE);
 }
 
 holder.mTvName.setText(mList.get(position).getText());
 return convertView;
 }
 
 static class ViewHolder {
 @BindView(R.id.img_head)
 ImageView mImgHead;
 @BindView(R.id.img_icon_delete)
 ImageView mImgIconDelete;
 @BindView(R.id.tv_name)
 TextView mTvName;
 
 ViewHolder(View view) {
 ButterKnife.bind(this, view);
 }
 }
}

您可能感兴趣的文章:

相关文章