推荐条+fragment
Posted thatme
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了推荐条+fragment相关的知识,希望对你有一定的参考价值。
主布局
package com.example.dell.day1215;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.dell.day1215.adapter.MyAdapter;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tab;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
//添加适配器
adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tab.setupWithViewPager(viewPager);
}
private void initView() {
//寻找资源id
viewPager = findViewById(R.id.viewpager);
tab = findViewById(R.id.tab);
}
}
适配器
开始适配器
package com.example.dell.day1215.adapter;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.dell.day1215.fragment.AFragment;
import com.example.dell.day1215.fragment.BFragment;
public class MyAdapter extends FragmentPagerAdapter {
private String[] menues=new String[]{
"电影","资讯","视频","我的"
};
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return menues[position];
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new AFragment();
case 1:
return new BFragment();
case 2:
return new BFragment();
default:
return new BFragment();
}
}
@Override
public int getCount() {
return menues.length;
}
}
子适配器
package com.example.dell.day1215.adapter;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.dell.day1215.fragment.aAFragment;
import com.example.dell.day1215.fragment.aBFragment;
public class aMyAdapter extends FragmentPagerAdapter {
//定义集合
private String[] menu=new String[]{
"正在上映","即将上映"
};
public aMyAdapter(FragmentManager fm) {
super(fm);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return menu[position];
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0:
return new aAFragment();
case 1:
return new aBFragment();
}
return null;
}
@Override
public int getCount() {
return menu.length;
}
}
list加载适配器
package com.example.dell.day1215.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.dell.day1215.Bean.MovieBean;
import com.example.dell.day1215.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class LISTVIEWBaseAdapter extends BaseAdapter {
private Context context;
private List<MovieBean.ResultBean> list;
public LISTVIEWBaseAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
//更新数据
public void setList(List<MovieBean.ResultBean> list1) {
list.clear();
if (list1!=null){
this.list = list1;
}
notifyDataSetChanged();
}
//追加数据
public void addData(List<MovieBean.ResultBean> d){
if (d!=null){
list.addAll(d);
}
notifyDataSetChanged();
}
@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;
if (convertView==null){
holder = new ViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.list,parent,false);
holder.img=convertView.findViewById(R.id.img);
holder.text=convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(list.get(position).getName());
ImageLoader.getInstance().displayImage(list.get(position).getImageUrl(),holder.img);
return convertView;
}
class ViewHolder{
ImageView img;
TextView text;
}
}
list2适配器
package com.example.dell.day1215.adapter;
import android.content.Context;
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.example.dell.day1215.Bean.MovieBean;
import com.example.dell.day1215.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class LISTVIEWBaseAdapter2 extends BaseAdapter {
private Context context;
private List<MovieBean.ResultBean> blist;
public LISTVIEWBaseAdapter2(Context context) {
this.context = context;
blist=new ArrayList<>();
}
public void setBlist(List<MovieBean.ResultBean> blist1) {
blist.clear();
if (blist1!=null){
this.blist = blist;
}
notifyDataSetChanged();
}
public void addDatab(List<MovieBean.ResultBean> n){
if (n!=null){
blist.addAll(n);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return blist.size();
}
@Override
public Object getItem(int position) {
return blist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder2 holder;
if (convertView==null){
holder = new LISTVIEWBaseAdapter2.ViewHolder2();
convertView=LayoutInflater.from(context).inflate(R.layout.list,parent,false);
holder.img=convertView.findViewById(R.id.img);
holder.text=convertView.findViewById(R.id.text);
convertView.setTag(holder);
}else{
holder = (LISTVIEWBaseAdapter2.ViewHolder2) convertView.getTag();
}
holder.text.setText(blist.get(position).getName());
ImageLoader.getInstance().displayImage(blist.get(position).getImageUrl(),holder.img);
return convertView;
}
class ViewHolder2{
ImageView img;
TextView text;
}
}
Bean类
package com.example.dell.day1215.Bean;
import java.util.List;
public class MovieBean {
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
private boolean followMovie;
private int id;
private String imageUrl;
private String name;
private int rank;
private String summary;
public boolean isFollowMovie() {
return followMovie;
}
public void setFollowMovie(boolean followMovie) {
this.followMovie = followMovie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
}
fragment
package com.example.dell.day1215.fragment;
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.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.dell.day1215.R;
import com.example.dell.day1215.adapter.aMyAdapter;
public class AFragment extends Fragment {
private TabLayout atab;
private ViewPager aviewpager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
atab = view.findViewById(R.id.atab);
aviewpager = view.findViewById(R.id.aviewpager);
//设置适配器
aMyAdapter adapter = new aMyAdapter(getChildFragmentManager());
aviewpager.setAdapter(adapter);
atab.setupWithViewPager(aviewpager);
}
}
fragment1
package com.example.dell.day1215.fragment;
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 com.example.dell.day1215.R;
public class BFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
子fragment
package com.example.dell.day1215.fragment;
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.ListView;
import android.widget.Toast;
import com.example.dell.day1215.Bean.MovieBean;
import com.example.dell.day1215.R;
import com.example.dell.day1215.adapter.LISTVIEWBaseAdapter;
import com.example.dell.day1215.util.NewUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class aAFragment extends Fragment {
private PullToRefreshListView pull;
private LISTVIEWBaseAdapter adapter;
private String uri="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=";
private int page=1;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.afragment1, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pull = view.findViewById(R.id.pull);
//添加适配器
adapter = new LISTVIEWBaseAdapter(getContext());
pull.setAdapter(adapter);
pull.setMode(PullToRefreshBase.Mode.BOTH);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //下载
page=1;
//初始化数据
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //刷新
initData();
page++;
}
});
initData();
}
private void initData() {
NewUtil.requestData(uri + page, MovieBean.class, new NewUtil.CallBack<MovieBean>() {
@Override
public void onsuccess(MovieBean nowBean) {
if (nowBean.getResult().size()==0){
pull.onRefreshComplete();
Toast.makeText(getActivity(),"没有数据",Toast.LENGTH_SHORT).show();
}
if (page==1){ //更新
adapter.setList(nowBean.getResult());
}else{ //追加
adapter.addData(nowBean.getResult());
}
page++;
pull.onRefreshComplete();
}
});
}
}
子2布局
package com.example.dell.day1215.fragment;
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.ListView;
import android.widget.Toast;
import com.example.dell.day1215.Bean.MovieBean;
import com.example.dell.day1215.R;
import com.example.dell.day1215.adapter.LISTVIEWBaseAdapter2;
import com.example.dell.day1215.util.NewUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class aBFragment extends Fragment {
private PullToRefreshListView pull;
private String uri="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=";
private int page=1;
private LISTVIEWBaseAdapter2 adapter2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.afragment1, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pull = view.findViewById(R.id.pull);
//添加适配器
adapter2 = new LISTVIEWBaseAdapter2(getContext());
pull.setAdapter(adapter2);
pull.setMode(PullToRefreshBase.Mode.BOTH);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //下载
page=1;
//初始化数据
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //刷新
initData();
page++;
}
});
initData();
}
private void initData() {
NewUtil.requestData(uri + page, MovieBean.class, new NewUtil.CallBack<MovieBean>() {
@Override
public void onsuccess(MovieBean downBean) {
if (downBean.getResult().size()==0){
pull.onRefreshComplete();
Toast.makeText(getActivity(),"没有数据",Toast.LENGTH_SHORT).show();
}
if (page==1){ //更新
adapter2.setBlist(downBean.getResult());
}else{ //追加
adapter2.addDatab(downBean.getResult());
}
page++;
pull.onRefreshComplete();
}
});
}
}
sqi类
package com.example.dell.day1215.sqilite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context) {
super(context,"User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table data(id integer primary key autoincrement," +
"title text," +
"summary text," +
"image_url text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
util
package com.example.dell.day1215.util;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
public class NewUtil {
public interface CallBack<T>{
void onsuccess(T t);
}
@SuppressLint("StaticFieldLeak")
public static void requestData(String e, final Class calzz, final CallBack callBack){
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return requestData(strings[0],calzz);
}
@Override
protected void onPostExecute(Object o) {
callBack.onsuccess(o);
}
}.execute(e);
}
private static <T> T requestData(String string, Class calzz) {
return(T) new Gson().fromJson(requestData(string),calzz);
}
private static String requestData(String string) {
try {
URL url = new URL(string);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
String result=stream2String(urlConnection.getInputStream());
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String stream2String(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp=br.readLine();tmp!=null;tmp=br.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
}
APP设置图片
package com.example.dell.day1215;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration loaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.memoryCacheSize(10)
.diskCacheSize(10 * 1024 * 1024)
.defaultDisplayImageOptions(new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.cacheOnDisk(true)
.cacheInMemory(true)
.build()
).build();
ImageLoader.getInstance().init(loaderConfiguration);
}
}
抽取类
package com.example.dell.day1215;
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
initView();
}
protected abstract void initView();
protected abstract void initData();
}
布局viewPager+Fragment+侧拉
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_weight="1"
android:background="#ff00"
android:layout_width="match_parent"
android:layout_height="700dp">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
app:tabMode="fixed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff99"
></android.support.design.widget.TabLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/cela"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#f0f0">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text01"
android:layout_weight="1"
android:textSize="30dp"
android:text="设置头像"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text02"
android:layout_weight="1"
android:textSize="30dp"
android:text="放大电路"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text03"
android:layout_weight="1"
android:textSize="30dp"
android:text="公开规范"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
TabLayout+ViewPager
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.TabLayout
android:id="@+id/atab"
app:tabMode="fixed"
android:background="#0f0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/aviewpager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#f679"
></android.support.v4.view.ViewPager>
</LinearLayout>
来源:https://blog.csdn.net/jiaxiaofei622/article/details/85013037
以上是关于推荐条+fragment的主要内容,如果未能解决你的问题,请参考以下文章