TabletLayout 数据库
作者:互联网
Main_activity
<?xml version="1.0" encoding="utf-8"?><FrameLayout
android:id="@+id/fragment_frage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/shou"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="首页" />
<RadioButton
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="视频" />
<RadioButton
android:id="@+id/my"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="我的" />
</RadioGroup>
***TableLayout***
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_frag01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></android.support.v4.view.ViewPager>
***PullToRefresh***
<?xml version="1.0" encoding="utf-8"?>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
***listView***
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView01_tabfrag01_list"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_background" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView01_tabfrag01_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView02_tabfrag01_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
***主页面Main***
package com.example.zhou3;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private Frag_01 frag_01;
private Frag_02 frag_02;
private Frag_03 frag_03;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
radioGroup = findViewById(R.id.radioGroup);
//获取fragment
frag_01 = new Frag_01();
frag_02 = new Frag_02();
frag_03 = new Frag_03();
//获取管理器
manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_frage, frag_01);
transaction.add(R.id.fragment_frage, frag_02);
transaction.add(R.id.fragment_frage, frag_03);
transaction.show(frag_01).hide(frag_02).hide(frag_03);
transaction.commit();
//radioGroup监听
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction fragmentTransaction = manager.beginTransaction();
switch (checkedId) {
case R.id.shou:
fragmentTransaction.show(frag_01).hide(frag_02).hide(frag_03);
break;
case R.id.video:
fragmentTransaction.show(frag_02).hide(frag_01).hide(frag_03);
break;
case R.id.my:
fragmentTransaction.show(frag_03).hide(frag_02).hide(frag_01);
break;
}
fragmentTransaction.commit();
}
});
}
}
TableLayut页面
package com.example.zhou3;
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.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.xlibrary.utils.NetWordUtils;
import com.example.zhou3.bean.Goods;
import com.example.zhou3.dao.JsonDao;
import com.example.zhou3.lian.NetUtil;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import java.util.ArrayList;
import java.util.List;
public class Frag01_item extends Fragment {
private PullToRefreshListView plv;
private JsonDao dao;
private MyPullAdapter pullAdapter;
ArrayList<Goods.DataBean> list = new ArrayList<>();
String jsonUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private int page = 0;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_item01, container, false);
//找控件
plv = view.findViewById(R.id.plv);
plv.setMode(PullToRefreshBase.Mode.BOTH);
//创建dao层
dao = new JsonDao(getActivity());
//创建适配器
pullAdapter = new MyPullAdapter();
plv.setAdapter(pullAdapter);
initData(page);
//上下拉刷新
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
list.clear();
initData(0);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
initData(page);
}
});
return view;
}
private void initData(int page) {
String murl = jsonUrl + page;
if (NetUtil.isconn(getActivity())) {
//创建MyAsnc
new MyAsyTask().execute(murl);
} else {
//无网情况
Toast.makeText(getActivity(), "当前没有网络", Toast.LENGTH_SHORT).show();
//数据哭进项查询
String select = dao.selete(murl);
if (!select.isEmpty()) {
//json
Gson gson = new Gson();
Goods fromJson = gson.fromJson(select, Goods.class);
List<Goods.DataBean> data = fromJson.getData();
list.addAll(data);
pullAdapter.notifyDataSetChanged();//刷新
plv.onRefreshComplete();
}
}
}
public class MyAsyTask extends AsyncTask<String, Void, String> {
String name = "";
@Override
protected String doInBackground(String... strings) {
name = strings[0];
return NetWordUtils.getNetjson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//代用dao层
dao.inserts(s, name);
//json
Gson gson = new Gson();
Goods fromJson = gson.fromJson(s, Goods.class);
List<Goods.DataBean> data = fromJson.getData();
list.addAll(data);
pullAdapter.notifyDataSetChanged();//刷新
plv.onRefreshComplete();//
}
}
public class MyPullAdapter 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) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(getActivity(), R.layout.frag02_itemlist, null);
holder.textView1 = convertView.findViewById(R.id.textView01_tabfrag01_list);
holder.getTextView2 = convertView.findViewById(R.id.textView02_tabfrag01_list);
holder.imageView = convertView.findViewById(R.id.imageView01_tabfrag01_list);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(list.get(position).getNews_title());
holder.getTextView2.setText(list.get(position).getNews_summary());
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(), holder.imageView);
return convertView;
}
class ViewHolder {
TextView textView1, getTextView2;
ImageView imageView;
}
}
}
package com.example.zhou3;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
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.ArrayAdapter;
import android.widget.BaseAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
public class Frag_01 extends Fragment {
private ArrayList titles = new ArrayList();
private PullToRefreshListView plv;
private Frag01_item frag01_item;
private Frag02_item frag02_item;
private Frag03_item frag03_item;
private ArrayList arrayList;
private TabLayout tableLayout;
private ViewPager viewPager;
private MyAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_01, container, false);
//获取控件
tableLayout = view.findViewById(R.id.tableLayout);
viewPager = view.findViewById(R.id.viewPager_frag01);
//创建头部数据
for (int i = 0; i < 3; i++) {
titles.add("首页" + i);
}
//获取fragment
frag01_item = new Frag01_item();
frag02_item = new Frag02_item();
frag03_item = new Frag03_item();
arrayList = new ArrayList<>();
arrayList.add(frag01_item);
arrayList.add(frag02_item);
arrayList.add(frag03_item);
//viewPager适配器
adapter = new MyAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(adapter);
//设置tabLayout显示模式
tableLayout.setTabMode(TabLayout.MODE_FIXED);
//让tabLayout和viewPager关联
tableLayout.setupWithViewPager(viewPager);
return view;
}
public class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return arrayList.get(i);
}
@Override
public int getCount() {
return arrayList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
}
JsonDao
package com.example.zhou3.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class JsonDao {
private final MSQLiteopen msqLiteopen;
public JsonDao(Context context) {
msqLiteopen = new MSQLiteopen(context);
}
public void inserts(String s, String name) {
//先删除在插入
SQLiteDatabase db = msqLiteopen.getWritableDatabase();
//先删除,再插入
db.delete("t_pulls", "urls=?", new String[]{name});
//插入时必用对象
ContentValues values = new ContentValues();
values.put("jsons", s);
values.put("urls", name);
//插入
db.insert("t_pulls", null, values);
}
public String selete(String jsonUrl) {
String querys = "";
SQLiteDatabase db = msqLiteopen.getWritableDatabase();
Cursor cursor = db.query("t_pulls", null, "urls=?", new String[]{jsonUrl}, null, null, null);
while (cursor.moveToNext()) {
querys = cursor.getString(cursor.getColumnIndex("jsons"));
}
return querys;
}
}
创建数据库表
package com.example.zhou3.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MSQLiteopen extends SQLiteOpenHelper {
public MSQLiteopen(Context context) {
super(context, "pull.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table t_pulls(ids integer primary key autoincrement,jsons text,urls text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
图片
package com.example.zhou3;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MyAppliction extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration loaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.build();
//初始化
ImageLoader.getInstance().init(loaderConfiguration);
}
}
标签:frag,数据库,TabletLayout,new,import,android,com,public 来源: https://blog.csdn.net/qq_42805722/article/details/84172992