Main
package com.example.lianxi;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import com.example.lianxi.fragment.MainFragment1;
import com.example.lianxi.fragment.MainFragment2;
import com.example.lianxi.fragment.MainFragment3;
import com.example.lianxi.fragment.MainFragment4;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//定义变量
private ListView listView;
private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
private RadioGroup group;
private FragmentManager supportFragmentManager;
String TAG = "wzq";
private DrawerLayout drawerLayout;
private LinearLayout cll;
private List<String> lists = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
cll=(LinearLayout)findViewById(R.id.cll);
drawerLayout.openDrawer(cll);
listView = (ListView) findViewById(R.id.lv);
lists.add("我的消息");
lists.add("教学视屏");
lists.add("我的成绩");
lists.add("学生日记");
lists.add("文章收藏");
lists.add("文章足迹");
lists.add("教员中心");
lists.add("设置");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
drawerLayout.closeDrawer(cll);
drawerLayout.openDrawer(cll);
}
});
//侧滑页面的打开关闭
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
//slideOffset:侧滑菜单 宽度的像素值
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
Log.d("zzz", "open");
}
@Override
public void onDrawerClosed(View drawerView) {
Log.d("zzz", "close");
}
@Override
public void onDrawerStateChanged(int newState) {
Log.d("zzz", "状态改变:" + newState);
}
});
//初始化组件
group = (RadioGroup) findViewById(R.id.group);
initFragment();
supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.beginTransaction().add(R.id.fl_main, fragmentList.get(0)).commit();
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch (i) {
case R.id.but1:
supportFragmentManager.beginTransaction().replace(R.id.fl_main,fragmentList.get(0)).commit();
break;
case R.id.but2:
supportFragmentManager.beginTransaction().replace(R.id.fl_main,fragmentList.get(1)).commit();
break;
case R.id.but3:
supportFragmentManager.beginTransaction().replace(R.id.fl_main,fragmentList.get(2)).commit();
break;
}
}
});
}
private void initFragment() {
MainFragment1 mainFragment1 = new MainFragment1();
MainFragment2 mainFragment2 = new MainFragment2();
MainFragment3 mainFragment3 = new MainFragment3();
MainFragment4 mainFragment4 = new MainFragment4();
fragmentList.add(mainFragment1);
fragmentList.add(mainFragment2);
fragmentList.add(mainFragment3);
fragmentList.add(mainFragment4);
}
}
ChildFragment1
package com.example.lianxi.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
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.TextView;
import com.bawei.wzq.wzlibrary.utils.NetWordUtils;
import com.bawei.wzq.wzlibrary.view.XListView;
import com.example.lianxi.Goods;
import com.example.lianxi.MainActivity;
import com.example.lianxi.R;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017-11-03.
*/
public class ChildFragment1 extends Fragment implements XListView.IXListViewListener{
String url = "http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603";
private View view;
List<Goods.ResultBean.RowsBean> list = new ArrayList<>(); //大集合
private MAdapter mAdapter;
private XListView xListView;
private ImageLoader imageLoaderInstances;
//只写加载布局和找到控件
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view=inflater.inflate(R.layout.frag2,null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Goods.ResultBean.RowsBean> list = new ArrayList<>();
xListView=(XListView) view.findViewById(R.id.xlv);
imageLoaderInstances = ImageLoader.getInstance();
mAdapter = new MAdapter();
xListView.setAdapter(mAdapter);
getNetData();//联网请求数据
xListView.setPullLoadEnable(true); //让XListView有上拉加载的功能;
xListView.setXListViewListener(this);
}
private void getNetData() {
new MAsyncTask().execute(url);
}
class MAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
return NetWordUtils.getNetjson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Goods goods = gson.fromJson(s, Goods.class);
//得到集合;
List<Goods.ResultBean.RowsBean> rows = goods.getResult().getRows();
//把从网络解析的数据放到adapter使用的集合里;
list.addAll(rows);
mAdapter.notifyDataSetChanged();
uiComplete();// 让刷新头 或者刷新底部ui返回;
}
}
class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View viewItem = View.inflate(getActivity(), R.layout.listview, null);
TextView textView = (TextView) viewItem.findViewById(R.id.textView);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
textView.setText(list.get(i).getInfo().getLoupan_name());
imageLoaderInstances.displayImage(list.get(i).getInfo().getDefault_image(),imageView);
return viewItem;
}
}
@Override
public void onRefresh() {
list.clear();
getNetData();
}
@Override
public void onLoadMore() {
getNetData();
}
private void uiComplete(){
xListView.stopRefresh();//停止刷新
xListView.stopLoadMore();//停止上拉加载更多
xListView.setRefreshTime("刚刚");
}
}
ChildFragment2
package com.example.lianxi.fragment;
import android.os.Bundle;
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.TextView;
/**
* author:Created by WangZhiQiang on 2017-11-03.
*/
public class ChildFragment2 extends Fragment{
//只写加载布局和找到控件
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("我是ChildFragment2");
return textView;
}
/**
* 写fragment的逻辑;
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
MainFragment1
package com.example.lianxi.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
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 com.example.lianxi.R;
import java.util.ArrayList;
/**
* author:Created by WangZhiQiang on 2017-11-03.
* 1--viewpager
* 2-- Adapter,返回子Fragment;
*/
public class MainFragment1 extends Fragment{
private ViewPager viewpager;
private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
//只写加载布局和找到控件
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
viewpager = view.findViewById(R.id.viewpager);
return view;
}
/**
* 写fragment的逻辑;
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initFragment();
//fragment中嵌套子fragment要用getChildFragmentManager()
//fragment中嵌套子fragment要用getChildFragmentManager()
//fragment中嵌套子fragment要用getChildFragmentManager()
viewpager.setAdapter(new MPagerAdapter(getChildFragmentManager()));
}
private void initFragment() {
ChildFragment1 childFragment1 = new ChildFragment1();
ChildFragment2 childFragment2 = new ChildFragment2();
fragmentList.add(childFragment1);
fragmentList.add(childFragment2);
}
private class MPagerAdapter extends FragmentPagerAdapter {
public MPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
}
MainFragment2
package com.example.lianxi.fragment;
import android.os.Bundle;
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.TextView;
/**
* author:Created by WangZhiQiang on 2017-11-03.
*/
public class MainFragment2 extends Fragment{
//只写加载布局和找到控件
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("我是第二个主fragment");
return textView;
}
/**
* 写fragment的逻辑;
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
MApp
package com.example.lianxi;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* author:Created by WangZhiQiang on 2017-10-10.
* 系统启动 会先运行这个MApp,所以我们在这里进行初始化 框架 组件等等;
*/
public class MApp extends Application{
@Override
public void onCreate() {
super.onCreate();
//初始化组件
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
}
}