public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private int typeid=1; private int count=1; private String NetString="http://api.expoon.com/AppNews/getNewsList/type/"; private List<TextBean.DataBean> list=new ArrayList<TextBean.DataBean>(); private MyAdapter adapter; private ImageLoader instance; private DrawerLayout drawerLayout; private XListView xlistview; private ListView lv; private List<String> str=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); xlistview = (XListView) findViewById(R.id.xlistview); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new MListViewAdapter()); lv.setOnItemClickListener(new MyOnItemClickListener()); instance = ImageLoader.getInstance(); getNetData(typeid,count); adapter = new MyAdapter(); xlistview.setAdapter(adapter); xlistview.setPullLoadEnable(true); //让XListView有上拉加载的功能; xlistview.setXListViewListener(this); Toast.makeText(this,count+" ",Toast.LENGTH_LONG).show(); } private void getNetData(int typeid,int count) { // 1/p/1 new MyAsyncTask().execute(NetString+typeid+"/p/"+count); } private class MyAsyncTask extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { return Utils.getNetString(strings[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); TextBean json = gson.fromJson(s, TextBean.class); //得到集合; List<TextBean.DataBean> data = json.getData(); //把从网络解析的数据放到adapter使用的集合里; list.addAll(data); adapter.notifyDataSetChanged(); uiComplete();// 让刷新头 或者刷新底部ui返回; } } private class MyAdapter extends BaseAdapter { private ViewHolder vh; private VHolder vholder; private TextView id; private TextView summary; private TextView title; private ImageView img; @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public int getItemViewType(int position) { //根据图片是否为空进行判断 int flag=0; if (list.get(position).getPic_url()==null){ flag=0; }else if (list.get(position).getPic_url()!=null){ flag=1; } return flag; } @Override public int getViewTypeCount() { //getview里有几个布局就写几 return 2; } @Override public View getView(int i, View view, ViewGroup viewGroup) { //根据getItemViewType的返回值进行判断加载布局 int type = getItemViewType(i); switch (type){ case 0: if (view==null){ vholder = new VHolder(); view = View.inflate(MainActivity.this, R.layout.xitem, null); view.setTag(vholder); }else{ vholder = (VHolder) view.getTag(); } TextView tv = view.findViewById(R.id.xitem1_tv); tv.setText("获取内容失败"); break; case 1: if (view==null){ vh = new ViewHolder(); view = View.inflate(MainActivity.this, R.layout.xitem, null); view.setTag(vh); }else{ vh = (ViewHolder) view.getTag(); } vh.id = view.findViewById(R.id.xitem_news_id); vh.summary = view.findViewById(R.id.xitem_news_summary); vh.title = view.findViewById(R.id.xitem_news_title); vh.img = view.findViewById(R.id.xitem_pic_url); vh.id.setText(list.get(i).getNews_id()); vh.summary.setText(list.get(i).getNews_summary()); vh.title.setText(list.get(i).getNews_title()); instance.displayImage(list.get(i).getPic_url(),vh.img, Utils.getDisplayImageOption()); break; } return view; } } //优化布局 class ViewHolder{ TextView id; TextView summary; TextView title; ImageView img; } //优化布局 class VHolder{ TextView tv; } /** * 下拉刷新 */ @Override public void onRefresh() { count=1; list.clear(); getNetData(typeid,count); // Toast.makeText(MainActivity.this,count+" ", Toast.LENGTH_LONG).show(); } /** * 上拉加载 */ @Override public void onLoadMore() { count+=1; getNetData(typeid,count); // Toast.makeText(MainActivity.this,count+" ",Toast.LENGTH_LONG).show(); } /** * 让刷新头 或者刷新底部ui返回; */ private void uiComplete(){ xlistview.stopRefresh();//停止刷新 xlistview.stopLoadMore();//停止上拉加载更多 xlistview.setRefreshTime("刚刚"); } private void initData() { str.add("新闻"); str.add("关注"); str.add("动态"); str.add("设置"); } private class MListViewAdapter extends BaseAdapter { @Override public int getCount() { return str.size(); } @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) { if (view==null){ view = View.inflate(MainActivity.this, R.layout.item, null); } TextView name = view.findViewById(R.id.item_str); name.setText(str.get(i)); return view; } } private class MyOnItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { typeid=i+1; list.clear(); getNetData(typeid,count); drawerLayout.closeDrawer(lv); Toast.makeText(MainActivity.this,typeid+" ",Toast.LENGTH_LONG).show(); } } }
接口切换
最新推荐文章于 2022-08-17 11:05:15 发布