1. 程式人生 > >ViewPager+Fragmnet+多條目+上下拉重新整理

ViewPager+Fragmnet+多條目+上下拉重新整理

 **上下拉重新整理依賴**      
  implementation project(':xlistviewlibrary')  
  **字串gson**
    implementation 'com.google.code.gson:gson:2.8.5'        
    **圖片框架**
    'com.nostra13.universalimageloader:universal-image-loader:1.9.5'  

佈局檔案

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_weight="7"
    android:layout_width="match_parent"
    android:layout_height="0dp">

</android.support.v4.view.ViewPager>
  <RadioGroup
      android:id="@+id/group"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:orientation="horizontal"
      android:layout_weight="1">

         <RadioButton
             android:id="@+id/btu1"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:button="@null"
             android:textColor="#fff"
             android:text="多條目列表"
             android:background="@drawable/selector"
             android:gravity="center"
             android:layout_weight="1"/>
      <RadioButton
          android:id="@+id/btu2"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:button="@null"
          android:textColor="#fff"
          android:text="上下拉重新整理"
          android:background="@drawable/selector"
          android:gravity="center"
          android:layout_weight="1"/>
      <RadioButton
          android:id="@+id/btu3"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:button="@null"
          android:textColor="#fff"
          android:background="@drawable/selector"
          android:text="我的"
          android:gravity="center"
          android:layout_weight="1"/>
  </RadioGroup>

MainActivity

package bawei.com.weichunhao20190104;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import bawei.com.weichunhao20190104.fragment.FragmentOne;
import bawei.com.weichunhao20190104.fragment.FragmentThree;
import bawei.com.weichunhao20190104.fragment.FragmentTwo;

public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private RadioGroup group;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

      viewPager = findViewById(R.id.viewPager);
        group = findViewById(R.id.group);

        group.check(group.getChildAt(0).getId());


           list = new ArrayList<>();
           list.add(new FragmentOne());
           list.add(new FragmentTwo());
           list.add(new FragmentThree());


           viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
               @Override
               public Fragment getItem(int i) {
                   return list.get(i);
               }

               @Override
               public int getCount() {
                   return list.size();
               }
           });

           viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
               @Override
               public void onPageScrolled(int i, float v, int i1) {

               }

               @Override
               public void onPageSelected(int i) {
                group.check(group.getChildAt(i).getId());
               }

               @Override
               public void onPageScrollStateChanged(int i) {

               }
           });
           group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(RadioGroup group, int checkedId) {
                   switch (checkedId){
                       case R.id.btu1:
                           viewPager.setCurrentItem(0);
                           break;
                       case R.id.btu2:
                           viewPager.setCurrentItem(1);
                           break;
                       case R.id.btu3:
                           viewPager.setCurrentItem(2);
                           break;
                   }
               }
           });
}

}

fragmentone.xml

<ListView
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:divider="@color/colorAccent"
    android:dividerHeight="1dp"
    >

</ListView>

FragmentOne
package bawei.com.weichunhao20190104.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import bawei.com.weichunhao20190104.Goods;
import bawei.com.weichunhao20190104.R;
import bawei.com.weichunhao20190104.netword.NetWordUtils;

public class FragmentOne extends Fragment {
private String urlString=“http://api.expoon.com/AppNews/getNewsList/type/1/p/0”;
private ListView listView;
private ImageLoader imageLoaderInstance;
private MAdapter mAdapter;
List<Goods.DataBean> list = new ArrayList<>();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragmentone, container, false);

    listView =(ListView) view.findViewById(R.id.listview);
    initData();
    mAdapter = new MAdapter();
    listView.setAdapter(mAdapter);
    return view;
}
private void initData() {
    new MAsynTask().execute(urlString);
}

private class MAdapter extends BaseAdapter {

    /**
     * 當前條目型別的種類數
     * 當前程式有兩種條目型別(純文字型別 ,文字+圖片條目型別)
     *
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return 2;
    }


    /**
     * 每次要顯示佈局條目的時候,要判斷當前條目是那種型別
     * 條目型別從0開始排序,當前有兩種條目型別,一種型別標識是0, 一種型別標識是1
     * 獲取條目檢視型別
     *
     * @param position
     */
    @Override
    public int getItemViewType(int position) {

        return position % 2;//

    }
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        int itemViewType = getItemViewType(position); //得到條目型別標識
        switch(itemViewType){
            case 0: //偶數條目 返回文字+圖片條目;
                ViewHolder0 viewHolder0 ;
                if (view == null) {
                    viewHolder0 = new ViewHolder0();
                    Log.e("wzq","偶數賦值");
                    view = View.inflate(getActivity(), R.layout.mybase, null);
                    viewHolder0.textView1 = view.findViewById(R.id.textView1);
                    viewHolder0.textView2 = view.findViewById(R.id.textView2);
                    viewHolder0.imageView = view.findViewById(R.id.imageView);
                    view.setTag(viewHolder0);
                }else {
                    viewHolder0 = (ViewHolder0) view.getTag();
                }
                viewHolder0.textView1.setText(list.get(position).getNews_id());
                viewHolder0.textView2.setText(list.get(position).getNews_title());
                ImageLoader imageLoaderInstance = ImageLoader.getInstance();

// imageLoaderInstance.displayImage(list.get(position).getPic_url(),viewHolder0.imageView);
imageLoaderInstance.displayImage(list.get(position).getPic_url(),viewHolder0.imageView);

                break;
            case 1: // 奇數條,只返回文字條目
                ViewHolder1 viewHolder1 =null;
                if (view == null) {
                    viewHolder1 = new ViewHolder1();
                    Log.e("wzq","奇數賦值");
                    view = View.inflate(getActivity(), R.layout.mybase1, null);
                    viewHolder1.tv1 = (TextView) view.findViewById(R.id.tv1);
                    viewHolder1.tv2 = (TextView) view.findViewById(R.id.tv2);
                    view.setTag(viewHolder1);
                }else {
                    viewHolder1 = (ViewHolder1) view.getTag();
                }
                viewHolder1.tv1.setText(list.get(position).getNews_id());
                viewHolder1.tv1.setText(list.get(position).getNews_title());

                break;

        }

        return view;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

}

private  class  MAsynTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        return NetWordUtils.getNetjson(strings[0]);


    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        Goods goods = gson.fromJson(s, Goods.class);
        List<Goods.DataBean> dataTemp = goods.getData();
        list.addAll(dataTemp);
        Log.e("wzq", list.size() + "");
        //更新介面卡
        mAdapter.notifyDataSetChanged();
    }
}
class ViewHolder0 {
    private TextView textView1;
    private TextView textView2;
    private ImageView imageView;
}

class ViewHolder1 {
    private TextView tv1;
    private TextView tv2;
}

}

Mapp
package bawei.com.weichunhao20190104.app;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class Mapp extends Application {

@Override
public void onCreate() {
    super.onCreate();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();

       ImageLoader.getInstance().init(configuration);
}

}

Goods
package bawei.com.weichunhao20190104;

import java.util.List;

public class Goods {

/**
 * status : 1
 * info : 獲取內容成功
 * data : [{"news_id":"13811","news_title":"深港澳臺千里連線,嘉年華會今夏入川","news_summary":"6月17\u201420日,\u201c2016成都深港澳臺嘉年華會\u201d(簡稱嘉年華會)將在成都世紀城國際會展中心舉辦。其主辦方勵展華博借力旗","pic_url":"http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg"},{"news_id":"13810","news_title":"第14屆溫州國際汽車展4月舉行 設9大主題展館","news_summary":"來自前不久舉行的溫州國際汽車展覽會第一次新聞釋出會的訊息, 2016第14屆溫州國際汽車展覽會定於4月7-10日在溫州國","pic_url":"http://f.expoon.com/sub/news/2016/01/21/580828_230x162_0.jpg"},{"news_id":"13808","news_title":"第十二屆中國(南安)國際水暖泵閥交易會 四大亮點","news_summary":"第十二屆中國(南安)國際水暖泵閥交易會將於2月10日至12日(即農曆正月初三至初五)在成功國際會展中心拉開帷幕。","pic_url":"http://f.expoon.com/sub/news/2016/01/21/745921_230x162_0.jpg"},{"news_id":"13805","news_title":"2016上海燈光音響展 商機無限,一觸即發","news_summary":"2016上海國際專業燈光音響展即日起全面啟動,海內外高階演藝裝置商貿平臺,商機無限,一觸即發。6大洲,80個國家,25,","pic_url":"http://f.expoon.com/sub/news/2016/01/21/158040_230x162_0.jpg"},{"news_id":"13804","news_title":"第四屆南京國際佛事展5月舉行","news_summary":"2016年,\u201c第四屆南京國際佛事文化用品展覽會\u201d將於5月26-29日在南京國際展覽中心舉辦。","pic_url":"http://f.expoon.com/sub/news/2016/01/21/865222_230x162_0.jpg"},{"news_id":"13802","news_title":"上海國際牛仔服裝博覽會 拓展國際貿易大市場","news_summary":"2016年第三屆上海國際牛仔服裝博覽會將於4月19-21日再次璀璨再現上海世博展覽館,共同探討牛仔流行趨勢,詮釋牛仔文化","pic_url":"http://f.expoon.com/sub/news/2016/01/20/370858_230x162_0.jpg"},{"news_id":"13800","news_title":"第三屆蘭州年貨會在甘肅國際會展中心本月19日開幕","news_summary":"由中國商業聯合會、甘肅省商業聯合會、蘭州市商務局主辦,甘肅省酒類商品管理局、蘭州市城關區商務局、第十四屆西安年貨會組委會","pic_url":"http://f.expoon.com/sub/news/2016/01/20/868385_230x162_0.jpg"},{"news_id":"13799","news_title":"首屆移動拍賣藝術博覽會啟動","news_summary":"首屆移動拍賣博覽會已於2016年1月全面啟動,由大咖拍賣主辦,聯合全國藝術機構共同打造拍賣藝術博覽會主會場,近百場拍賣專","pic_url":"http://f.expoon.com/sub/news/2016/01/20/768695_230x162_0.jpg"},{"news_id":"13798","news_title":"武漢金融理財投資博覽會將在5月舉辦","news_summary":"由武漢市貿促會、上海《理財週刊》社、湖北好博塔蘇斯展覽有限公司等單位聯合發起的\u201c2016武漢金融理財投資博覽會\u201d,將在武","pic_url":"http://f.expoon.com/sub/news/2016/01/20/512947_230x162_0.jpg"},{"news_id":"13796","news_title":"第三屆中國微商博覽會 3月底濟南舉辦","news_summary":"2015年,沸點天下開創了微商行業第一個展會\u2014\u2014中國微商博覽會,並於2015年成功舉行兩屆,讓微商展會從無到有,並且起了","pic_url":"http://f.expoon.com/sub/news/2016/01/20/348021_230x162_0.jpg"},{"news_id":"13793","news_title":"2016中國西部國際絲綢博覽會","news_summary":"\u201c2016年中國西部國際絲綢博覽會\u201d最新確定於2016年5月11日至15日在南充舉行。據悉,\u201c絲博會\u201d的會徽、會標及宣傳","pic_url":"http://f.expoon.com/sub/news/2016/01/19/113912_230x162_0.jpg"},{"news_id":"13792","news_title":"中國針棉織品交易會開拓\u201c西部市場\u201d","news_summary":"由國家商務部重點支援、中國紡織品商業協會主辦的第98屆中國針棉織品交易會將於3月15日~17日綻放成都。作為中國國內針棉","pic_url":"http://f.expoon.com/sub/news/2016/01/19/650175_230x162_0.jpg"},{"news_id":"13791","news_title":"樂山市第二十屆房地產展示交易會開幕","news_summary":"美麗樂山,生態宜居。今日,樂山市第二十屆房地產展示交易會在該市中心城區樂山廣場開幕,展會將持續到1月24日。","pic_url":"http://f.expoon.com/sub/news/2016/01/19/321787_230x162_0.jpg"},{"news_id":"13790","news_title":"2016華中屋面與建築防水技術展3月即將開幕","news_summary":"由湖北省建築防水協會聯合湖南、河南、江西、安徽五省建築防水協會主辦\u201c2016第二屆華中屋面與建築防水技術展覽會\u201d將於20","pic_url":"http://f.expoon.com/sub/news/2016/01/19/376254_230x162_0.jpg"},{"news_id":"13789","news_title":"2016海南國際旅遊貿易博覽會召開新聞釋出會","news_summary":"近日,三亞旅遊官方網從海南省\u201c首屆海博會\u201d新聞釋出會上獲悉,海南省\u201c首屆海博會\u201d將於2016年3月26日至4月1日在三亞","pic_url":"http://f.expoon.com/sub/news/2016/01/19/958046_230x162_0.jpg"},{"news_id":"13788","news_title":"2016阿里巴巴·貴州年貨節展銷會開幕","news_summary":"\u201c2016阿里巴巴·貴州年貨節\u201d的展銷會及迎春廟會昨日啟動。150多家餐飲商參與的美食節、近千個品種組成的年貨展銷會等,","pic_url":"http://f.expoon.com/sub/news/2016/01/19/371688_230x162_0.jpg"},{"news_id":"13787","news_title":"第二屆中國盆栽花卉交易會\u200b 本月28日開幕","news_summary":"據廣州市政府獲悉,經中國花卉協會和廣州市政府批准,第二屆中國盆栽花卉交易會將於本月28日至31日在廣州花卉博覽園舉行。屆","pic_url":"http://f.expoon.com/sub/news/2016/01/18/687647_230x162_0.jpg"},{"news_id":"13786","news_title":"李益:視野、品質、融合是展覽工程國際化的必由路徑","news_summary":"\u201c視野、品質、融合是中國展覽工程走向國際化的必由路徑。\u201d北京逸格天驕國際展覽有限公司副總經理李益日前在第二十二屆國際(常","pic_url":"http://f.expoon.com/sub/news/2016/01/18/343556_230x162_0.jpg"},{"news_id":"13785","news_title":"第八屆中國國際整合住宅產業博覽會將於5月在廣州舉辦","news_summary":"2016年1月14日,第八屆中國(廣州)國際整合住宅產業博覽會暨2016亞太建築科技論壇\u2014\u2014新聞釋出會在廣州館隆重召開。","pic_url":"http://f.expoon.com/sub/news/2016/01/18/581830_230x162_0.jpg"},{"news_id":"13784","news_title":"絲綢之路敦煌國際文化博覽會籌備工作進展順利","news_summary":"近日,絲綢之路(敦煌)國際文化博覽會組委會第二次會議在蘭召開。會議研究討論了省直廳局一對一服務保障沿線省區市方案、文博會","pic_url":"http://f.expoon.com/sub/news/2016/01/18/656693_230x162_0.jpg"}]
 */

private int status;
private String info;
private List<DataBean> data;

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public String getInfo() {
    return info;
}

public void setInfo(String info) {
    this.info = info;
}

public List<DataBean> getData() {
    return data;
}

public void setData(List<DataBean> data) {
    this.data = data;
}

public static class DataBean {
    /**
     * news_id : 13811
     * news_title : 深港澳臺千里連線,嘉年華會今夏入川
     * news_summary : 6月17—20日,“2016成都深港澳臺嘉年華會”(簡稱嘉年華會)將在成都世紀城國際會展中心舉辦。其主辦方勵展華博借力旗
     * pic_url : http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg
     */

    private String news_id;
    private String news_title;
    private String news_summary;
    private String pic_url;

    public String getNews_id() {
        return news_id;
    }

    public void setNews_id(String news_id) {
        this.news_id = news_id;
    }

    public String getNews_title() {
        return news_title;
    }

    public void setNews_title(String news_title) {
        this.news_title = news_title;
    }

    public String getNews_summary() {
        return news_summary;
    }

    public void setNews_summary(String news_summary) {
        this.news_summary = news_summary;
    }

    public String getPic_url() {
        return pic_url;
    }

    public void setPic_url(String pic_url) {
        this.pic_url = pic_url;
    }
}

}

多條目佈局檔案

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="36dp"
    android:layout_marginStart="36dp"
    android:layout_toEndOf="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"

    android:textSize="24sp" />**加粗樣式**

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_toEndOf="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:textColor="#ffff"
    android:textSize="24sp" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="22dp"
    android:layout_marginStart="22dp"
    android:src="@mipmap/ic_launcher" />

多條目佈局檔案

<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffff"
    android:layout_marginLeft="10dp"
    android:textSize="24sp"
    />

fragmenttwo.xml

<com.bwie.xlistviewlibrary.view.XListView
android:id="@+id/xlv"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
>

</com.bwie.xlistviewlibrary.view.XListView>




**FragmentTwo**




package bawei.com.weichunhao20190104.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.xlistviewlibrary.view.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import bawei.com.weichunhao20190104.Goods;
import bawei.com.weichunhao20190104.R;
import bawei.com.weichunhao20190104.netword.NetWordUtils;

public class FragmentTwo extends Fragment {
String baseUrl = “http://api.expoon.com/AppNews/getNewsList/type/1/p/”;
int pag;
private XListView xListView;
List<Goods.DataBean> list = new ArrayList<>();//大集合
private ImageLoader imageLoaderInstances;
private MyAdapter1 myApater;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragmenttwo, container, false);

       xListView = view.findViewById(R.id.xlv);
    imageLoaderInstances = ImageLoader.getInstance();
    myApater = new MyAdapter1();
    xListView.setAdapter(myApater);
    initData(pag);

    //設定上下拉邏輯
    xListView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
            pag=0;
            initData(pag);
        }

        @Override
        public void onLoadMore() {
            pag++;
            initData(pag);
        }
    });
       return view;
}
private void initData(int pag) {

    String Url = baseUrl+pag;
    new MAsycnTask().execute(Url);

}
private class  MyAdapter1 extends BaseAdapter {

    @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) {

        View viewItem = View.inflate(getActivity(), R.layout.mybase2, null);
        TextView textView = (TextView) viewItem.findViewById(R.id.textView);
        ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
        textView.setText(list.get(position).getNews_title());
        imageLoaderInstances.displayImage(list.get(position).getPic_url(), imageView);

        return viewItem;
    }
}

class  MAsycnTask extends AsyncTask<String,Void,String> {


    @Override
    protected String doInBackground(String... strings) {
        return NetWordUtils.getNetjson(strings[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Gson gson = new Gson();
        Goods goods = gson.fromJson(s, Goods.class);
        List<Goods.DataBean> data = goods.getData();

        list.addAll(data);
        myApater.notifyDataSetChanged();
        uiComplete();
    }
}

private void uiComplete() {

    xListView.setRefreshTime("剛剛");
    xListView.stopRefresh();//隱藏頭部
    xListView.stopLoadMore();//隱藏腳部;
}

}
NetWordUtils
package bawei.com.weichunhao20190104.netword;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetWordUtils {
private static String tag = “NetWordUtils”;

public  static  String getNetjson(String urlstring){

    try {
        URL url = new URL(urlstring);

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(8000);

        InputStream inputStream = httpURLConnection.getInputStream();

        //可拼的字串

        StringBuffer stringBuffer = new StringBuffer();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String temp = "";
        while ((temp = bufferedReader.readLine()) != null) {
            stringBuffer.append(temp);
            temp = "";
        }
        String data = stringBuffer.toString();
        Log.e(tag, "getData: " + data);
        return data;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
public static Bitmap getNetBitmap(String urlString) {

    try {
        //用URL封裝連結地址;
        URL url = new URL(urlString);
        //用url開啟連結
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //聯網的狀態碼
        int responseCode = urlConnection.getResponseCode();
        if(responseCode ==200){
            //連結上獲取輸入流
            InputStream inputStream = urlConnection.getInputStream();
            //把流直接轉換成bitmap(系統提供的BitmapFactory)
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//BitmapFactory是個工具類,系統提供的
            return bitmap;
        }else {
            Log.e(tag, "網路狀態碼:: "+responseCode );
        }




    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

}
許可權
android:name=“android.permission.WRITE_EXTERNAL_STORAGE”>
android:name=“android.permission.READ_EXTERNAL_STORAGE”>
android:name=“android.permission.INTERNET”>
android:name=".app.Mapp"