本文主要是介绍DrawerLayout+TabLayout+ViewPage+prt多条目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Util工具类
package com.example.zonghe.Utils;import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;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.URL;
import java.net.URLConnection;public class MyUtil {private static MyUtil instance;private MyUtil(){}public static MyUtil getInstance(){if(instance==null){instance = new MyUtil();}return instance;}//通过Uri得到地址public String url2Path(Uri uri,Context context){String [] proj = {MediaStore.Images.Media.DATA};CursorLoader loader = new CursorLoader(context, uri, proj, null, null, null);Cursor cursor = loader.loadInBackground();int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);cursor.moveToFirst();String string = cursor.getString(columnIndexOrThrow);return string;}
//二次采样public Bitmap caiyang (String iconPath,int wigth,int height){BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds=true;BitmapFactory.decodeFile(iconPath,options);options.inSampleSize = Math.max(options.outHeight/height,options.outWidth/wigth);options.inJustDecodeBounds = false;Bitmap bitmap = BitmapFactory.decodeFile(iconPath, options);return bitmap;}//获取字符public String getString (InputStream stream) throws IOException {BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));StringBuilder builder = new StringBuilder();for (String tmp = bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){builder.append(tmp);}return builder.toString();}public InputStream getNewData (String url){try {URL url1 = new URL(url);HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();urlConnection.setRequestMethod("GET");urlConnection.setReadTimeout(10000);urlConnection.setConnectTimeout(10000);int responseCode = urlConnection.getResponseCode();if(responseCode==200){return urlConnection.getInputStream();}} catch (IOException e) {e.printStackTrace();}return null;}public <T>T getGson (String s,Class clazz){Object o = new Gson().fromJson(s, clazz);return (T)o;}public interface CallBack<T>{void getData(T o);}public void getNetData(String url, final Class clazz, final CallBack callBack){new AsyncTask<String, Void, Object>() {@Overrideprotected Object doInBackground(String... strings) {InputStream newData = getNewData(strings[0]);try {String string = getString(newData);Object gson = getGson(string, clazz);return gson;} catch (IOException e) {e.printStackTrace();}return null;}@Overrideprotected void onPostExecute(Object o) {callBack.getData(o);}}.execute(url);}}
Activity
<?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"android:id="@+id/drawer"tools:context=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.ViewPagerandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/viewpage"android:layout_above="@id/tablayout"></android.support.v4.view.ViewPager><android.support.design.widget.TabLayoutandroid:layout_width="match_parent"android:id="@+id/tablayout"android:layout_height="wrap_content"android:layout_alignParentBottom="true"></android.support.design.widget.TabLayout></RelativeLayout><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/fragment_drawer"android:layout_gravity="left"android:background="#ffffff"></FrameLayout>
</android.support.v4.widget.DrawerLayout><?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.TabLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/tablayout_dianying"android:overScrollMode="ifContentScrolls"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"></android.support.design.widget.TabLayout><android.support.v4.view.ViewPagerandroid:layout_width="0dp"android:layout_height="0dp"android:id="@+id/viewpage_dianying"app:layout_constraintTop_toBottomOf="@id/tablayout_dianying"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintBottom_toBottomOf="parent"></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
MainActivity
package com.example.zonghe;import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;import com.example.zonghe.adapter.ContentViewPage;
import com.example.zonghe.base.BaseActivity;
import com.example.zonghe.fragment.LeftFragment;public class MainActivity extends BaseActivity {private DrawerLayout drawerLayout;private ActionBarDrawerToggle toggle;private ViewPager viewPager;private TabLayout tabLayout;private ContentViewPage contentViewPage;@Overrideprotected int setView() {return R.layout.activity_main;}@Overrideprotected void initView(Bundle savedInstanceState) {viewPager = findViewById(R.id.viewpage);tabLayout = findViewById(R.id.tablayout);contentViewPage = new ContentViewPage(getSupportFragmentManager());if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.fragment_drawer,new LeftFragment()).commit();}getSupportActionBar().setDisplayHomeAsUpEnabled(true);drawerLayout = findViewById(R.id.drawer);toggle = new ActionBarDrawerToggle(this, drawerLayout,R.string.open,R.string.close);drawerLayout.addDrawerListener(toggle);toggle.syncState();}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(toggle.onOptionsItemSelected(item)){return true;}return super.onOptionsItemSelected(item);}@Overrideprotected void initData() {viewPager.setAdapter(contentViewPage);tabLayout.setupWithViewPager(viewPager);}
}
Fragment
//侧拉fragment
package com.example.zonghe.fragment;import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;import com.example.zonghe.R;
import com.example.zonghe.Utils.MyUtil;
import com.example.zonghe.base.BaseFragment;
import com.example.zonghe.leftAdapter;public class LeftFragment extends BaseFragment {private ListView listview;private com.example.zonghe.leftAdapter leftAdapter;private final int IMAGE_OK = 100;private Uri iconUri;@Overrideprotected int setView() {return R.layout.leftfragment;}@Overrideprotected void initView(View view) {listview = view.findViewById(R.id.leftlistview);leftAdapter = new leftAdapter(getActivity());listview.setAdapter(leftAdapter);}@Overrideprotected void initData() {listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(getActivity(),"123",Toast.LENGTH_LONG).show();if(position==0){Intent intent = new Intent(Intent.ACTION_PICK);intent.setType("image/*");startActivityForResult(intent,IMAGE_OK);}return true;}});}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode==IMAGE_OK&&resultCode==getActivity().RESULT_OK){iconUri = data.getData();String s = MyUtil.getInstance().url2Path(iconUri, getActivity());Bitmap caiyang = MyUtil.getInstance().caiyang(s, 100, 100);leftAdapter.setIcon(caiyang);}}
}prt Fragment
package com.example.zonghe.fragment;import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;import com.example.zonghe.R;
import com.example.zonghe.Utils.MyUtil;
import com.example.zonghe.adapter.ShangYingFragmentAdapter;
import com.example.zonghe.base.BaseFragment;
import com.example.zonghe.bean.Bean;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.internal.Utils;public class ShangYingFragment extends BaseFragment {private String url;private PullToRefreshListView listView;private ShangYingFragmentAdapter shangYingFragmentAdapter;@Overrideprotected int setView() {return R.layout.shangying_fragment;}@Overrideprotected void initView(View view) {url = getArguments().getString("url");listView = view.findViewById(R.id.pulllistview);shangYingFragmentAdapter = new ShangYingFragmentAdapter(getActivity());listView.setAdapter(shangYingFragmentAdapter);listView.setMode(PullToRefreshBase.Mode.BOTH);listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {mpage = 1;initData();}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {initData();}});}private int mpage = 1;@Overrideprotected void initData() {MyUtil.getInstance().getNetData(url + mpage, Bean.class, new MyUtil.CallBack<Bean>() {@Overridepublic void getData(Bean o) {if (mpage == 1) {shangYingFragmentAdapter.setData(o.getResult());} else {shangYingFragmentAdapter.addData(o.getResult());}mpage++;listView.onRefreshComplete();}});}
}
///
//TabLayout 绑定
package com.example.zonghe.fragment;import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;import com.example.zonghe.R;
import com.example.zonghe.adapter.DianYingPagerAdapter;
import com.example.zonghe.base.BaseFragment;public class Fragmentdianying extends BaseFragment {@Overrideprotected int setView() {return R.layout.fragmentdianying;}@Overrideprotected void initView(View view) {ViewPager viewPager = view.findViewById(R.id.viewpage_dianying);TabLayout tabLayout = view.findViewById(R.id.tablayout_dianying);viewPager.setAdapter(new DianYingPagerAdapter(getChildFragmentManager()));tabLayout.setupWithViewPager(viewPager);}@Overrideprotected void initData() {}
}
Abapter
package com.example.zonghe.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.zonghe.fragment.FragmentShouye;
import com.example.zonghe.fragment.FragmentWode;
import com.example.zonghe.fragment.Fragmentdianying;
public class ContentViewPage extends FragmentPagerAdapter {
private String [] pageNames = new String[]{“首页”,“电影”,“我的”};
public ContentViewPage(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {switch (i){case 0:return new Fragmentdianying();case 1:return new FragmentShouye();case 2:return new FragmentWode();}return null;
}@Override
public int getCount() {return pageNames.length;
}@Nullable
@Override
public CharSequence getPageTitle(int position) {return pageNames[position];
}
}
切换fragment 传url
package com.example.zonghe.adapter;
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 com.example.zonghe.fragment.ShangYingFragment;
public class DianYingPagerAdapter extends FragmentPagerAdapter {
private String [] names = new String[]{“正在上映”,“即将热映”};
private String[] urls = new String[]{
“http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=”,
“http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=”
};
public DianYingPagerAdapter(FragmentManager fm) {super(fm);
}@Override
public Fragment getItem(int i) {switch (i){default:Bundle bundle = new Bundle();bundle.putString("url",urls[i]);ShangYingFragment shangYingFragment = new ShangYingFragment();shangYingFragment.setArguments(bundle);return shangYingFragment;}}@Override
public int getCount() {return names.length;
}@Nullable
@Override
public CharSequence getPageTitle(int position) {return names[position];
}
}
///
prt fragment
package com.example.zonghe.adapter;
import android.content.Context;
import android.support.v4.view.ViewPager;
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.zonghe.R;
import com.example.zonghe.bean.Bean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class ShangYingFragmentAdapter extends BaseAdapter {
private List<Bean.ResultBean>mlist;
private Context mContext;
public ShangYingFragmentAdapter(Context mContext) {this.mContext = mContext;mlist = new ArrayList<>();
}
public void setData(List<Bean.ResultBean>list){mlist.clear();if(list!=null){mlist.addAll(list);}notifyDataSetChanged();
}
public void addData(List<Bean.ResultBean>list){if(list!=null){mlist.addAll(list);notifyDataSetChanged();}
}@Override
public int getCount() {return mlist.size();
}@Override
public Bean.ResultBean getItem(int position) {return mlist.get(position);
}@Override
public long getItemId(int position) {return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {ViewHoder viewHoder;if(convertView==null){convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shangying,parent,false);viewHoder = new ViewHoder();viewHoder.imageView = convertView.findViewById(R.id.imageview_shangying);viewHoder.texttitle = convertView.findViewById(R.id.title_shangying);viewHoder.textsum = convertView.findViewById(R.id.sun_shangying);convertView.setTag(viewHoder);}else{viewHoder = (ViewHoder) convertView.getTag();}ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),viewHoder.imageView);viewHoder.textsum.setText(getItem(position).getSummary());viewHoder.texttitle.setText(getItem(position).getName());return convertView;
}
class ViewHoder {ImageView imageView;TextView texttitle,textsum;
}
}
///
//侧拉Adapter
package com.example.zonghe.adapter;
import android.content.Context;
import android.support.v4.view.ViewPager;
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.zonghe.R;
import com.example.zonghe.bean.Bean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class ShangYingFragmentAdapter extends BaseAdapter {
private List<Bean.ResultBean>mlist;
private Context mContext;
public ShangYingFragmentAdapter(Context mContext) {this.mContext = mContext;mlist = new ArrayList<>();
}
public void setData(List<Bean.ResultBean>list){mlist.clear();if(list!=null){mlist.addAll(list);}notifyDataSetChanged();
}
public void addData(List<Bean.ResultBean>list){if(list!=null){mlist.addAll(list);notifyDataSetChanged();}
}@Override
public int getCount() {return mlist.size();
}@Override
public Bean.ResultBean getItem(int position) {return mlist.get(position);
}@Override
public long getItemId(int position) {return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {ViewHoder viewHoder;if(convertView==null){convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shangying,parent,false);viewHoder = new ViewHoder();viewHoder.imageView = convertView.findViewById(R.id.imageview_shangying);viewHoder.texttitle = convertView.findViewById(R.id.title_shangying);viewHoder.textsum = convertView.findViewById(R.id.sun_shangying);convertView.setTag(viewHoder);}else{viewHoder = (ViewHoder) convertView.getTag();}ImageLoader.getInstance().displayImage(getItem(position).getImageUrl(),viewHoder.imageView);viewHoder.textsum.setText(getItem(position).getSummary());viewHoder.texttitle.setText(getItem(position).getName());return convertView;
}
class ViewHoder {ImageView imageView;TextView texttitle,textsum;
}
}
这篇关于DrawerLayout+TabLayout+ViewPage+prt多条目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!