我们来看看我们的fragment的布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/key1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="科目一"
android:layout_weight="1"
/>
<Button
android:id="@+id/key2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="科目二"
android:layout_weight="1"
/>
<Button
android:id="@+id/key3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="科目三"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/root_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/clean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清楚缓存"
/>
</LinearLayout>
</LinearLayout>
下面就是我们的MainActivity里的内容
package com.example.lianxi2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private FragmentManager fm;
private FragmentTransaction ft;
private Fragment1 f1;
private Fragment2 f2;
private Fragment3 f3;
private Button b1,b2,b3,clean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
b1 = (Button)findViewById(R.id.key1);
b2 = (Button)findViewById(R.id.key2);
b3 = (Button)findViewById(R.id.key3);
clean = (Button)findViewById(R.id.clean);
f1 = new Fragment1();
f2 = new Fragment2();
f3 = new Fragment3();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.add(R.id.root_fragment, f1);
ft.add(R.id.root_fragment, f2);
ft.add(R.id.root_fragment, f3);
ft.hide(f2);
ft.hide(f3);
ft.commit();
b1.setOnClickListener(l);
b2.setOnClickListener(l);
b3.setOnClickListener(l);
clean.setOnClickListener(l);
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction fn = fm.beginTransaction();
switch (v.getId()) {
case R.id.key1:
fn.hide(f2);
fn.hide(f3);
fn.show(f1);
break;
case R.id.key2:
fn.hide(f1);
fn.hide(f3);
fn.show(f2);
break;
case R.id.key3:
fn.hide(f2);
fn.hide(f1);
fn.show(f3);
break;
case R.id.clean:
DataClenManager.cleanInternalCache(MainActivity.this);
DataClenManager.cleanDatabases(MainActivity.this);
Toast.makeText(MainActivity.this, "清楚成了", 2000).show();
break;
}
fn.commit();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我们在上面创建了3个fragment,接下来我们要做的是把缓存清除在DataClenManager内容里s
package com.example.lianxi2;
import java.io.File;
import android.content.Context;
import android.os.Environment;
public class DataClenManager {
/** * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context */
public static void cleanInternalCache(Context context) {
deleteFilesByDirectory(context.getCacheDir());
}
/** * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context */
public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/databases"));
}
/**
* * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
* context
*/
public static void cleanSharedPreference(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/shared_prefs"));
}
/** * 按名字清除本应用数据库 * * @param context * @param dbName */
public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}
/** * 清除/data/data/com.xxx.xxx/files下的内容 * * @param context */
public static void cleanFiles(Context context) {
deleteFilesByDirectory(context.getFilesDir());
}
/**
* * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
* context
*/
public static void cleanExternalCache(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteFilesByDirectory(context.getExternalCacheDir());
}
}
/** * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * @param filePath */
public static void cleanCustomCache(String filePath) {
deleteFilesByDirectory(new File(filePath));
}
/** * 清除本应用所有的数据 * * @param context * @param filepath */
public static void cleanApplicationData(Context context, String... filepath) {
cleanInternalCache(context);
cleanExternalCache(context);
cleanDatabases(context);
cleanSharedPreference(context);
cleanFiles(context);
for (String filePath : filepath) {
cleanCustomCache(filePath);
}
}
/** * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * @param directory */
private static void deleteFilesByDirectory(File directory) {
if (directory != null && directory.exists() && directory.isDirectory()) {
for (File item : directory.listFiles()) {
item.delete();
}
}
}
}
接着我们就把bean包写了
1
package com.example.bean;
public class Yi {
private int song;
private String category_id;
private String lesson_id;
private String lesson_images;
private String lesson_number;
private String lesson_title;
private String user_name;
public int getSong() {
return song;
}
public void setSong(int song) {
this.song = song;
}
public String getCategory_id() {
return category_id;
}
public void setCategory_id(String category_id) {
this.category_id = category_id;
}
public String getLesson_id() {
return lesson_id;
}
public void setLesson_id(String lesson_id) {
this.lesson_id = lesson_id;
}
public String getLesson_images() {
return lesson_images;
}
public void setLesson_images(String lesson_images) {
this.lesson_images = lesson_images;
}
public String getLesson_number() {
return lesson_number;
}
public void setLesson_number(String lesson_number) {
this.lesson_number = lesson_number;
}
public String getLesson_title() {
return lesson_title;
}
public void setLesson_title(String lesson_title) {
this.lesson_title = lesson_title;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Yi(String category_id, String lesson_id,
String lesson_images, String lesson_number, String lesson_title,
String user_name) {
super();
this.song = song;
this.category_id = category_id;
this.lesson_id = lesson_id;
this.lesson_images = lesson_images;
this.lesson_number = lesson_number;
this.lesson_title = lesson_title;
this.user_name = user_name;
}
@Override
public String toString() {
return "Yi [song=" + song + ", category_id=" + category_id
+ ", lesson_id=" + lesson_id + ", lesson_images="
+ lesson_images + ", lesson_number=" + lesson_number
+ ", lesson_title=" + lesson_title + ", user_name=" + user_name
+ "]";
}
}
2
package com.example.bean;
public class Yi {
private int song;
private String category_id;
private String lesson_id;
private String lesson_images;
private String lesson_number;
private String lesson_title;
private String user_name;
public int getSong() {
return song;
}
public void setSong(int song) {
this.song = song;
}
public String getCategory_id() {
return category_id;
}
public void setCategory_id(String category_id) {
this.category_id = category_id;
}
public String getLesson_id() {
return lesson_id;
}
public void setLesson_id(String lesson_id) {
this.lesson_id = lesson_id;
}
public String getLesson_images() {
return lesson_images;
}
public void setLesson_images(String lesson_images) {
this.lesson_images = lesson_images;
}
public String getLesson_number() {
return lesson_number;
}
public void setLesson_number(String lesson_number) {
this.lesson_number = lesson_number;
}
public String getLesson_title() {
return lesson_title;
}
public void setLesson_title(String lesson_title) {
this.lesson_title = lesson_title;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Yi(String category_id, String lesson_id,
String lesson_images, String lesson_number, String lesson_title,
String user_name) {
super();
this.song = song;
this.category_id = category_id;
this.lesson_id = lesson_id;
this.lesson_images = lesson_images;
this.lesson_number = lesson_number;
this.lesson_title = lesson_title;
this.user_name = user_name;
}
@Override
public String toString() {
return "Yi [song=" + song + ", category_id=" + category_id
+ ", lesson_id=" + lesson_id + ", lesson_images="
+ lesson_images + ", lesson_number=" + lesson_number
+ ", lesson_title=" + lesson_title + ", user_name=" + user_name
+ "]";
}
}
3
package com.example.bean;
public class Yi {
private int song;
private String category_id;
private String lesson_id;
private String lesson_images;
private String lesson_number;
private String lesson_title;
private String user_name;
public int getSong() {
return song;
}
public void setSong(int song) {
this.song = song;
}
public String getCategory_id() {
return category_id;
}
public void setCategory_id(String category_id) {
this.category_id = category_id;
}
public String getLesson_id() {
return lesson_id;
}
public void setLesson_id(String lesson_id) {
this.lesson_id = lesson_id;
}
public String getLesson_images() {
return lesson_images;
}
public void setLesson_images(String lesson_images) {
this.lesson_images = lesson_images;
}
public String getLesson_number() {
return lesson_number;
}
public void setLesson_number(String lesson_number) {
this.lesson_number = lesson_number;
}
public String getLesson_title() {
return lesson_title;
}
public void setLesson_title(String lesson_title) {
this.lesson_title = lesson_title;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Yi(String category_id, String lesson_id,
String lesson_images, String lesson_number, String lesson_title,
String user_name) {
super();
this.song = song;
this.category_id = category_id;
this.lesson_id = lesson_id;
this.lesson_images = lesson_images;
this.lesson_number = lesson_number;
this.lesson_title = lesson_title;
this.user_name = user_name;
}
@Override
public String toString() {
return "Yi [song=" + song + ", category_id=" + category_id
+ ", lesson_id=" + lesson_id + ", lesson_images="
+ lesson_images + ", lesson_number=" + lesson_number
+ ", lesson_title=" + lesson_title + ", user_name=" + user_name
+ "]";
}
}
接着我们把fragment完整起来
package com.example.lianxi2;
import java.util.List;
import com.example.adapter.Myadapter;
import com.example.bean.San;
import com.example.bean.Yi;
import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
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.ListView;
public class Fragment1 extends Fragment{
private ListView lv;
private List<Yi> list;
private Myadapter ada;
private San sa;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag1, null);
lv = (ListView)view.findViewById(R.id.lw1);
httpget();
return view;
}
private void httpget() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
HttpUtils http = new HttpUtils();
String URLS = "http://172.22.37.80:8080/1402c/kebiao.json";
http.configTimeout(5*1000);
http.send(HttpMethod.GET, URLS,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
Gson gs = new Gson();
sa = gs.fromJson(arg0.result, San.class);
Log.i("aaa", "--------------------------"+sa.toString());
list = sa.getData().get(0).getMore();
ada= new Myadapter(getActivity(), list);
lv.setAdapter(ada);
//添加数据库
// add();
}
private void add() {
// TODO Auto-generated method stub
DbUtils db = DbUtils.create(getActivity());
for(int i=0;i<list.size();i++){
Yi more = new Yi(list.get(i).getCategory_id(),list.get(i).getLesson_id(),
list.get(i).getLesson_images(),list.get(i).getLesson_number(),
list.get(i).getLesson_title(),list.get(i).getUser_name());
try {
db.save(more);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
好了,自己如果fragment多的话,大多数和上面这个雷同