MainActivity
package com.example.a0812jishi_test_xlistview;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import bean.Max;
import view.XListView;
public class MainActivity extends AppCompatActivity {
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
XListView xlv;
Max max;
View view;
List<ImageView> vpList;
ViewPager vp;
MyAdapter myAdapter;
List<Max.StoriesBean> list2=new ArrayList<Max.StoriesBean>();
List<Max.TopStoriesBean> list1=new ArrayList<Max.TopStoriesBean>();
boolean flag=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(config);
setContentView(R.layout.activity_main);
xlv=(XListView) findViewById(R.id.xlv);
initData();
myAdapter = new MyAdapter();
xlv.setAdapter(myAdapter);
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
getData();
xlv.stopRefresh();
}
},2000);
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
getData();
xlv.stopLoadMore();
}
},2000);
}
});
}
private void initData() {
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
getData();
return null;
}
@Override
protected void onPostExecute(String s) {
myAdapter.notifyDataSetChanged();
view=initViewPager();
xlv.addHeaderView(view);
super.onPostExecute(s);
}
}.execute();
}
private View initViewPager() {
View view=View.inflate(MainActivity.this,R.layout.title,null);
vp=view.findViewById(R.id.vp);
initvpData();
MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
vp.setAdapter(myPagerAdapter);
final TextView vptv=view.findViewById(R.id.vptv);
if(list1!=null){
vptv.setText(list1.get(0).getTitle());
}
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
vptv.setText(list1.get(position).getTitle());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
if(max!=null){
return max.getTop_stories().size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(vpList.get(position));
return vpList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
private void initvpData() {
vpList=new ArrayList<ImageView>();
if(max!=null){
for(int i=0;i<max.getTop_stories().size();i++){
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(max.getTop_stories().get(i).getImage(),iv);
vpList.add(iv);
}
}
}
private void getData() {
try {
URL url = new URL("https://news-at.zhihu.com/api/4/news/latest");
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
int code = conn.getResponseCode();
if(code==200){
Gson gson = new Gson();
max = gson.fromJson(new InputStreamReader(conn.getInputStream()), Max.class);
if(flag){
// list1.addAll(max.getTop_stories());
list1=max.getTop_stories();
list2.addAll(max.getStories());
}else{
list1=max.getTop_stories();
list2=max.getStories();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
if(list2!=null){
return list2.size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view==null){
view=View.inflate(MainActivity.this,R.layout.xlistview_item,null);
holder=new ViewHolder();
holder.xlv_tv1=view.findViewById(R.id.xlv_tv1);
holder.xlv_tv2=view.findViewById(R.id.xlv_tv2);
holder.iv=view.findViewById(R.id.iv);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.xlv_tv1.setText(list2.get(i).getGa_prefix());
holder.xlv_tv2.setText(list2.get(i).getTitle());
ImageLoader.getInstance().displayImage(list2.get(i).getImages().get(0),holder.iv);
return view;
}
}
class ViewHolder{
TextView xlv_tv1;
TextView xlv_tv2;
ImageView iv;
}
}
xlistview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv"/>
<LinearLayout
android:layout_marginLeft="15dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xlv_tv1"
android:textSize="18sp"
android:text="dddddddddd"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xlv_tv2"
android:textSize="18sp"
android:text="dddddddddd"/>
</LinearLayout>
</LinearLayout>