热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GridView、ListView的简单使用

封装的BaseAdapter这个函数包含了最基本的列表功能,传入的数据类型为泛型,使用更方便。publicabstractclassBaseAdapter<T>e

封装的BaseAdapter
这个函数包含了最基本的列表功能,传入的数据类型为泛型,使用更方便。

public abstract class BaseAdapter extends SimpleAdapter {
public Context mContext;
private int mFirstPageIndex = 0;
protected List mListData = new ArrayList();

protected abstract View getItemView(int position, View convertView,
ViewGroup parent);

public BaseAdapter(Context context) {
super(context, null, 0, null, null);
this.mCOntext= context;
this.mFirstPageIndex = 0;
}

public BaseAdapter(Context context, int firstPageIndex) {
super(context, null, 0, null, null);
this.mCOntext= context;
this.mFirstPageIndex = firstPageIndex;
}

public void initData(int pageIndex, List data) {
if (pageIndex == mFirstPageIndex) {
mListData.clear();
}
if (null != data) {
mListData.addAll(data);
}
this.notifyDataSetChanged();
}


@Override
public int getCount() {
return mListData.size();
}

@Override
public T getItem(int position) {
if (position > -1 && position return mListData.get(position);
}
return null;
}

@Override
public boolean isEnabled(int position) {
return true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getItemView(position, convertView, parent);
}

}

GridView的使用
1.activity的xml布局:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:verticalSpacing="1dp"/>


LinearLayout>

2.item的布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/tv_name"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />

<TextView
android:id="@+id/tv_sum"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />

LinearLayout>

3.新建类GridAdapter,类似于列表一样,写好getItemView函数:

public class GridAdapter extends BaseAdapter<GrideFunctionMode.ListBean> {

public GridAdapter(Context context) {
super(context);
}

@Override
protected View getItemView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;
if (cOnvertView== null) {
cOnvertView= LayoutInflater.from(mContext).inflate(R.layout.item_grid, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

GrideFunctionMode.ListBean item = getItem(position);
viewHolder.tvName.setText(item.getName());
viewHolder.tvSum.setText(item.getSum() + "项");

return convertView;
}

static class ViewHolder {
@BindView(R.id.tv_name)
TextView tvName;
@BindView(R.id.tv_sum)
TextView tvSum;

ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

4.在activity中:

public class GridActivity extends AppCompatActivity {

@BindView(R.id.gridView)
GridView gridView;

GridAdapter gridAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
ButterKnife.bind(this);

//为gridView setAdapter
gridAdapter = new GridAdapter(this);
gridView.setAdapter( gridAdapter);

getFunctionInfo();
}

private void getFunctionInfo(){
//根据接口获取信息
RequestParams requestParams = new RequestParams(Constant.GRIDFUNCTIONURL);
requestParams.addBodyParameter("token", COOKIEUtil.getUserInfo().getObj().getToken());
x.http().post(requestParams, new Callback.CommonCallback() {
@Override
public void onSuccess(GrideFunctionMode grideFunctionMode) {
if( grideFunctionMode != null ){
final List list = grideFunctionMode.getList();
if( list != null && list.size() != 0){
//绑定数据:第几页,数据对象
gridAdapter.initData(0, list);
}
}
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {
ex.printStackTrace();
}

@Override
public void onCancelled(CancelledException cex) {

}

@Override
public void onFinished() {

}
});
}
}

ListView的使用上面有很多相似之处
使用SwipeRefreshLayout进行列表数据刷新
1.布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

android.support.v4.widget.SwipeRefreshLayout>

<RelativeLayout
android:background="@color/colorWhite"
android:visibility="gone"
android:id="@+id/layout_empty"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂无消息"
android:textSize="26sp"
android:textColor="@color/colorBlack"/>

RelativeLayout>


LinearLayout>

2.列表项的布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<TextView
android:id="@+id/text_id"
android:text="用户id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


LinearLayout>

3.Adapter的编写:

//封装的BaseAdapter,里面包含listview通用的方法
public class InfoListAdapter extends BaseAdapter<String> {

public InfoListAdapter(Context context) {
super(context);
}

@Override
protected View getItemView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//convertView若为空则新生成一个,并把viewHolder给存起来
if (cOnvertView== null) {
cOnvertView= LayoutInflater.from(mContext).inflate(R.layout.item_infolist, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
//convertView不为空,则直接取出viewHolder继续使用
viewHolder = (ViewHolder) convertView.getTag();
}
//取出数据为控件赋值
String string = getItem(position);
viewHolder.textId.setText(string);

return convertView;
}

static class ViewHolder {
@BindView(R.id.text_id)
TextView textId;

ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

4.activity里的编写:

package com.lotus.COOKIEdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.lotus.COOKIEdemo.R;
import com.lotus.COOKIEdemo.helper.InfoListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Created by admin on 2017/7/25.
*/


public class InfoListActivity extends AppCompatActivity {

@BindView(R.id.listview)
ListView listview;

InfoListAdapter infoListAdapter;

ArrayList stringArrayList = new ArrayList<>();
@BindView(R.id.refresh)
SwipeRefreshLayout refresh;
@BindView(R.id.layout_empty)
RelativeLayout layoutEmpty;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infolist);
ButterKnife.bind(this);

refresh.setProgressBackgroundColor(R.color.colorGray);
refresh.setColorSchemeResources(R.color.colorAccent);
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
onRefreshStart();
}
});

infoListAdapter = new InfoListAdapter(this);
listview.setDividerHeight(10);
listview.setAdapter(infoListAdapter);
onRefreshStart();
}

private void onRefreshStart() {

if (stringArrayList != null && stringArrayList.size() > 0) {
layoutEmpty.setVisibility(View.GONE);
//为myListAdapter绑定数据源,参数为:从第一页开始,数据对象
infoListAdapter.initData(0, stringArrayList);
} else {
System.out.println("显示空白页面");
//显示空白页面
layoutEmpty.setVisibility(View.VISIBLE);
}
refresh.setRefreshing(false);

int index = stringArrayList.size();
for (int i = index; i 5; i++) {
stringArrayList.add("用户Id: " + String.valueOf(i));
}
}
}

推荐阅读
author-avatar
手机用户2502922313
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有