public class MainActivity extends AppCompatActivity {
private XListView xListView;
private int pager=1;
private int types=1;
private List<ReaData.DataBean.ContentBean> dadts;
private String path="http://data.v.qq.com/videocms/getNewsvideoList.php?ref=pclient&appkey=6UkwV9DeHr9_PC&vsite=new_vshou&ename=new_vs_feature&report=web.news&_=1504358456351"+pager;
private MyAdapter adapter;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler( ){
@Override
public void handleMessage(Message msg) {
super.handleMessage( msg );
List<ReaData.DataBean.ContentBean> list= (List<ReaData.DataBean.ContentBean>) msg.obj;
}
};
private void setXlAdapter() {
if (adapter==null){
adapter = new MyAdapter( dadts, MainActivity.this );
xListView.setAdapter( adapter );
}else {
adapter.notifyDataSetChanged();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
dadts=new ArrayList<>( );
xListView= findViewById( R.id.xlv );
initTher();
initPlv();
}
private void initPlv() {
xListView.setPullLoadEnable( true );
xListView.setPullRefreshEnable( true );
xListView.setXListViewListener( new XListView.IXListViewListener() {
@Override
public void onRefresh() {
types=1;
pager=1;
path="http://data.v.qq.com/videocms/getNewsvideoList.php?ref=pclient&appkey=6UkwV9DeHr9_PC&vsite=new_vshou&ename=new_vs_feature&report=web.news&_=1504358456351"+pager;
initTher();
}
@Override
public void onLoadMore() {
types=2;
pager++;
path="http://data.v.qq.com/videocms/getNewsvideoList.php?ref=pclient&appkey=6UkwV9DeHr9_PC&vsite=new_vshou&ename=new_vs_feature&report=web.news&_=1504358456351"+pager;
initTher();
}
} );
}
private void initTher() {
new Thread( ){
@Override
public void run() {
super.run();
initData();
}
}.start();
}
private void initData() {
try {
URL url = new URL(path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod( "GET" );
urlConnection.setReadTimeout( 5000 );
urlConnection.setConnectTimeout( 5000 );
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
String s=streamToString(inputStream);
String replace = s.replace( "content(", "" ).replace( ");", "" );
Gson gson = new Gson();
ReaData reaData = gson.fromJson( replace, ReaData.class );
List<ReaData.DataBean.ContentBean> content = reaData.getData().getContent();
if (types==1){
dadts.clear();
}
dadts.addAll( content );
setXlAdapter();
if (types==1){
xListView.stopRefresh();
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd HH:mm:ss");
String time = sdf.format(date);
xListView.setRefreshTime( time );
}else {
xListView.stopLoadMore();
}
Message obtain = Message.obtain();
obtain.obj=content;
handler.sendMessage( obtain );
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String streamToString(InputStream stream) {
StringBuilder sbuilder=new StringBuilder();
String str;
BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
try {
while ((str=reader.readLine())!=null){
sbuilder.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return sbuilder.toString();
}
}
public class MyAdapter extends BaseAdapter {
List<ReaData.DataBean.ContentBean> list;
Context context;
private final int ONE_ITEM=0;
private final int TWO_ITEM=1;
private final DisplayImageOptions options;
public MyAdapter(List<ReaData.DataBean.ContentBean> list, Context context) {
this.list = list;
this.context = context;
//使用内存缓存
//使用磁盘缓存
//设置图片色彩模式
//设置图片的缩放模式
// .displayer(new RoundedBitmapDisplayer(50))//设置圆角
options = new DisplayImageOptions.Builder().cacheInMemory( true )//使用内存缓存
.cacheOnDisk( true )//使用磁盘缓存
.bitmapConfig( Bitmap.Config.RGB_565 )//设置图片色彩模式
.imageScaleType( ImageScaleType.EXACTLY )//设置图片的缩放模式
.showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
.showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
.displayer(new RoundedBitmapDisplayer(50))//设置圆角
.build();
}
@Override
public int getItemViewType(int position) {
String vplus_pic = list.get( position ).getVplus_pic();
if (vplus_pic==null){
return ONE_ITEM;
}else {
return TWO_ITEM;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@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(int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType( position );
if (itemViewType==ONE_ITEM){
MyHolder1 myHolder1;
if (convertView==null){
convertView=View.inflate( context,R.layout.item2,null );
myHolder1 = new MyHolder1();
myHolder1.tv=convertView.findViewById( R.id.tv );
convertView.setTag( myHolder1 );
}else {
myHolder1= (MyHolder1) convertView.getTag();
}
myHolder1.tv.setText( list.get( position ).getTitle());
return convertView;
} else {
MyHolder myHolder;
if (convertView==null){
convertView=View.inflate( context,R.layout.item1,null );
myHolder = new MyHolder();
myHolder.textView=convertView.findViewById( R.id.tv );
myHolder.imageView=convertView.findViewById( R.id.img );
convertView.setTag( myHolder );
}else {
myHolder= (MyHolder) convertView.getTag();
}
myHolder.textView.setText( list.get( position ).getTitle());
String vplus_pic = list.get( position ).getVplus_pic();
ImageLoader.getInstance().displayImage( vplus_pic,myHolder.imageView,options );
return convertView;
}
}
class MyHolder{
TextView textView;
ImageView imageView;
}
class MyHolder1{
TextView tv;
}
}
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
File cachefile = new File( Environment.getExternalStorageDirectory().getPath()+"/idm");
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽
.threadPoolSize(3)//线程池的数量
.threadPriority(4)
.memoryCacheSize(30*1024*1024)//设置内存缓存区大小
.diskCacheSize(30*1024*1024)//设置sd卡缓存区大小
.diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录
.writeDebugLogs()//打印日志内容
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理
.build();
ImageLoader.getInstance().init(configuration);
}
}
public class ReaData { 数据
main
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mydrawer"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#aff"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<com.bawei.jane.mxlistview.view.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlv"
>
</com.bawei.jane.mxlistview.view.XListView>
</android.support.v4.widget.DrawerLayout>
item1
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/img"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:layout_toRightOf="@+id/img"/>
</RelativeLayout>
item2
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</android.support.constraint.ConstraintLayout>