当前位置: 代码迷 >> Android >> listview分页怎么固定item数量显示
  详细解决方案

listview分页怎么固定item数量显示

热度:98   发布时间:2016-04-28 00:40:59.0
listview分页如何固定item数量显示
假设每次固定显示10项
每次新加载时清楚上次显示的10项!

主界面代码:
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.wiky.zhang.listviewpagedata.R;
import cn.wiky.zhang.listviewpagedata.utils.HTTPutils;
import cn.wiky.zhang.listviewpagedata.utils.UrlUtils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;

public class MainActivity extends Activity implements OnScrollListener,
OnClickListener {


private String url = null;
private ListView listview;
private List<Map<String, String>> list;
private MyAdapter a;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {

list = (List<Map<String, String>>) msg.obj;
Log.v("22222", list + "00000000");
a = new MyAdapter();
listview.setAdapter(a);
Log.v("-----------", list + "-------");
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
listview = (ListView) this.findViewById(R.id.listview);
listview.setOnScrollListener(this);
View v = LayoutInflater.from(this).inflate(R.layout.tootbar, null);
// TextView v=new TextView(this);
// v.setText("加载更多");
listview.addFooterView(v);
v.setOnClickListener(this);
url = UrlUtils.geturl(1);
HTTPutils u = new HTTPutils(this, url);
u.getJSONdata(handler);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {// 判断是否滑动到底
Toast.makeText(this, "已滑动到底部", 0).show();
if (list.size()-a.pageCount*a.pagenum<=a.pageCount) {
Toast.makeText(getApplicationContext(), "已至末尾...",Toast.LENGTH_SHORT).show();
}
}
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}

@Override
public void onClick(View v) {
Toast.makeText(this, "jiazai", 0).show();

a.pagenum ++;
a.notifyDataSetChanged();
}

class MyAdapter extends BaseAdapter {
// 每一次显示多少个
 int pageCount = 10;
int pagenum=1;


@Override
public int getCount() {

return pageCount*pagenum;

}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder h = null;
if (convertView == null) {

h = new ViewHolder();
convertView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.item, parent, false);
h.img = (ImageView) convertView.findViewById(R.id.img_listview);
h.tv = (TextView) convertView.findViewById(R.id.tv_listview);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.tv.setText(list.get(position).get("shorttitle"));
BitmapUtils b = new BitmapUtils(MainActivity.this);
if (list.get(position).get("litpic").equals("")) {
b.display(h.img, "assets/jienigui.png");
} else {
b.display(h.img,
UrlUtils.getimgURL(list.get(position).get("litpic")));
}
return convertView;
}

class ViewHolder {
TextView tv;
ImageView img;
}
}
}

网络请求使用的xutils,解析后的数据使用消息发送出去:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class HTTPutils {
private Context con;
private String url;
private List<Map<String, String>> arrayList = new ArrayList<Map<String, String>>();
public HTTPutils(Context con, String url) {
super();
this.con = con;
this.url = url;
}

public void getJSONdata(final Handler handler) {
HttpUtils http = new HttpUtils();
http.send(HttpMethod.GET, url, new RequestCallBack<String>() {

@Override
public void onFailure(HttpException arg0, String arg1) {
 Toast.makeText(con, "网络连接异常", Toast.LENGTH_SHORT).show();
}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
try {
JSONObject j = new JSONObject(arg0.result);
JSONObject js = j.getJSONObject("data");
for (int i = 0; i < js.length(); i++) {
HashMap<String, String> map = new HashMap<>();
JSONObject json = js.getJSONObject(i + "");
String shorttitle = json.getString("shorttitle");
String litpic = json.getString("litpic");
map.put("shorttitle", shorttitle);
map.put("litpic", litpic);
arrayList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
Message m = handler.obtainMessage(1, arrayList);
handler.sendMessage(m);
}
});
}
}

其他类:
public class UrlUtils {
//列表 URL
private static final String URL="http://www.3dmgame.com/sitemap/api.php?row=100&typeid=%d&paging=1&page=n";
public  static String geturl(int i){
return String.format(URL, i);
}
//图片url
private static final String IMGURL="http://www.3dmgame.com";
public static String getimgURL(String imgurl){
return IMGURL+imgurl;
}
}

住界面布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.wiky.zhang.listviewpagedata.activity.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listview分页加载" />
    
    <ListView 
        android:id="@+id/listview"
        android:layout_below="@id/tv"
        android:layout_width="match_parent"
        android:divider="#000"
        android:dividerHeight="2dp"
        android:layout_height="match_parent"
        android:scrollbars="none"
        ></ListView>
    
</RelativeLayout>

listview的item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img_listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="默认默认默认默认" />

</LinearLayout>

footview布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/foot"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff0"
        android:gravity="center"
        android:text="加载更多"
        android:textColor="@color/bright_foreground_inverse_material_dark"
        android:textSize="30sp" />

</FrameLayout>

求指点!

------解决思路----------------------
这个分页都得自己做了,那分页的大小当然也得自己搞咯
------解决思路----------------------
文字太多懒得看了。可以在 adapter方法里面加个自定义update

private void update(object obj){

list.clear();
list.ad(obj);
notifysetchange()--记不清怎么拼了;
}

------解决思路----------------------
每次先clea绑定到listview上的listr,然后这个list添加10条记录,然后调用adapter.notifyDataSetChanged()
  相关解决方案