<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.a1603a.MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="200dp"
>
</android.support.v4.view.ViewPager>
<com.bawei.jane.mxlistview.view.XListView
android:layout_below="@id/vp"
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/img"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
---------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
private List<String> picUrl;
private XListView xlv;
private int pageIndex=1;//第一頁的數據
private String urlpart="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
private String url =urlpart + pageIndex;
private MyAdapter adapter;
private List<Result.DataBean> data = new ArrayList<>();
private int operType = 1;
private ViewPager viewpage;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int index = viewpage.getCurrentItem();
index++;
viewpage.setCurrentItem(index);
sendEmptyMessageDelayed(0,2000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picUrl = new ArrayList<>();
xlv = findViewById(R.id.xlv);
viewpage = findViewById(R.id.vp);
if (NetUtils.isConn(this)) {
initData();
MyTask myTask = new MyTask();
myTask.execute(url);
Toast.makeText(this, "有網絡", Toast.LENGTH_SHORT).show();
//設置適配器
viewpage.setAdapter(new MyPagerAdapter(picUrl, this));
//設置默認顯示頁面
viewpage.setCurrentItem(picUrl.size() * 10);
//發消息
handler.sendEmptyMessage(0);
} else {
NetUtils.openDg(this);
}
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
pageIndex = 1;
url = urlpart + pageIndex;
//從新賦值
operType = 1;
MyTask myTask = new MyTask();
myTask.execute(url);
}
@Override
public void onLoadMore() {
// 頁數增長
pageIndex++;
url = urlpart + pageIndex;
// 從新賦值
operType = 2;
Toast.makeText(MainActivity.this, "加載更多", Toast.LENGTH_SHORT).show();
MyTask task = new MyTask();
task.execute(url);
}
});
}
private void initData() {
picUrl.add("https://m.360buyimg.com//n0//jfs//t7441//10//64242474//419246//adb30a7d//598e95fbNd989ba0a.jpg");
picUrl.add("https://m.360buyimg.com//n0//jfs//t7504//338//63721388//491286//f5957f53//598e95f1N7f2adb87.jpg");
picUrl.add("https://m.360buyimg.com//n0//jfs//t9004//210//1160833155//647627//ad6be059//59b4f4e1N9a2b1532.jpg");
}
class MyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(5000);
c.setConnectTimeout(5000);
if (c.getResponseCode()==200){
InputStream inputStream = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str=reader.readLine())!=null){
builder.append(str);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class);
if (operType==1){
data.clear();
}
data.addAll(result.getData());
setAdapter();
//關閉頭尾佈局
if (operType ==1){
xlv.stopRefresh();
Date date = new Date(System.currentTimeMillis());//獲取當前時間
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化時間
String refreShTime = sf.format(date);
xlv.setRefreshTime(refreShTime);//設置刷新時間
}else {
xlv.stopLoadMore();//關閉尾佈局
}
}
}
public void setAdapter(){
if (adapter==null){
adapter = new MyAdapter(data,MainActivity.this);
xlv.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
-----------------------------------------------------------------------------------------------------
public class MyAdapter extends BaseAdapter{
private List<Result.DataBean> list;
private Context context;
private ImageView img;
private TextView title;
public MyAdapter(List<Result.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(context,R.layout.item,null );
img = view.findViewById(R.id.img);
title = view.findViewById(R.id.title);
title.setText(list.get(i).getTITLE());
ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(),img, ImageLoaderUtils.getImageOptions(context));
//圖片是object類型寫法:ImageLoader.getInstance().displayImage((String)list.get(i).getIMAGEURL(), img);
return view;
}
}
-----------------------------------------------------------------------------------------------------
public class MyPagerAdapter extends PagerAdapter {
private List<String> urls;
private Context context;
public MyPagerAdapter(List<String> urls, Context context) {
this.urls = urls;
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
//加載顯示圖片
ImageLoader.getInstance().displayImage(urls.get(position%urls.size()),imageView, ImageLoaderUtils.getImageOptions(context));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
----------------------------------------------------------------------------------------------------
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by lenovo on 2018/7/16.
*/
public class MyAdapter extends BaseAdapter {
private List<Result.DataBean> list;
private Context context;
private ImageView img;
private TextView title;
public MyAdapter(List<Result.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// view = View.inflate(context,R.layout.item,null );
// img = view.findViewById(R.id.img);
// title = view.findViewById(R.id.title);
// title.setText(list.get(i).getTITLE());
int type = getItemViewType(i);
MyHolader myHolader = null;
MyHolader2 myHolader2 = null;
if (view == null) {
switch (type) {
case 0:
view = View.inflate(context, R.layout.item1, null);
myHolader = new MyHolader();
myHolader.title = view.findViewById(R.id.title);
view.setTag(myHolader);
break;
case 1:
view = View.inflate(context, R.layout.item, null);
myHolader2 = new MyHolader2();
myHolader2.title = view.findViewById(R.id.title);
myHolader2.img = view.findViewById(R.id.img);
view.setTag(myHolader2);
break;
}
} else {
if (type == 0) {
myHolader = (MyHolader) view.getTag();
}
if (type == 1) {
myHolader2 = (MyHolader2) view.getTag();
}
}
if (type == 0) {
//1,賦值
myHolader.title.setText(list.get(i).getTITLE());
}
if (type == 1) {
//2,賦值
myHolader2.title.setText(list.get(i).getTITLE());
ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(), myHolader2.img, ImageLoaderUtils.getImageOptions(context));
}
//ImageLoader.getInstance().displayImage(list.get(i).getIMAGEURL(),img, ImageLoaderUtils.getImageOptions(context));
return view;
}
@Override
public int getItemViewType(int position) {
int type = getViewTypeCount();
if (list.get(position).getIMAGEURL() != null) {
return 1;
}
return 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
class MyHolader {
TextView title;
}
class MyHolader2 {
TextView title;
ImageView img;
}
}
----------------------------------------------------------------------------------------------------------------------------
<?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="vertical"
>
<TextView
android:text="位置"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="200dp" />
</LinearLayout>
---------
---------
---------
---------
<?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="vertical" > <TextView android:text="位置" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/title"/></LinearLayout>