侧拉的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab"
></android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@color/black"
app:tabSelectedTextColor="@color/red"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabGravity="fill"
android:layout_alignParentBottom="true"
></android.support.design.widget.TabLayout>
</RelativeLayout>
<FrameLayout
android:id="@+id/frame"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ccc"
></FrameLayout>
</android.support.v4.widget.DrawerLayout>
导航的适配器
public class MyFragAdapter extends FragmentPagerAdapter {
String[] str=new String[]{"首页","视频","我的"};
public MyFragAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
@Override
public int getCount() {
return str.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return str[position];
}
}
侧拉的代码
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private FrameLayout frameLayout;
private ActionBar actionBar;
private ActionBarDrawerToggle toggle;
private ViewPager viewPager;
private TabLayout tabLayout;
private MyFragAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
drawerLayout = findViewById(R.id.drawer);
frameLayout=findViewById(R.id.frame);
viewPager=findViewById(R.id.viewpager);
tabLayout=findViewById(R.id.tab);
//加载布局
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction()
.add(R.id.frame,new LeftFragment())
.commit();
}
actionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
toggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.kai,R.string.guan);
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
//切换
viewPager.setAdapter(new MyFragAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
}
public void getIntent(int position){
viewPager.setCurrentItem(position);
drawerLayout.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
侧拉的列表布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="26dp"
android:layout_marginEnd="68dp"
android:background="@mipmap/ic_launcher"
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="210dp"
></ListView>
</RelativeLayout>
侧拉的适配器
public class MyBase extends BaseAdapter {
private List<String> list;
private Context context;
public MyBase(List<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public String 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;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
holder=new ViewHolder();
holder.textView=convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.textView.setText(getItem(position));
return convertView;
}
class ViewHolder{
TextView textView;
}
}
ViewPager+TabLayout布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.TabLayout
android:id="@+id/top_tab"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:tabTextColor="@color/black"
app:tabSelectedTextColor="@color/red"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_tab"
></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
ViewPager+TabLayout布局适配器
public class MoveAdapter extends FragmentPagerAdapter {
private String[] name=new String[]{"正在上映","即将上映"};
private String[] urls=new String[]{"http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=1",
"http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=1"
};
public MoveAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
default:
Bundle bundle=new Bundle();
bundle.putString("url",urls[position]);
Fragment_do fragment_do=new Fragment_do();
fragment_do.setArguments(bundle);
return fragment_do;
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return name[position];
}
@Override
public int getCount() {
return name.length;
}
}
public class FragmentTwo extends Fragment {
private TabLayout top_tab;
private ViewPager view_Pager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.two,container,false);
top_tab = view.findViewById(R.id.top_tab);
view_Pager = view.findViewById(R.id.view_pager);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
view_Pager.setAdapter(new MoveAdapter(getChildFragmentManager()));
top_tab.setupWithViewPager(view_Pager);
}
}
展示的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_movie"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
></com.handmark.pulltorefresh.library.PullToRefreshListView>
</android.support.constraint.ConstraintLayout>
展示数据的适配器
public class MyMovieAdapter extends BaseAdapter {
private List<NewsBean.ResultBean> list;
private Context context;
public MyMovieAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<NewsBean.ResultBean> list) {
this.list .clear();
this.list.addAll(list);
notifyDataSetChanged();
}
public void addList(List<NewsBean.ResultBean> list) {
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public NewsBean.ResultBean 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) {
ViewHolder1 holder1;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.movieitem,parent,false);
holder1=new ViewHolder1();
holder1.imageView_url=convertView.findViewById(R.id.imageView_url);
holder1.textView_name=convertView.findViewById(R.id.textView_name);
holder1.textView_sum=convertView.findViewById(R.id.textView_sum);
convertView.setTag(holder1);
}else {
holder1= (ViewHolder1) convertView.getTag();
}
holder1.textView_name.setText(getItem(position).getName());
holder1.textView_sum.setText(getItem(position).getSummary());
ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),holder1.imageView_url);
return convertView;
}
class ViewHolder1{
ImageView imageView_url;
TextView textView_sum,textView_name;
}
}
class MovieFragment extends BaseFragment{
private PullToRefreshListView listView;
private MyAdapter adapter;
private int mPage;
private String url;
private final int COUNTTYPE=11;
private NewsDao dao;
@Override
protected int getViewId() {
return R.layout.movie;
}
@Override
protected void initData() {
mPage=1;
dao=new NewsDao(getActivity());
url = getArguments().getString("url");
adapter=new MyAdapter(getActivity());
listView.setAdapter(adapter);
listView.setMode(PullToRefreshListView.Mode.BOTH);
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
mPage=1;
getInfo();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
getInfo();
}
});
getInfo();
}
public void getInfo() {
Method.getInstance().getRequest(url, NewBean.class, new Method.CallBack<NewBean>() {
@Override
public void onSuccess(NewBean o) {
if(o==null || !o.isSuccess()){
Toast.makeText(getActivity(),"没有数据",Toast.LENGTH_LONG).show();
listView.onRefreshComplete();
return;
}
List<NewBean.ResultBean> result = o.getResult();
if(mPage==1){
dao.del(url.hashCode());
}
for (int i=0;i<result.size();i++){
String name = result.get(i).getName();
String summary = result.get(i).getSummary();
String imageUrl = result.get(i).getImageUrl();
dao.add(name,summary,imageUrl,url.hashCode());
}
List<NewBean.ResultBean> select = dao.select(url.hashCode());
if(mPage==1){
adapter.setList(select);
}else {
adapter.addList(select);
}
mPage++;
listView.onRefreshComplete();
if(result.size()<COUNTTYPE){
listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
}else {
listView.setMode(PullToRefreshBase.Mode.BOTH);
}
}
});
}
@Override
protected void initView(View view) {
listView = view.findViewById(R.id.pull_listview);
}
}
数据库
public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(@Nullable Context context) {
super(context, "News.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table news(id integer primary key autoincrement," +
"name text," +
"summary text," +
"imageUrl text," +
"type integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Dao层
public class NewsDao {
private SqliteHelper helper;
private SQLiteDatabase database;
public NewsDao(Context context){
helper=new SqliteHelper(context);
database=helper.getReadableDatabase();
}
//添加
public void add(String name,String summary,String imageUrl,int type){
ContentValues values=new ContentValues();
values.put("name",name);
values.put("summary",summary);
values.put("imageUrl",imageUrl);
values.put("type",type);
database.insert("news",null,values);
}
//查询
public List<NewBean.ResultBean> select(int type){
List<NewBean.ResultBean> list=new ArrayList<NewBean.ResultBean>();
Cursor query = database.query("news", null, "type=?", new String[]{String.valueOf(type)}, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String summary = query.getString(query.getColumnIndex("summary"));
String imageUrl = query.getString(query.getColumnIndex("imageUrl"));
NewBean.ResultBean bean=new NewBean.ResultBean(name,summary,imageUrl);
list.add(bean);
}
return list;
}
//删除
public void del(int type){
database.delete("news","type=?",new String[]{String.valueOf(type)});
}
}
工具类
public class Method {
private static Method instance;
public static Method getInstance(){
if(instance==null){
instance=new Method();
}
return instance;
}
public interface CallBack<T>{
void onSuccess(T t);
}
public void getRequest(final String path, final Class clazz, final CallBack callBack){
new AsyncTask<String, Void, Object>() {
@Override
protected Object doInBackground(String... strings) {
return getRequest(path,clazz);
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
callBack.onSuccess(o);
}
}.execute(path);
}
public <E> E getRequest(String path,Class clazz){
String request = getRequest(path);
Gson gson=new Gson();
E e= (E) gson.fromJson(request,clazz);
return e;
}
public String getRequest(String path){
String result="";
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
if(responseCode==200){
InputStream inputStream = connection.getInputStream();
result=stream(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String stream(InputStream is) throws IOException {
StringBuilder builder=new StringBuilder();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
for (String tem=br.readLine();tem!=null;tem=br.readLine()) {
builder.append(tem);
}
return builder.toString();
}
}