主页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="test.bwei.com.zjl20171021.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ff3360"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是侧滑"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
///Fragment1和Fragment2一样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"/>
</LinearLayout>
/fragment3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="match_parent"
android:layout_height="50dp">
<LinearLayout
android:id="@+id/hll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
<com.youth.banner.Banner
android:id="@+id/mybanner"
android:layout_width="match_parent"
android:layout_height="150dp">
</com.youth.banner.Banner>
<android.support.v4.view.ViewPager
android:layout_weight="1"
android:id="@+id/hvp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
//XListView 页面布局fragment_fxf.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<test.bwei.com.zjl20171021.xListview.XListView
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</test.bwei.com.zjl20171021.xListview.XListView>
</LinearLayout>
/ lv_item1_layout.xml
<?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="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题加载中..."
android:gravity="center"
android:textSize="20dp"
/>
<TextView
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="内容加载中..."
android:textSize="20dp"
/>
</LinearLayout>
</LinearLayout>
/ lv_item2_layout.xml
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="500dp"
android:layout_height="200dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题加载中..."
android:gravity="center"
android:textSize="20dp"
/>
<TextView
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="内容加载中..."
android:textSize="20dp"
/>
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
/ xlistview的布局xlistview_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/xlistview_footer_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<ProgressBar
android:id="@+id/xlistview_footer_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="@+id/xlistview_footer_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/xlistview_footer_hint_normal" />
</RelativeLayout>
</LinearLayout>
/ xlistview的布局xlistview_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<RelativeLayout
android:id="@+id/xlistview_header_content"
android:layout_width="fill_parent"
android:layout_height="60dp" >
<LinearLayout
android:id="@+id/xlistview_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/xlistview_header_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_hint_normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_last_time"
android:textSize="12sp" />
<TextView
android:id="@+id/xlistview_header_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/xlistview_header_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-35dp"
android:src="@mipmap/ic_launcher" />
<ProgressBar
android:id="@+id/xlistview_header_progressbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-40dp"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
/ 配置的
android:name=""
/ 加载的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
/ 主页面的代码
package test.bwei.com.zjl20171021;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import test.bwei.com.zjl20171021.fragment.Fragment1;
import test.bwei.com.zjl20171021.fragment.Fragment2;
import test.bwei.com.zjl20171021.fragment.Fragment3;
public class MainActivity extends AppCompatActivity {
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button1 = (Button) findViewById(R.id.button);
button3.setTextColor(Color.RED);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment1).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment2).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fl, fragment3).commit();
getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
getSupportFragmentManager().beginTransaction().show(fragment1).commit();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setTextColor(Color.RED);
button2.setTextColor(Color.BLACK);
button3.setTextColor(Color.BLACK);
getSupportFragmentManager().beginTransaction().show(fragment1).commit();
getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setTextColor(Color.BLACK);
button2.setTextColor(Color.RED);
button3.setTextColor(Color.BLACK);
getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
getSupportFragmentManager().beginTransaction().show(fragment2).commit();
getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setTextColor(Color.BLACK);
button2.setTextColor(Color.BLACK);
button3.setTextColor(Color.RED);
getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
getSupportFragmentManager().beginTransaction().show(fragment3).commit();
}
});
}
}
/ Fragment1和Fragment2的代码一样
package test.bwei.com.zjl20171021.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import test.bwei.com.zjl20171021.R;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class Fragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment1, container, false);
return inflate;
}
}
/ Fragment3的代码
package test.bwei.com.zjl20171021.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.youth.banner.Banner;
import java.util.ArrayList;
import java.util.List;
import test.bwei.com.zjl20171021.Fragment_fxf;
import test.bwei.com.zjl20171021.GlideImaGlideImageLoader;
import test.bwei.com.zjl20171021.R;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class Fragment3 extends Fragment{
final String[] titles = new String[]{"推荐", "课程", "实战", "职业路径"};
private LinearLayout hll;
private ViewPager hvp;
private List<TextView> textlist = new ArrayList<>();
private View view;
private HorizontalScrollView hsv;
ArrayList<String> mlist;
Banner mbanner;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment3, container, false);
mbanner = view.findViewById(R.id.mybanner);
mbanner.setImageLoader(new GlideImaGlideImageLoader());
ArrayList<String> mlist = new ArrayList<>();
mlist.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg");
mlist.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg");
mlist.add("http://img3.3lian.com/2013/s1/20/d/57.jpg");
mbanner.setImages(mlist);
mbanner.start();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
}
private void initView() {
hsv = (HorizontalScrollView) view.findViewById(R.id.hsv);
hll = (LinearLayout) view.findViewById(R.id.hll);
hvp = (ViewPager) view.findViewById(R.id.hvp);
for (int i = 0; i < titles.length; i++) {
TextView textView = new TextView(getActivity());
textView.setText(titles[i]);
textView.setTextSize(18);
textView.setWidth(150);
textView.setId(i + 1000);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
hvp.setCurrentItem(id - 1000);
}
});
if (i == 0) {
textView.setTextColor(Color.RED);
} else {
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 20, 10);
//添加到布局
hll.addView(textView, layoutParams);
//添加到集合
textlist.add(textView);
}
hvp.setAdapter(new MAdapter(getActivity().getSupportFragmentManager()));
hvp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int i = 0; i < textlist.size(); i++) {
if (position == i) {
textlist.get(i).setTextColor(Color.RED);
} else {
textlist.get(i).setTextColor(Color.BLACK);
}
}
TextView textView = textlist.get(position);
int i = textView.getWidth() + 10;
hsv.scrollTo(i * position, 0);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
class MAdapter extends FragmentPagerAdapter {
public MAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//http://mnews.gw.com.cn/wap/data/news/分类标识/page_页数.json
String[] title = new String[]{"xbsjxw", "txs", "toutiao", "news/mobile/jbgg", "news/mobile/mgxw",
"news/mobile/ggxw", "news/mobile/jjxw", "news/mobile/lcxw"};
return Fragment_fxf.getinstans("http://mnews.gw.com.cn/wap/data/news/" + title[position] + "/page_");
}
@Override
public int getCount() {
return textlist.size();
}
}
}
/ Fragment_fxf,的代码
package test.bwei.com.zjl20171021;
import android.os.AsyncTask;
import android.os.Bundle;
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.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import test.bwei.com.zjl20171021.xListview.XListView;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class Fragment_fxf extends Fragment implements XListView.IXListViewListener {
List<LvBean> list = new ArrayList<>();
String title;
int n = 1;
DisplayImageOptions options;
XListView xlv;
private View view;
private MAdapter adapter;
public static Fragment_fxf getinstans(String title) {
Fragment_fxf fragment_fxf = new Fragment_fxf();
Bundle bundle = new Bundle();
bundle.putString("title", title);
fragment_fxf.setArguments(bundle);
return fragment_fxf;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_fxf, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//sd卡
options = new DisplayImageOptions.Builder()
.cacheOnDisk(true) //sd卡
.cacheInMemory(true)
.build();
//接收路径
Bundle arguments = getArguments();
title = arguments.getString("title");
//初始化控件
xlv = (XListView) view.findViewById(R.id.xlv);
//必须写
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(this);
Log.e("Conan", title + "1.json");
adapter = new MAdapter();
xlv.setAdapter(adapter);
new MAsyncTask().execute(title + "1.json");
}
public void onLoad() {
xlv.stopRefresh();
xlv.stopLoadMore();
xlv.setRefreshTime("刚刚");
}
//下拉刷新
@Override
public void onRefresh() {
list.clear();
new MAsyncTask().execute(title + "1.json");
onLoad();
}
@Override
public void onLoadMore() {
n++;
new MAsyncTask().execute(title + n + ".json");
onLoad();
}
//网络获取
class MAsyncTask extends AsyncTask<String, Void, String> {
private StringBuilder builder;
@Override
protected String doInBackground(String... strings) {
try {
//获取url
URL url = new URL(strings[0]);
//请求网络
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
//判断是否返回成功
if (code == 200) {
//获取网络信息
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
builder = new StringBuilder();
String s = "";
//拼接
while ((s = bf.readLine()) != null) {
builder.append(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("Conan", s + "+-+-+-+-+-+-+-+-ss");
try {
JSONArray array = new JSONArray(s);
JSONObject object = array.optJSONObject(0);
JSONArray array2 = object.optJSONArray("data");
for (int i = 0; i < array2.length(); i++) {
String titleJson = array2.optJSONObject(i).optString("title");
String summaryJson = array2.optJSONObject(i).optString("summary");
String imgJson = array2.optJSONObject(i).optString("img");
if (imgJson.equals("")) {
list.add(new LvBean(titleJson, summaryJson, "http://news.xinhuanet.com/politics/2017-09/30/1121752088_15067520678551n.jpg"));
} else {
list.add(new LvBean(titleJson, summaryJson, imgJson));
}
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//适配器
class MAdapter extends BaseAdapter {
@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;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
int type = getItemViewType(i);
switch (type) {
case 0:
ViewHodler vh;
if (view == null) {
view = View.inflate(getActivity(), R.layout.lv_item1_layout, null);
vh = new ViewHodler();
vh.title = (TextView) view.findViewById(R.id.title);
vh.summary = (TextView) view.findViewById(R.id.summary);
vh.img = (ImageView) view.findViewById(R.id.img);
view.setTag(vh);
} else {
vh = (ViewHodler) view.getTag();
}
vh.title.setText(list.get(i).getTitle());
vh.summary.setText(list.get(i).getSummary());
ImageLoader.getInstance().displayImage(list.get(i).getImg(), vh.img, options);
break;
case 1:
ViewHodler vh1;
if (view == null) {
view = View.inflate(getActivity(), R.layout.lv_item2_layout, null);
vh1 = new ViewHodler();
vh1.title = (TextView) view.findViewById(R.id.title);
vh1.summary = (TextView) view.findViewById(R.id.summary);
vh1.img = (ImageView) view.findViewById(R.id.img);
view.setTag(vh1);
} else {
vh1 = (ViewHodler) view.getTag();
}
vh1.title.setText(list.get(i).getTitle());
vh1.summary.setText(list.get(i).getSummary());
ImageLoader.getInstance().displayImage(list.get(i).getImg(), vh1.img, options);
break;
}
return view;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return 0;
} else {
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
class ViewHodler {
TextView title;
TextView summary;
ImageView img;
}
}
}
/ GlideImaGlideImageLoader
package test.bwei.com.zjl20171021;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class GlideImaGlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
/ LvBean
package test.bwei.com.zjl20171021;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class LvBean {
private String title;
private String summary;
private String img;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public LvBean(String title, String summary, String img) {
this.title = title;
this.summary = summary;
this.img = img;
}
@Override
public String toString() {
return "LvBean{" +
"title='" + title + '\'' +
", summary='" + summary + '\'' +
", img='" + img + '\'' +
'}';
}
}
/ MApp的代码
package test.bwei.com.zjl20171021;
import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class MApp extends Application {
@Override
public void onCreate() {
super.onCreate();
File file = getExternalCacheDir();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).memoryCacheExtraOptions(200, 200)//配置内存缓存图片的尺寸
.memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小
.threadPoolSize(3)//配置加载图片的线程数
.threadPriority(1000)//配置线程的优先级
.diskCache(new UnlimitedDiscCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径
.diskCacheFileCount(50)//配置sdcard缓存文件的数量
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字
.diskCacheSize(50 * 1024 * 1024)//在sdcard缓存50MB .memoryCacheExtraOptions(200, 200)//配置内存缓存图片的尺寸
.memoryCacheSize(2 * 1024 * 1024)//配置内存缓存的大小
.threadPoolSize(3)//配置加载图片的线程数
.threadPriority(1000)//配置线程的优先级
.diskCache(new UnlimitedDiscCache(file))//UnlimitedDiskCache 限制这个图片的缓存路径
.diskCacheFileCount(50)//配置sdcard缓存文件的数量
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//MD5这种方式生成缓存文件的名字
.diskCacheSize(50 * 1024 * 1024)//在sdcard缓存50MB
.build();
ImageLoader.getInstance().init(build);
}
}
/ XListView插件代码
package test.bwei.com.zjl20171021.xListview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import test.bwei.com.zjl20171021.R;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class XListView extends ListView implements AbsListView.OnScrollListener {
private float mLastY = -1; // save event y
private Scroller mScroller; // 用于回滚
private OnScrollListener mScrollListener; // 回滚监听
// 触发刷新和加载更多接口.
private IXListViewListener mListViewListener;
// -- 头部的View
private XListViewHeader mHeaderView;
// 查看头部的内容,用它计算头部高度,和隐藏它
// 当禁用的时候刷新
private RelativeLayout mHeaderViewContent;
private TextView mHeaderTimeView;
private int mHeaderViewHeight; // 头部View的高
private boolean mEnablePullRefresh = true;
private boolean mPullRefreshing = false; // 是否刷新.
// -- 底部的View
private XListViewFooter mFooterView;
private boolean mEnablePullLoad;
private boolean mPullLoading;
private boolean mIsFooterReady = false;
// 总列表项,用于检测列表视图的底部
private int mTotalItemCount;
// for mScroller, 滚动页眉或者页脚
private int mScrollBack;
private final static int SCROLLBACK_HEADER = 0;// 顶部
private final static int SCROLLBACK_FOOTER = 1;// 下部
private final static int SCROLL_DURATION = 400; // 滚动回时间
private final static int PULL_LOAD_MORE_DELTA = 50; // 当大于50PX的时候,加载更多
private final static float OFFSET_RADIO = 1.8f; // support iOS like pull
// feature.
/**
* @param context
*/
public XListView(Context context) {
super(context);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs) {
super(context, attrs);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initWithContext(context);
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
// 初始化头部View
mHeaderView = new XListViewHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView
.findViewById(R.id.xlistview_header_content);
mHeaderTimeView = (TextView) mHeaderView
.findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);// 把头部这个视图添加进去
// 初始化底部的View
mFooterView = new XListViewFooter(context);
// 初始化头部高度
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
}
@Override
public void setAdapter(ListAdapter adapter) {
// 确定XListViewFooter是最后底部的View, 并且只有一次
if (mIsFooterReady == false) {
mIsFooterReady = true;
addFooterView(mFooterView);
}
super.setAdapter(adapter);
}
/**
* 启用或禁用下拉刷新功能.
*
* @param enable
*/
public void setPullRefreshEnable(boolean enable) {
mEnablePullRefresh = enable;
if (!mEnablePullRefresh) { // 禁用,隐藏内容
mHeaderViewContent.setVisibility(View.INVISIBLE);// 如果为false则隐藏下拉刷新功能
} else {
mHeaderViewContent.setVisibility(View.VISIBLE);// 否则就显示下拉刷新功能
}
}
/**
* 启用或禁用加载更多的功能.
*
* @param enable
*/
public void setPullLoadEnable(boolean enable) {
mEnablePullLoad = enable;
if (!mEnablePullLoad) {
mFooterView.hide();// 隐藏
mFooterView.setOnClickListener(null);
} else {
mPullLoading = false;
mFooterView.show();// 显示
mFooterView.setState( XListViewFooter.STATE_NORMAL);
// both "上拉" 和 "点击" 将调用加载更多.
mFooterView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startLoadMore();
}
});
}
}
/**
* 停止刷新, 重置头视图.
*/
public void stopRefresh() {
if (mPullRefreshing == true) {
mPullRefreshing = false;
resetHeaderHeight();
}
}
/**
* stop load more, reset footer view.
*/
public void stopLoadMore() {
if (mPullLoading == true) {
mPullLoading = false;
mFooterView.setState( XListViewFooter.STATE_NORMAL);
}
}
/**
* 設置最後一次刷新時間
*
* @param time
*/
@SuppressLint("SimpleDateFormat")
public void setRefreshTime(String time) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());
//获取当前时间
String str = formatter.format(curDate);
mHeaderTimeView.setText(str);
}
private void invokeOnScrolling() {
if (mScrollListener instanceof OnXScrollListener) {
OnXScrollListener l = (OnXScrollListener) mScrollListener;
l.onXScrolling(this);
}
}
private void updateHeaderHeight(float delta) {
mHeaderView.setVisiableHeight((int) delta
+ mHeaderView.getVisiableHeight());
if (mEnablePullRefresh && !mPullRefreshing) { // 未处于刷新状态,更新箭头
if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mHeaderView.setState( XListViewHeader.STATE_READY);
} else {
mHeaderView.setState( XListViewHeader.STATE_NORMAL);
}
}
setSelection(0); // scroll to top each time
}
/**
* 重置头视图的高度
*/
private void resetHeaderHeight() {
int height = mHeaderView.getVisiableHeight();
if (height == 0) // 不显示.
return;
// 不显示刷新和标题的时候,什么都不显示
if (mPullRefreshing && height <= mHeaderViewHeight) {
return;
}
int finalHeight = 0; // 默认:滚动回头.
// 当滚动回显示所有头标题时候,刷新
if (mPullRefreshing && height > mHeaderViewHeight) {
finalHeight = mHeaderViewHeight;
}
mScrollBack = SCROLLBACK_HEADER;
mScroller.startScroll(0, height, 0, finalHeight - height,
SCROLL_DURATION);
// 触发刷新
invalidate();
}
// 改变底部视图高度
private void updateFooterHeight(float delta) {
int height = mFooterView.getBottomMargin() + (int) delta;
if (mEnablePullLoad && !mPullLoading) {
if (height > PULL_LOAD_MORE_DELTA) { // 高度足以调用加载更多
mFooterView.setState( XListViewFooter.STATE_READY);
} else {
mFooterView.setState( XListViewFooter.STATE_NORMAL);
}
}
mFooterView.setBottomMargin(height);
// setSelection(mTotalItemCount - 1); // scroll to bottom
}
private void resetFooterHeight() {
int bottomMargin = mFooterView.getBottomMargin();
if (bottomMargin > 0) {
mScrollBack = SCROLLBACK_FOOTER;
mScroller.startScroll(0, bottomMargin, 0, -bottomMargin,
SCROLL_DURATION);
invalidate();
}
}
// 开始加载更多
private void startLoadMore() {
mPullLoading = true;
mFooterView.setState( XListViewFooter.STATE_LOADING);
if (mListViewListener != null) {
mListViewListener.onLoadMore();
}
}
// 触发事件
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mLastY == -1) {
mLastY = ev.getRawY();
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
final float deltaY = ev.getRawY() - mLastY;
mLastY = ev.getRawY();
System.out.println("数据监测:" + getFirstVisiblePosition() + "---->"
+ getLastVisiblePosition());
if (getFirstVisiblePosition() == 0
&& (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) {
// 第一项显示,标题显示或拉下来.
updateHeaderHeight(deltaY / OFFSET_RADIO);
invokeOnScrolling();
} else if (getLastVisiblePosition() == mTotalItemCount - 1
&& (mFooterView.getBottomMargin() > 0 || deltaY < 0)) {
// 最后一页,已停止或者想拉起
updateFooterHeight(-deltaY / OFFSET_RADIO);
}
break;
default:
mLastY = -1; // 重置
if (getFirstVisiblePosition() == 0) {
// 调用刷新,如果头部视图高度大于设定高度。
if (mEnablePullRefresh
&& mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mPullRefreshing = true;// 那么刷新
mHeaderView.setState( XListViewHeader.STATE_REFRESHING);
if (mListViewListener != null) {
mListViewListener.onRefresh();
}
}
resetHeaderHeight();// 刷新完毕,重置头部高度,也就是返回上不
}
if (getLastVisiblePosition() == mTotalItemCount - 1) {
// 调用加载更多.
if (mEnablePullLoad
&& mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
startLoadMore();// 如果底部视图高度大于可以加载高度,那么就开始加载
}
resetFooterHeight();// 重置加载更多视图高度
}
break;
}
return super.onTouchEvent(ev);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
if (mScrollBack == SCROLLBACK_HEADER) {
mHeaderView.setVisiableHeight(mScroller.getCurrY());
} else {
mFooterView.setBottomMargin(mScroller.getCurrY());
}
postInvalidate();
invokeOnScrolling();
}
super.computeScroll();
}
@Override
public void setOnScrollListener(OnScrollListener l) {
mScrollListener = l;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// 发送到用户的监听器
mTotalItemCount = totalItemCount;
if (mScrollListener != null) {
mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
public void setXListViewListener(IXListViewListener l) {
mListViewListener = l;
}
/**
* 你可以监听到列表视图,OnScrollListener 或者这个. 他将会被调用 , 当头部或底部触发的时候
*/
public interface OnXScrollListener extends OnScrollListener {
public void onXScrolling(View view);
}
/**
* 实现这个接口来刷新/负载更多的事件
*/
public interface IXListViewListener {
public void onRefresh();
public void onLoadMore();
}
}
/ XListViewFooter插件的代码
package test.bwei.com.zjl20171021.xListview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import test.bwei.com.zjl20171021.R;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class XListView extends ListView implements AbsListView.OnScrollListener {
private float mLastY = -1; // save event y
private Scroller mScroller; // 用于回滚
private OnScrollListener mScrollListener; // 回滚监听
// 触发刷新和加载更多接口.
private IXListViewListener mListViewListener;
// -- 头部的View
private XListViewHeader mHeaderView;
// 查看头部的内容,用它计算头部高度,和隐藏它
// 当禁用的时候刷新
private RelativeLayout mHeaderViewContent;
private TextView mHeaderTimeView;
private int mHeaderViewHeight; // 头部View的高
private boolean mEnablePullRefresh = true;
private boolean mPullRefreshing = false; // 是否刷新.
// -- 底部的View
private XListViewFooter mFooterView;
private boolean mEnablePullLoad;
private boolean mPullLoading;
private boolean mIsFooterReady = false;
// 总列表项,用于检测列表视图的底部
private int mTotalItemCount;
// for mScroller, 滚动页眉或者页脚
private int mScrollBack;
private final static int SCROLLBACK_HEADER = 0;// 顶部
private final static int SCROLLBACK_FOOTER = 1;// 下部
private final static int SCROLL_DURATION = 400; // 滚动回时间
private final static int PULL_LOAD_MORE_DELTA = 50; // 当大于50PX的时候,加载更多
private final static float OFFSET_RADIO = 1.8f; // support iOS like pull
// feature.
/**
* @param context
*/
public XListView(Context context) {
super(context);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs) {
super(context, attrs);
initWithContext(context);
}
public XListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initWithContext(context);
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
// 初始化头部View
mHeaderView = new XListViewHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView
.findViewById(R.id.xlistview_header_content);
mHeaderTimeView = (TextView) mHeaderView
.findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);// 把头部这个视图添加进去
// 初始化底部的View
mFooterView = new XListViewFooter(context);
// 初始化头部高度
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
}
@Override
public void setAdapter(ListAdapter adapter) {
// 确定XListViewFooter是最后底部的View, 并且只有一次
if (mIsFooterReady == false) {
mIsFooterReady = true;
addFooterView(mFooterView);
}
super.setAdapter(adapter);
}
/**
* 启用或禁用下拉刷新功能.
*
* @param enable
*/
public void setPullRefreshEnable(boolean enable) {
mEnablePullRefresh = enable;
if (!mEnablePullRefresh) { // 禁用,隐藏内容
mHeaderViewContent.setVisibility(View.INVISIBLE);// 如果为false则隐藏下拉刷新功能
} else {
mHeaderViewContent.setVisibility(View.VISIBLE);// 否则就显示下拉刷新功能
}
}
/**
* 启用或禁用加载更多的功能.
*
* @param enable
*/
public void setPullLoadEnable(boolean enable) {
mEnablePullLoad = enable;
if (!mEnablePullLoad) {
mFooterView.hide();// 隐藏
mFooterView.setOnClickListener(null);
} else {
mPullLoading = false;
mFooterView.show();// 显示
mFooterView.setState( XListViewFooter.STATE_NORMAL);
// both "上拉" 和 "点击" 将调用加载更多.
mFooterView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startLoadMore();
}
});
}
}
/**
* 停止刷新, 重置头视图.
*/
public void stopRefresh() {
if (mPullRefreshing == true) {
mPullRefreshing = false;
resetHeaderHeight();
}
}
/**
* stop load more, reset footer view.
*/
public void stopLoadMore() {
if (mPullLoading == true) {
mPullLoading = false;
mFooterView.setState( XListViewFooter.STATE_NORMAL);
}
}
/**
* 設置最後一次刷新時間
*
* @param time
*/
@SuppressLint("SimpleDateFormat")
public void setRefreshTime(String time) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());
//获取当前时间
String str = formatter.format(curDate);
mHeaderTimeView.setText(str);
}
private void invokeOnScrolling() {
if (mScrollListener instanceof OnXScrollListener) {
OnXScrollListener l = (OnXScrollListener) mScrollListener;
l.onXScrolling(this);
}
}
private void updateHeaderHeight(float delta) {
mHeaderView.setVisiableHeight((int) delta
+ mHeaderView.getVisiableHeight());
if (mEnablePullRefresh && !mPullRefreshing) { // 未处于刷新状态,更新箭头
if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mHeaderView.setState( XListViewHeader.STATE_READY);
} else {
mHeaderView.setState( XListViewHeader.STATE_NORMAL);
}
}
setSelection(0); // scroll to top each time
}
/**
* 重置头视图的高度
*/
private void resetHeaderHeight() {
int height = mHeaderView.getVisiableHeight();
if (height == 0) // 不显示.
return;
// 不显示刷新和标题的时候,什么都不显示
if (mPullRefreshing && height <= mHeaderViewHeight) {
return;
}
int finalHeight = 0; // 默认:滚动回头.
// 当滚动回显示所有头标题时候,刷新
if (mPullRefreshing && height > mHeaderViewHeight) {
finalHeight = mHeaderViewHeight;
}
mScrollBack = SCROLLBACK_HEADER;
mScroller.startScroll(0, height, 0, finalHeight - height,
SCROLL_DURATION);
// 触发刷新
invalidate();
}
// 改变底部视图高度
private void updateFooterHeight(float delta) {
int height = mFooterView.getBottomMargin() + (int) delta;
if (mEnablePullLoad && !mPullLoading) {
if (height > PULL_LOAD_MORE_DELTA) { // 高度足以调用加载更多
mFooterView.setState( XListViewFooter.STATE_READY);
} else {
mFooterView.setState( XListViewFooter.STATE_NORMAL);
}
}
mFooterView.setBottomMargin(height);
// setSelection(mTotalItemCount - 1); // scroll to bottom
}
private void resetFooterHeight() {
int bottomMargin = mFooterView.getBottomMargin();
if (bottomMargin > 0) {
mScrollBack = SCROLLBACK_FOOTER;
mScroller.startScroll(0, bottomMargin, 0, -bottomMargin,
SCROLL_DURATION);
invalidate();
}
}
// 开始加载更多
private void startLoadMore() {
mPullLoading = true;
mFooterView.setState( XListViewFooter.STATE_LOADING);
if (mListViewListener != null) {
mListViewListener.onLoadMore();
}
}
// 触发事件
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mLastY == -1) {
mLastY = ev.getRawY();
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastY = ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
final float deltaY = ev.getRawY() - mLastY;
mLastY = ev.getRawY();
System.out.println("数据监测:" + getFirstVisiblePosition() + "---->"
+ getLastVisiblePosition());
if (getFirstVisiblePosition() == 0
&& (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) {
// 第一项显示,标题显示或拉下来.
updateHeaderHeight(deltaY / OFFSET_RADIO);
invokeOnScrolling();
} else if (getLastVisiblePosition() == mTotalItemCount - 1
&& (mFooterView.getBottomMargin() > 0 || deltaY < 0)) {
// 最后一页,已停止或者想拉起
updateFooterHeight(-deltaY / OFFSET_RADIO);
}
break;
default:
mLastY = -1; // 重置
if (getFirstVisiblePosition() == 0) {
// 调用刷新,如果头部视图高度大于设定高度。
if (mEnablePullRefresh
&& mHeaderView.getVisiableHeight() > mHeaderViewHeight) {
mPullRefreshing = true;// 那么刷新
mHeaderView.setState( XListViewHeader.STATE_REFRESHING);
if (mListViewListener != null) {
mListViewListener.onRefresh();
}
}
resetHeaderHeight();// 刷新完毕,重置头部高度,也就是返回上不
}
if (getLastVisiblePosition() == mTotalItemCount - 1) {
// 调用加载更多.
if (mEnablePullLoad
&& mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
startLoadMore();// 如果底部视图高度大于可以加载高度,那么就开始加载
}
resetFooterHeight();// 重置加载更多视图高度
}
break;
}
return super.onTouchEvent(ev);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
if (mScrollBack == SCROLLBACK_HEADER) {
mHeaderView.setVisiableHeight(mScroller.getCurrY());
} else {
mFooterView.setBottomMargin(mScroller.getCurrY());
}
postInvalidate();
invokeOnScrolling();
}
super.computeScroll();
}
@Override
public void setOnScrollListener(OnScrollListener l) {
mScrollListener = l;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// 发送到用户的监听器
mTotalItemCount = totalItemCount;
if (mScrollListener != null) {
mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
public void setXListViewListener(IXListViewListener l) {
mListViewListener = l;
}
/**
* 你可以监听到列表视图,OnScrollListener 或者这个. 他将会被调用 , 当头部或底部触发的时候
*/
public interface OnXScrollListener extends OnScrollListener {
public void onXScrolling(View view);
}
/**
* 实现这个接口来刷新/负载更多的事件
*/
public interface IXListViewListener {
public void onRefresh();
public void onLoadMore();
}
}
/ XListViewHeader插件代码
package test.bwei.com.zjl20171021.xListview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import test.bwei.com.zjl20171021.R;
/**
* Created by Administrator on 2017/10/21 0021.
*/
public class XListViewHeader extends LinearLayout {
private LinearLayout mContainer;
private ImageView mArrowImageView;
private ProgressBar mProgressBar;
private TextView mHintTextView;
private int mState = STATE_NORMAL;// 初始状态
private Animation mRotateUpAnim;
private Animation mRotateDownAnim;
private final int ROTATE_ANIM_DURATION = 180;
public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_REFRESHING = 2;
public XListViewHeader(Context context) {
super(context);
initView(context);
}
/**
* @param context
* @param attrs
*/
public XListViewHeader(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
// 初始情况,设置下拉刷新view高度为0
LayoutParams lp = new LayoutParams(
LayoutParams.FILL_PARENT, 0);
// 时间TextView
mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.xlistview_header, null);
addView(mContainer, lp);
setGravity(Gravity.BOTTOM);
// 找到头部页面里的控件
mArrowImageView = (ImageView) findViewById(R.id.xlistview_header_arrow);
mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
mProgressBar = (ProgressBar) findViewById(R.id.xlistview_header_progressbar);
mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateUpAnim.setFillAfter(true);
mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateDownAnim.setFillAfter(true);
}
// 设置状态
public void setState(int state) {
if (state == mState)
return;
if (state == STATE_REFRESHING) { // 显示进度
mArrowImageView.clearAnimation();
mArrowImageView.setVisibility(View.INVISIBLE);// 不显示图片
mProgressBar.setVisibility(View.VISIBLE);// 显示进度条
} else { // 显示箭头图片
mArrowImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
}
switch (state) {
case STATE_NORMAL:
if (mState == STATE_READY) {// 当状态时准备的时候,显示动画
mArrowImageView.startAnimation(mRotateDownAnim);
}
if (mState == STATE_REFRESHING) {// 当状态显示进度条的时候,清除动画
mArrowImageView.clearAnimation();
}
mHintTextView.setText(R.string.xlistview_header_hint_normal);// 文字提示:下拉刷新
break;
case STATE_READY:
if (mState != STATE_READY) {
mArrowImageView.clearAnimation();
mArrowImageView.startAnimation(mRotateUpAnim);
mHintTextView.setText(R.string.xlistview_header_hint_ready);// 松开刷新数据
}
break;
case STATE_REFRESHING:
mHintTextView.setText(R.string.xlistview_header_hint_loading);
break;
default:
}
mState = state;
}
public void setVisiableHeight(int height) {
if (height < 0)
height = 0;
LayoutParams lp = (LayoutParams) mContainer
.getLayoutParams();
lp.height = height;
mContainer.setLayoutParams(lp);
}
public int getVisiableHeight() {
return mContainer.getHeight();
}
}