**
public class ImagerUtil {
public static void initImageLoader(Context context)public class MainActivity extends AppCompatActivity {
private ViewPager pager;
private XListView xlv;
private int index=3;
private String part="http://api.tianapi.com/tiyu/?key=36bafe109f1b68cc2c3829db69c7c180&num=";
private String url=part+index;
private int type=1;
private List<Bean.NewslistBean> data=new ArrayList<>();
public MyAdapter adapter1;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==0)
{
int item = pager.getCurrentItem();
pager.setCurrentItem(item+1);
handler.sendEmptyMessageDelayed(0,1000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = findViewById(R.id.pager);
xlv = findViewById(R.id.xlv);
if (TextUtil.iscon(MainActivity.this))
{
MyTask task = new MyTask(new MyTask.Back() {
@Override
public void setDate(String s) {
List<String> litt=new ArrayList<>();
Gson gson = new Gson();
Bean json = gson.fromJson(s, Bean.class);
List<Bean.NewslistBean> beans = json.getNewslist();
for (int i=0;i<beans.size();i++)
{
litt.add(beans.get(i).getPicUrl());
}
MnAdapter adapter=new MnAdapter(MainActivity.this,litt);
pager.setAdapter(adapter);
handler.sendEmptyMessageDelayed(0,1000);
}
});
task.execute("http://api.tianapi.com/tiyu/?key=36bafe109f1b68cc2c3829db69c7c180&num=10");
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
index=3;
url=part+index;
type=1;
requestData();
}
@Override
public void onLoadMore() {
index++;
url=part+index;
type=2;
requestData();
}
});
xlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
return true;
}
});
requestData();
}else {
TextUtil.open(MainActivity.this);
}
}
private void requestData() {
MyTask task1 = new MyTask(new MyTask.Back() {
@Override
public void setDate(String s) {
Gson gson = new Gson();
Bean json = gson.fromJson(s, Bean.class);
if (type==1)
{
data.clear();
}
data.addAll(json.getNewslist());
if (adapter1==null)
{
adapter1=new MyAdapter(MainActivity.this,data);
xlv.setAdapter(adapter1);
}else {
adapter1.notifyDataSetChanged();
}
if (type==1)
{
xlv.stopRefresh();
}else {
xlv.stopLoadMore();
}
}
});
task1.execute(url);
}
}
{
//自定义磁盘缓存路径 系统默认的缓存路径 sd...<package>..cache 当app被卸载时,此路径下的缓存图片一并被删除
//File cacheDir=getExternalC## 标题 ##acheDir();
File cacheDir= new File(Environment.getExternalStorageDirectory().getPath()+"/aaa");//自定义 sd卡的缓存路径
//第一步:对ImageLoader进行初使化
ImageLoaderConfiguration config2 = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config2);
}
public static DisplayImageOptions getImageOption(Context context)
{
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher) // 加载开始默认的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) //url爲空會显示该图片,自己放在drawable里面的
.showImageOnFail(R.mipmap.ic_launcher) //加载图片出现问题,会显示该图片
.cacheInMemory() //缓存用
.cacheOnDisc() //缓存用
.displayer(new RoundedBitmapDisplayer(5)) //图片圆角显示,值为整数
.build();
return options;
}
}
重点内容**