import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import bean.Max;
import view.XListView;
public class MainActivity extends AppCompatActivity {
Max max;
ViewPager vp;
MyAdapterxlv myAdapterxlv;
XListView xlv;
List<ImageView> listiv=new ArrayList<ImageView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(config);
vp=(ViewPager) findViewById(R.id.vp);
xlv=(XListView) findViewById(R.id.xlv);
initData();
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
getjsonbybean();
xlv.stopRefresh();
}
@Override
public void onLoadMore() {
getjsonbybean();
xlv.stopLoadMore();
}
});
}
class MyAdapterxlv extends BaseAdapter{
public static final int One=0;
public static final int Two=1;
@Override
public int getCount() {
if(max.getResult().getIndexProducts()!=null){
return max.getResult().getIndexProducts().size();
}
return 0;
}
@Override
public int getItemViewType(int position) {
if(position%2==0){
return One;
}else{
return Two;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@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) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
int type = getItemViewType(i);
if(view==null){
if(type==One){
view=View.inflate(MainActivity.this,R.layout.xlv_item,null);
holder1=new ViewHolder1();
holder1.iv1= (ImageView) view.findViewById(R.id.iv1);
holder1.tv1= (TextView) view.findViewById(R.id.tv1);
view.setTag(holder1);
}else{
view=View.inflate(MainActivity.this,R.layout.xlv_item2,null);
holder2=new ViewHolder2();
holder2.iv2= (ImageView) view.findViewById(R.id.iv2);
holder2.tv2= (TextView) view.findViewById(R.id.tv2);
view.setTag(holder2);
}
}else{
if(type==One){
holder1= (ViewHolder1) view.getTag();
}else if(type==Two){
holder2= (ViewHolder2) view.getTag();
}
}
if(type==One){
ImageLoader.getInstance().displayImage(max.getResult().getIndexProducts().get(i).getPic(),holder1.iv1);
holder1.tv1.setText(max.getResult().getIndexProducts().get(i).getName());
}else{
ImageLoader.getInstance().displayImage(max.getResult().getIndexProducts().get(i).getPic(),holder2.iv2);
holder2.tv2.setText(max.getResult().getIndexProducts().get(i).getName());
}
return view;
}
}
class ViewHolder1{
ImageView iv1;
TextView tv1;
}
class ViewHolder2{
ImageView iv2;
TextView tv2;
}
private void initData() {
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
getjsonbybean();
return null;
}
@Override
protected void onPostExecute(String s) {
initvpData();
myAdapterxlv= new MyAdapterxlv();
xlv.setAdapter(myAdapterxlv);
myAdapterxlv.notifyDataSetChanged();
super.onPostExecute(s);
}
}.execute();
}
private void initvpData() {
if(max.getResult().getIndexProducts()!=null){
for(int i=0;i<max.getResult().getIndexProducts().size();i++){
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(max.getResult().getIndexProducts().get(i).getPic(),iv);
listiv.add(iv);
}
}
MyAdaptervp myAdaptervp = new MyAdaptervp();
vp.setAdapter(myAdaptervp);
}
class MyAdaptervp extends PagerAdapter{
@Override
public int getCount() {
if(max.getResult().getIndexProducts()!=null){
return Integer.MAX_VALUE;
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int current=position%max.getResult().getIndexProducts().size();
container.addView(listiv.get(current));
return listiv.get(current);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
private void getjsonbybean() {
try {
URL url= new URL("http://www.babybuy100.com/API/getShopOverview.ashx");
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);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
另一种加载
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.当代种种事件不同于历史之处,在于我们不知道它们会产生什么后果。 2.经验和利益的偶然结合,往往会向人们揭示...
- Angular controller 通信 父子级通信 子级给父级发送数据 ...