res文件:1、lv_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv_icon" android:layout_width="149dp" android:layout_height="86dp" android:layout_margin="10dp" android:background="@drawable/shape_images" android:padding="2dp" android:src="@drawable/moren" fresco:placeholderImage="@drawable/img_example" /> <LinearLayout android:layout_width="match_parent" android:layout_height="86dp" android:layout_marginBottom="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="李克强:决不能让农民的辛苦付出得不到回报" android:textColor="#636363" android:textSize="16sp" /> <TextView android:id="@+id/tv_addtime" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:gravity="right" android:text="2017/02/12" android:textColor="#636363" android:textSize="10sp" /> <TextView android:id="@+id/tv_laiyuan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:ellipsize="end" android:gravity="left" android:lines="2" android:maxLines="3" android:text="来源:中华人名共和国人民政" android:textSize="12sp" /> </LinearLayout> </LinearLayout>2、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginBottom="2dp" android:layout_marginTop="2dp" android:background="#3399cc" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="30dp"> <RelativeLayout android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="10" android:paddingTop="5dp" android:focusable="true" android:focusableInTouchMode="true"> <EditText android:id="@+id/edt_operator_name" android:layout_width="400dp" android:layout_height="35dp" android:background="@drawable/shape_edittext" android:gravity="center_vertical" android:hint="请输入您想搜索的文字" android:paddingLeft="40dp" android:textSize="20sp" /> <ImageView android:id="@+id/syncOperatorImg" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignBottom="@+id/edt_operator_name" android:layout_alignLeft="@+id/edt_operator_name" android:layout_alignTop="@+id/edt_operator_name" android:src="@drawable/serch" /> </RelativeLayout> <TextView android:id="@+id/tv_fenxiang" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_weight="2" android:text="分享" android:textColor="#ffffff" android:textSize="24sp" /> </LinearLayout> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/tv_sannong" style="@style/Tab_style" android:drawableLeft="@drawable/sannongtu" android:textSize="14dp" android:text="农业新闻" /> <RadioButton android:id="@+id/tv_shehui" style="@style/Tab_style" android:drawableLeft="@drawable/shehui" android:textSize="14dp" android:text="农业技术" /> <RadioButton android:id="@+id/tv_zhongzhi" style="@style/Tab_style" android:drawableLeft="@drawable/zhongzhi" android:textSize="14dp" android:text="农业政策" /> </RadioGroup> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRe" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:scrollbars="none"> </ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>adapter
public class NewsListViewAdapter extends BaseAdapter { private List<News> list; private News news = null; private LayoutInflater mInflater = null; private ViewHolder holder; private Context context; private Handler handler; private Bitmap bitmap; public NewsListViewAdapter(Context context, List<News> list) { super(); this.list = list; this.mInflater = LayoutInflater.from(context); this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { news = list.get(position); if (convertView == null) { convertView = mInflater.inflate(R.layout.lv_item, parent, false); holder = new ViewHolder(); holder.text_news_content = (TextView) convertView.findViewById(R.id.tv_laiyuan); holder.text_news_title = (TextView) convertView.findViewById(R.id.tv_title); holder.img_news = (SimpleDraweeView) convertView.findViewById(R.id.iv_icon); holder.text_addtime = (TextView) convertView.findViewById(R.id.tv_addtime); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); holder.text_news_content.setText(news.getShort_derail()); holder.text_news_title.setText(news.getTitle()); holder.img_news.setImageURI(ResUrl.url + news.getTop_img()); } return convertView; } private class ViewHolder { TextView text_news_content; TextView text_news_title; TextView text_addtime; SimpleDraweeView img_news; } }4、main_activity.java
下拉刷新:
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Thread(new Runnable() { @Override public void run() { count = 1; json = MyOkHttpUtils.doGet(ResUrl.getList +Class+ "&page=1&pagesize=10"); handler.sendEmptyMessage(2); } }).start(); } });
加载更多:
lv_news.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (view.getLastVisiblePosition() == (view.getCount() - 1)) { count++; new Thread(new Runnable() { @Override public void run() { json = MyOkHttpUtils.doGet(ResUrl.getList+Class + "&page=" + count + "&pagesize=10"); handler.sendEmptyMessage(0); } }).start(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });Handler 接受:json处理private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: if (json == null) { Toast.makeText(getApplicationContext(), "服务器异常!", Toast.LENGTH_SHORT).show(); } else { makeList(); Log.e("list2", list.size() + ""); adapter.notifyDataSetChanged(); } break; case 1: if (json != null) { makeList(); adapter.notifyDataSetChanged(); } break; case 2: swipeRefreshLayout.setRefreshing(false); makeList(); adapter.notifyDataSetChanged(); } } };
private void makeList() { Log.e("json2",json); try { // list.clear(); JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); news = new News(); news.setId(jsonObject.getString("Id")); news.setTitle(jsonObject.getString("Title")); news.setShort_derail(jsonObject.getString("Short_detail")); news.setTop_img(jsonObject.getString("Top_img")); news.setAddtime(jsonObject.getString("Addtime")); list.add(news); } } catch (JSONException e) { e.printStackTrace(); } }如果遇到第一次进来,页面不刷新,可采用以下方法:
new Handler().postDelayed(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); } }, 700);