1.MainActivity布局
<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” >
<GridView
android:id=”@+id/gridView1″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:numColumns=”2″ >
</GridView>
</RelativeLayout>
2.SecondActivity布局
<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=”.SecondActivity” >
<android.support.v4.view.ViewPager
android:id=”@+id/vp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
/>
<LinearLayout
android:id=”@+id/lin”
android:layout_width=”match_parent”
android:layout_height=”30dp”
android:background=”#000″
android:layout_alignParentBottom=”true”
android:layout_alignParentLeft=”true”
android:gravity=”center”
android:orientation=”horizontal” >
</LinearLayout>
</RelativeLayout>
3.gv_item布局
<?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” >
<ImageView
android:id=”@+id/imageView1″
android:layout_width=”150dp”
android:layout_height=”150dp”
android:scaleType=”fitXY”
android:src=”@drawable/ic_launcher” />
<TextView
android:id=”@+id/tcontent_info”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView” />
<ImageView
android:id=”@+id/imageView2″
android:layout_width=”45dp”
android:layout_height=”45dp”
android:src=”@drawable/ic_launcher” />
</LinearLayout>
4.bean包
package com.baway.bean;
import java.io.Serializable;
import java.util.List;
public class Goods {
public List<Data> data;
public class Data implements Serializable{
public String content_info;
public String head_img;
public String img;
public String nick_name;
public List<Like_user_list> like_user_list;
public class Like_user_list implements Serializable {
public String head_img;
}
}
}
5.MyApplication类
package com.baway.application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//初始化ImageLoader
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).memoryCacheSize(2*1024*1024).build();
ImageLoader.getInstance().init(configuration);
}
}
6.EntWorkUtils类
package com.baway.utils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class EntWorkUtils {
public static String getStr(){
//请求数据
String path=”http://mbfun.funwear.com/mbfun_server/index.php?m=Home&a=getCollocationListHot&page=1″;
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(path);
try {
HttpResponse response = client.execute(get);
int code = response.getStatusLine().getStatusCode();
if(code==200){
return EntityUtils.toString(response.getEntity(), “gbk”);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
7.MainActivity类
package com.baway.test;
import java.util.ArrayList;
import java.util.List;
import com.baway.adapter.MyAdapter;
import com.baway.bean.Goods;
import com.baway.bean.Goods.Data;
import com.baway.utils.EntWorkUtils;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gv;
private MyAdapter adapter;
private List<Data> list=new ArrayList<Data>();
private Handler hand=new Handler(){
public void handleMessage(Message msg) {
list = (List<Data>) msg.obj;
adapter=new MyAdapter(MainActivity.this,list);
gv.setAdapter(adapter);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件
gv = (GridView)findViewById(R.id.gridView1);
//解析数据
getData();
//监听事件
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“names”, list.get(position));
startActivity(intent);
}
});
}
private void getData() {
//创建内部类
new Thread(){
public void run() {
String json = EntWorkUtils.getStr();
Gson gson=new Gson();
Goods json2 = gson.fromJson(json, Goods.class);
List<Data> list=json2.data;
Message msg=Message.obtain();
msg.obj=list;
hand.sendMessage(msg);
};
}.start();
}
}
8.MyAdapter适配器类
package com.baway.adapter;
import java.util.List;
import com.baway.bean.Goods;
import com.baway.bean.Goods.Data;
import com.baway.test.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
Context context;
List<Data> list;
public MyAdapter(Context context, List<Data> list) {
this.context=context;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//优化
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
convertView=View.inflate(context, R.layout.gv_item, null);
holder.imageView1=(ImageView) convertView.findViewById(R.id.imageView1);
holder.tcontent_info=(TextView) convertView.findViewById(R.id.tcontent_info);
holder.imageView2=(ImageView) convertView.findViewById(R.id.imageView2);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.tcontent_info.setText(list.get(position).content_info);
ImageLoader.getInstance().displayImage(list.get(position).img, holder.imageView1);
ImageLoader.getInstance().displayImage(list.get(position).head_img, holder.imageView2);
return convertView;
}
class ViewHolder{
ImageView imageView1;
TextView tcontent_info;
ImageView imageView2;
}
}
9.SecondActivity类
package com.baway.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.baway.adapter.MyAdapter;
import com.baway.adapter.MyPageadapter;
import com.baway.bean.Goods;
import com.baway.bean.Goods.Data;
import com.baway.bean.Goods.Data.Like_user_list;
import com.baway.utils.EntWorkUtils;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class SecondActivity extends Activity {
private ViewPager vp;
private List<Like_user_list> like_user_list=new ArrayList<Like_user_list>();
private LinearLayout lin;
List<ImageView> ivlist = new ArrayList<ImageView>();;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//接受传值
Intent intent = getIntent();
Data data =(Data) intent.getSerializableExtra(“names”);
like_user_list=data.like_user_list;
//控件
vp = (ViewPager)findViewById(R.id.vp);
lin = (LinearLayout)findViewById(R.id.lin);
lin.getBackground().setAlpha(150);
//适配器
vp.setAdapter(new MyPageadapter(SecondActivity.this,like_user_list));
dotData();
vp.setCurrentItem(100000);
setTimer();
//监听事件
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < like_user_list.size(); i++) {
if(i==arg0%like_user_list.size()){
ivlist.get(arg0%like_user_list.size()).setImageResource(R.drawable.dot_selected);
}else{
ivlist.get(i).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void dotData() {
if(ivlist!=null){
ivlist.clear();
}
for (int i = 0; i < like_user_list.size(); i++) {
iv = new ImageView(this);
if(i==0){
//同步
iv.setImageResource(R.drawable.dot_selected);
}else{
iv.setImageResource(R.drawable.dot_normal);
}
ivlist.add(iv);
LayoutParams params=new LayoutParams(20, 20);
params.setMargins(12, 0, 12, 0);
lin.addView(iv, params);
}
}
private void setTimer() {
Timer tim=new Timer();
tim.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
int item = vp.getCurrentItem();
item++;
vp.setCurrentItem(item);
}
});
}
}, 3000, 2000);
}
}
10.MyPageadapter适配器类
package com.baway.adapter;
import java.util.List;
import com.baway.bean.Goods.Data.Like_user_list;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MyPageadapter extends PagerAdapter {
Context context;
List<Like_user_list> like_user_list;
public MyPageadapter(Context context,
List<Like_user_list> like_user_list) {
this.context=context;
this.like_user_list=like_user_list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(context);
ScaleType scaleType=ScaleType.CENTER;
iv.setScaleType(scaleType);
ImageLoader.getInstance().displayImage(like_user_list.get(position%like_user_list.size()).head_img, iv);
container.addView(iv);
return iv;
}
}