//mainActivity
package com.example.zhoukao11;
import java.util.ArrayList;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder;
import android.os.Bundle;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private int channelId=1;
private String pathSub1="http://www.93.gov.cn/93app/data.do?channelId=";
private String pathSub2="&startNum=";
private ViewPager vp;
private ArrayList<TextView> al_tv=new ArrayList<TextView>();
private String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Builder builder=new Builder(this);
ImageLoader.getInstance().init(builder.build());
setContentView(R.layout.activity_main);
LinearLayout ll=(LinearLayout) findViewById(R.id.ll);
for (int i = 0; i < ll.getChildCount(); i++) {
TextView tv=(TextView)ll.getChildAt(i);
final int num=i;
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (TextView tv : al_tv) {
tv.setBackgroundColor(Color.WHITE);
}
v.setBackgroundColor(Color.GREEN);
vp.setCurrentItem(num);
}
});
al_tv.add(tv);
}
al_tv.get(0).setBackgroundColor(Color.GREEN);
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return al_tv.size();
}
@Override
public Fragment getItem(int arg0) {
return F01.getF01(pathSub1+(channelId+arg0)+pathSub2);
}
});
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (TextView tv : al_tv) {
tv.setBackgroundColor(Color.WHITE);
}
al_tv.get(arg0).setBackgroundColor(Color.GREEN);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
//fragment
package com.example.zhoukao11;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import com.example.zhoukao11.MyJson.MyData;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.riso.myxlistview.MyListView;
import com.riso.myxlistview.XListView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class F01 extends Fragment {
private MyListView myListView;
private XListView lv;
private int startNum=1;
private String path;
private MyJson fromJson;
private ArrayList<MyData> al=new ArrayList<MyData> ();
private MyAdapter adapter;
private String string;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f01, container, false);
lv = (XListView) view.findViewById(R.id.lv);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
string = getArguments().getString("path");
path = string+startNum;
getNetData();
}
private void getNetData() {
new AsyncTask<Void, Void, Void>(){
@Override //在线程执行
protected Void doInBackground(Void... params) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
if (200==conn.getResponseCode()) {
fromJson = new Gson().fromJson(new InputStreamReader(conn.getInputStream()), MyJson.class);
al.addAll(fromJson.data);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
//设置设配器
if (adapter==null) {
adapter = new MyAdapter();
myListView = new MyListView(lv,adapter) {
@Override
public void shuaXin() {
al.clear();
startNum=1;
path = string+startNum;
getNetData();
}
@Override
public void jiaZai() {
startNum++;
path = string+startNum;
getNetData();
}
};
}else{
adapter.notifyDataSetChanged();
myListView.onOK();
}
};
}.execute();
}
public static F01 getF01(String path){
F01 f01 = new F01();
Bundle args=new Bundle();
args.putString("path", path);
f01.setArguments(args);
return f01;
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=View.inflate(getActivity(), R.layout.lv_item, null);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
TextView tv_subtitle = (TextView) convertView.findViewById(R.id.tv_subtitle);
TextView tv_gsname = (TextView) convertView.findViewById(R.id.tv_gsname);
TextView tv_date = (TextView) convertView.findViewById(R.id.tv_date);
String imageurl = al.get(position).IMAGEURL;
if (TextUtils.isEmpty(imageurl)||imageurl.equals("null")) {
iv.setVisibility(View.GONE);
}else{ //else 代表有图片
iv.setVisibility(View.VISIBLE);
ImageLoader.getInstance().displayImage(imageurl, iv);
}
tv_title.setText(al.get(position).TITLE);
tv_subtitle.setText(al.get(position).SUBTITLE);
tv_gsname.setText(al.get(position).FROMNAME);
tv_date.setText(al.get(position).SHOWTIME);
return convertView;
}
}
}
//bean
package com.example.zhoukao11;
import java.util.ArrayList;
public class MyJson {
public ArrayList<MyData> data;
public class MyData{
public String TITLE;
public String SUBTITLE;
public String IMAGEURL;
public String FROMNAME;
public String SHOWTIME;
}
}
//main——xml
<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=".MainActivity" >
<HorizontalScrollView
android:id="@+id/sh"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="赵小龙帅"
/>
<TextView
android:id="@+id/tv02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="老板腰好"
/>
<TextView
android:id="@+id/tv03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="老板活好"
/>
<TextView
android:id="@+id/tv04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="老板肾虚"
/>
<TextView
android:id="@+id/tv05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="老板手雷"
/>
<TextView
android:id="@+id/tv06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="16sp"
android:text="老板萎了"
/>
</LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/sh"
/>
</RelativeLayout>
//fragment_xml
<?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" >
<com.riso.myxlistview.XListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></com.riso.myxlistview.XListView >
</LinearLayout>
//item
<?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/iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"
android:padding="4dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="老腰好了"
android:layout_toRightOf="@id/iv"
android:padding="4dp"
/>
<TextView
android:id="@+id/tv_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="老腰又坏了,不能老耕地啊,和拱白菜,zxvxxxxxvgasdasfadgasgagasgasgasxxxxxxxxxxxx"
android:layout_toRightOf="@id/iv"
android:layout_below="@id/tv_title"
android:padding="4dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv"
android:layout_below="@id/tv_subtitle"
>
<TextView
android:id="@+id/tv_gsname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="腾讯"
android:padding="4dp"
/>
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="2016-10-08"
android:padding="4dp"
/>
</LinearLayout>
</RelativeLayout>
listview分页加载
最新推荐文章于 2021-05-26 01:45:38 发布