rawerLayout是Library包中实现了侧滑菜单效果的控件,drawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏(drawerLayout自身特性),
一定要导入包
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
app:tabMode="scrollable"
android:id="@+id/tab_layout"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tab_layout"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
<ListView
android:id="@+id/left_lv"
android:layout_width="240sp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#191515"
android:dividerHeight="1dp"
android:background="#fff"
/>
</android.support.v4.widget.DrawerLayout>
fragment.xml 里面写一个pullToRefreshListView
<?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">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrAnimationStyle="rotate"
ptr:ptrHeaderTextColor="#ffffff"
ptr:ptrHeaderSubTextColor="#00ffff"
ptr:ptrHeaderBackground="@null"
ptr:ptrDrawable="@mipmap/ic_launcher"/>
</LinearLayout>
下面是代码注释全自己看
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.bwie.adapter.MyAdapter;
import com.bwie.fragment.TabFragment;
import java.util.ArrayList;
import java.util.List;
public class SecondActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private DrawerLayout drawerLayout;
private List<String> mTitleList = new ArrayList<>();//导航集合
private ListView lv;
private List<String> list = new ArrayList<>();
private ArrayList<TabFragment> fs;
private String[] urls = {
"http://gank.io/api/data/Android/10/1",
"http://gank.io/api/data/Android/10/2",
"http://gank.io/api/data/Android/10/3",
"http://gank.io/api/data/Android/10/4",
"http://gank.io/api/data/Android/10/5",
"http://gank.io/api/data/Android/10/6",
"http://gank.io/api/data/Android/10/7",
"http://gank.io/api/data/Android/10/8",
"http://gank.io/api/data/Android/10/9",
"http://gank.io/api/data/Android/10/10",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initData();
initCehua();
}
private void initData() {
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout.setupWithViewPager(viewPager); //tablayou 关联viewPager
//标题集合添加数据
mTitleList.add("头条");mTitleList.add("社会");mTitleList.add("国内");
mTitleList.add("国际");mTitleList.add("娱乐");mTitleList.add("体育");
mTitleList.add("军事");mTitleList.add("科技");mTitleList.add("财经");
fs = new ArrayList<TabFragment>();
for(int i=0;i<10;i++){
TabFragment tf = new TabFragment();
Bundle b = new Bundle();
b.putString("url",urls[i]);
tf.setArguments(b);
fs.add(tf);
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
TabFragment tabfragment = fs.get(position);
return tabfragment;
}
@Override
public int getCount() {
return mTitleList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}
});
}
private void initCehua() {
drawerLayout= (DrawerLayout) findViewById(R.id.draw);
lv= (ListView) findViewById(R.id.left_lv);
list.add("搜索"); list.add("收藏"); list.add("消息");
list.add("离线"); list.add("活动"); list.add("更多");
MyAdapter adapter = new MyAdapter(SecondActivity.this,list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
drawerLayout.closeDrawer(lv);
}
});
}
}
创建一个Fragment "TabFragment"
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.ListView;
import android.widget.Toast;
import com.bwie.adapter.MyBaseAdapter;
import com.bwie.bean.SuperClassStudy;
import com.bwie.myapplication.R;
import com.bwie.utils.NetWorkUtils;
import com.bwie.utils.URLDB;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
public class TabFragment extends Fragment {
private MyBaseAdapter myBaseAdapter;
private List<SuperClassStudy.ResultsBean> results;
private PullToRefreshListView mPullRefreshListView;
private String url = "";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment,container,false);
mPullRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
Bundle bundle = getArguments();
url= bundle.getString("url");
//加载utl对应的json数据,解析,展示在ListView上
initView();
initData();
return view;
}
public void initData() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json = new NetWorkUtils().getJsonHttpConnetion(URLDB.URL_IJ+"1");
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClassStudy superClassStudy = new Gson().fromJson(s, SuperClassStudy.class);
results = superClassStudy.getResults();
myBaseAdapter = new MyBaseAdapter(results,getActivity());
mPullRefreshListView.setAdapter(myBaseAdapter);
}
}.execute();
}
public void initView(){
mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//加载新数据,刷新显示
pullToRefreshData();
Toast.makeText(getActivity(),"刷新完毕",Toast.LENGTH_SHORT).show();
}
});
mPullRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
//加载更多数据
loadMoreData();
Toast.makeText(getActivity(),"加载完毕",Toast.LENGTH_SHORT).show();
}
});
}
int count=1;
//加载数据
private void loadMoreData() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
count++;
String json = new NetWorkUtils().getJsonHttpConnetion(URLDB.URL_IJ+count);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClassStudy superClassStudy = new Gson().fromJson(s, SuperClassStudy.class);
List<SuperClassStudy.ResultsBean> results1 = superClassStudy.getResults();
results.addAll(results1);
myBaseAdapter.notifyDataSetChanged();//刷新适配器
}
}.execute();
}
//刷新数据
private void pullToRefreshData() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json = new NetWorkUtils().getJsonHttpConnetion(url);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SuperClassStudy superClassStudy = new Gson().fromJson(s, SuperClassStudy.class);
results = superClassStudy.getResults();
myBaseAdapter = new MyBaseAdapter(results,getActivity());
mPullRefreshListView.setAdapter(myBaseAdapter);
mPullRefreshListView.onRefreshComplete();//停止刷新
}
}.execute();
}
}
多多支持多多点赞