1.侧滑和切换
package com.example.administrator.mnykt; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RadioGroup; import com.example.administrator.fragment.Frag1; import com.example.administrator.fragment.Frag2; import com.example.administrator.fragment.Frag3; import com.example.administrator.fragment.Frag4; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private Frag1 frag1; private Frag2 frag2; private Frag3 frag3; private Frag4 frag4; private ArrayList<Fragment> list; private RadioGroup radio_group; private FrameLayout fram_layout; private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final DrawerLayout d1 = (DrawerLayout) findViewById(R.id.d1); ImageView iv1 = (ImageView) findViewById(R.id.iv1); ImageView iv2 = (ImageView) findViewById(R.id.iv2); fram_layout = (FrameLayout) findViewById(R.id.fram_layout); radio_group = (RadioGroup) findViewById(R.id.radio_group); iv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d1.openDrawer(Gravity.LEFT); } }); iv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d1.closeDrawer(Gravity.LEFT); } }); final Frag1 frag1 = new Frag1(); final Frag2 frag2 = new Frag2(); final Frag3 frag3 = new Frag3(); final Frag4 frag4 = new Frag4(); manager = getSupportFragmentManager(); manager.beginTransaction().replace(R.id.fram_layout,frag1).commit(); radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction transaction = manager.beginTransaction(); switch (checkedId){ case R.id.rb1: transaction.replace(R.id.fram_layout,frag1); break; case R.id.rb2: transaction.replace(R.id.fram_layout,frag2); break; case R.id.rb3: transaction.replace(R.id.fram_layout,frag3); break; case R.id.rb4: transaction.replace(R.id.fram_layout,frag4); break; } transaction.commit(); } }); } }
2.布局
<?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:id="@+id/d1" 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" > <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/colorPrimaryDark" > <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主页面" android:layout_centerHorizontal="true" android:textSize="20sp" android:layout_marginTop="10dp" /> </RelativeLayout> <FrameLayout android:id="@+id/fram_layout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8" /> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <RadioButton android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="首页" android:button="@null" android:textSize="20sp" /> <RadioButton android:id="@+id/rb2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="关注" android:button="@null" android:textSize="20sp" /> <RadioButton android:id="@+id/rb3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="推荐" android:button="@null" android:textSize="20sp" /> <RadioButton android:id="@+id/rb4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="娱乐" android:button="@null" android:textSize="20sp" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="350dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/colorPrimary" > <ImageView android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
3.fragment页面
package com.example.administrator.fragment; import android.os.Bundle; import android.support.annotation.NonNull; 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.ListView; import android.widget.Toast; import com.example.administrator.adapter.Adapter; import com.example.administrator.bean.NewsBean; import com.example.administrator.helper.NewsDao; import com.example.administrator.helper.User; import com.example.administrator.mnykt.R; import com.example.administrator.utils.HttpUtils; import com.example.administrator.utils.NetUtils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.HashSet; public class Frag1 extends Fragment implements HttpUtils.Netcallback { private PullToRefreshListView pulltorefsh; private String url = "http://ttpc.dftoutiao.com/jsonpc/refresh?type="; //private String bannerUrl = "http://www.xieast.com/api/banner.php"; private int type = 5010; private ArrayList<NewsBean.DataBean> list; private Adapter adapter; private NewsDao newsDao; private HttpUtils httpUtils; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getContext(), R.layout.frag1, null); pulltorefsh = view.findViewById(R.id.pulltorefsh); pulltorefsh.setMode(PullToRefreshBase.Mode.BOTH); pulltorefsh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { type=5010; pulltorefsh.postDelayed(new Runnable() { @Override public void run() { pulltorefsh.onRefreshComplete(); } },2000); getData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { type++; pulltorefsh.postDelayed(new Runnable() { @Override public void run() { pulltorefsh.onRefreshComplete(); } },2000); getData(); } }); //创建一个空的结合 list = new ArrayList<>(); //创建适配器 adapter = new Adapter(getActivity(), list); pulltorefsh.setAdapter(adapter); //创建dao层 newsDao = new NewsDao(getActivity()); httpUtils = HttpUtils.getInstance(); getData(); //网络工具类 return view; } private void getData() { User user = newsDao.query(url + type); if (user!=null){ parserjson(user.json); }else{ if (NetUtils.iswlzj(getActivity())){ httpUtils.getwangluo(url+type,this); }else{ Toast.makeText(getActivity(), "请连接网络", Toast.LENGTH_SHORT).show(); } } } private void parserjson(String json) { Gson gson = new Gson(); NewsBean newsBean = gson.fromJson(json, NewsBean.class); if (type==5010){ list.clear(); } list.addAll(newsBean.getData()); HashSet hashSet = new HashSet(list); list.clear(); list.addAll(hashSet); adapter.notifyDataSetChanged(); } @Override public void onSuccess(String result) { Log.i("aaa","执行了success"); //截取字段 String json = result.replace("null(", "").replace(")", ""); //把数据添加到数据库 newsDao.insert(new User((url+type),json)); Gson gson = new Gson(); NewsBean newsBean = gson.fromJson(json, NewsBean.class); list.addAll(newsBean.getData()); } @Override public void onError(String s) { Log.i("aaa",s); } }
4.适配器
package com.example.administrator.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.administrator.bean.NewsBean; import com.example.administrator.mnykt.R; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; public class Adapter extends BaseAdapter { private Context context; private ArrayList<NewsBean.DataBean> list; public Adapter(Context context, ArrayList<NewsBean.DataBean> list) { this.context = context; this.list = list; } private final int TYPE_0=0; private final int TYPE_1=1; @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { try { return list.get(position); } catch (Exception e) { e.printStackTrace(); } return null; } @Override public long getItemId(int position) { return 0; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { int p=position%2; if (p==0){ return TYPE_0; }else{ return TYPE_1; } } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); if (type==0){ ViewHolder holder=null; if (convertView==null){ convertView = View.inflate(context, R.layout.item1, null); holder = new ViewHolder(); holder.text_view = (TextView)convertView.findViewById(R.id.text_view); holder.iv3 = (ImageView)convertView.findViewById(R.id.iv3); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.text_view.setText(list.get(position).getBrief()); ImageLoader.getInstance().displayImage(list.get(position).getMiniimg().get(0).getSrc(),holder.iv3); return convertView; }else if (type==1){ ViewHolder holder=null; if (convertView==null){ convertView = View.inflate(context, R.layout.item2, null); holder = new ViewHolder(); holder.text_view1 = (TextView)convertView.findViewById(R.id.text_view1); holder.iv4 = (ImageView)convertView.findViewById(R.id.iv4); holder.iv5 = (ImageView)convertView.findViewById(R.id.iv5); holder.iv6 = (ImageView)convertView.findViewById(R.id.iv6); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.text_view1.setText(list.get(position).getBrief()); ImageLoader.getInstance().displayImage(list.get(position).getMiniimg().get(0).getSrc(),holder.iv4); ImageLoader.getInstance().displayImage(list.get(position).getMiniimg().get(0).getSrc(),holder.iv5); ImageLoader.getInstance().displayImage(list.get(position).getMiniimg().get(0).getSrc(),holder.iv6); return convertView; } return null; } public class ViewHolder{ public TextView text_view; public ImageView iv3; public TextView text_view1; public ImageView iv4; public ImageView iv5; public ImageView iv6; } }