标签:
Android——ViewPager+Fragment+ListView之间
<span style="font-size:18px;">package com.example.jreduch05; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import com.astuetz.PagerSlidingTabStrip; import com.example.jreduch05.adapter.MyVpFragmentAdapter; import com.example.jreduch05.fragment.VpFragmentLv; import java.util.ArrayList; import java.util.List; public class VpFragmentLvActivity extends AppCompatActivity { private ViewPager vp; private PagerSlidingTabStrip pst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_vp_fragment_lv); getSupportActionBar().hide(); vp=(ViewPager)findViewById(R.id.vp); pst= (PagerSlidingTabStrip) findViewById(R.id.pst); List<Fragment> list=new ArrayList<>(); List<String> titles=new ArrayList<>(); titles.add("推荐"); titles.add("热点"); titles.add("烟台"); titles.add("视频"); titles.add("订阅"); titles.add("体育"); titles.add("娱乐"); titles.add("奥运"); titles.add("财经"); titles.add("国际"); for (int i=1;i<=10;i++){ Fragment fragment=new VpFragmentLv(); Bundle bundle=new Bundle(); bundle.putInt("arg",i); fragment.setArguments(bundle); list.add(fragment); } MyVpFragmentAdapter ma=new MyVpFragmentAdapter( getSupportFragmentManager(),list,titles); vp.setAdapter(ma); pst.setIndicatorColor(getResources().getColor(R.color.colorPrimary)); pst.setIndicatorHeight(5); pst.setAllCaps(true); pst.setShouldExpand(true);//平分 pst.setTextColor(getResources().getColor(R.color.colorAccent)); pst.setViewPager(vp); } } </span>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#ef4848" android:id="@+id/rr" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="今日头条@" android:textSize="26sp" android:textColor="#ffffff" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/base_main_action_personal_normal" android:id="@+id/imageView" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/right_drawer_normal" android:id="@+id/imageView2" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" /> </RelativeLayout> <com.astuetz.PagerSlidingTabStrip android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/pst" android:layout_below="@+id/rr" ></com.astuetz.PagerSlidingTabStrip> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/pst" android:id="@+id/ll" > <TextView android:layout_width="wrap_content" android:layout_height="30dp" android:text="2016.08.09" android:layout_marginLeft="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="30dp" android:text="星期二" android:layout_marginLeft="50dp" /> <TextView android:layout_width="wrap_content" android:layout_height="30dp" android:text="今天" android:layout_marginLeft="120dp" /> </LinearLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/vp" android:layout_below="@+id/ll" ></android.support.v4.view.ViewPager> </RelativeLayout>
package com.example.jreduch05.fragment; import android.content.Intent; 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.AdapterView; import android.widget.ListView; import com.example.jreduch05.IntentActivity; import com.example.jreduch05.R; import com.example.jreduch05.adapter.MyListAdapter; import com.example.jreduch05.entity.News; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class VpFragmentLv extends Fragment { public VpFragmentLv() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_vp_fragment_lv, container, false); } @Override public void onActivityCreated (@Nullable Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); // TextView tv=(TextView)getView().findViewById(R.id.tv); ListView lv = (ListView) getView().findViewById(R.id.lv); final List<News> list = new ArrayList<>(); MyListAdapter ma = new MyListAdapter(list, getActivity()); lv.setAdapter(ma); final Bundle bundle = getArguments(); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { News news = list.get(i); Intent intent = new Intent(getActivity(), IntentActivity.class); intent.putExtra("obj", news); Bundle b1 = new Bundle(); b1.putString("arg1", "今天七月七"); intent.putExtra("bundle", b1); startActivity(intent); } }); if (bundle != null) { int arg = bundle.getInt("arg"); // tv.setText("我是Fagment"+arg); switch (arg) { case 1: for (int i = 0; i < 20; i++) { list.add(new News("体育新闻" + i, "今天", "0001", R.mipmap.zyfzyf, "新浪网")); ma.notifyDataSetChanged(); } break; case 2: for (int i = 0; i < 20; i++) { list.add(new News("烟台新闻" + i, "今天", "0002", R.mipmap.zyf, "新浪网")); ma.notifyDataSetChanged(); } break; case 3: for (int i = 0; i < 20; i++) { list.add(new News("奥运新闻" + i, "今天", "0002", R.mipmap.zz, "新浪网")); ma.notifyDataSetChanged(); } break; } } } }
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jreduch05.fragment.VpFragmentLv"> <!-- TODO: Update blank fragment layout --> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" > </ListView> </FrameLayout>
package com.example.jreduch05.entity; import android.os.Parcel; import android.os.Parcelable; import java.io.Serializable; /** * Created by 冲天之峰 on 2016/8/9. */ //implements Serializable 接口 //Parcelable Android 独有的 public class News implements Parcelable{ private String title; private String pubDate; // private String newId; private int img; private String from; public News(String title, String pubDate, String newId, int img, String from) { this.title = title; this.pubDate = pubDate; // this.newId = newId; this.img = img; this.from = from; } protected News(Parcel in) { title = in.readString(); pubDate = in.readString(); img = in.readInt(); from = in.readString(); } public static final Creator<News> CREATOR = new Creator<News>() { @Override public News createFromParcel(Parcel in) { return new News(in); } @Override public News[] newArray(int size) { return new News[size]; } }; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } // public String getNewId() { // return newId; // } // // public void setNewId(String newId) { // this.newId = newId; // } public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(title); parcel.writeString(pubDate); parcel.writeInt(img); parcel.writeString(from); } }
package com.example.jreduch05.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by 冲天之峰 on 2016/8/9. */ public class MyVpFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> list; private List<String> titles; public MyVpFragmentAdapter(FragmentManager fm, List<Fragment> list, List<String> titles) { super(fm); this.list= list; this.titles= titles; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }
package com.example.jreduch05; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTabHost; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TextView; import com.example.jreduch05.adapter.MyTabHostAdapter; import com.example.jreduch05.fragment.TabHostFragment; import java.util.ArrayList; import java.util.List; public class FragmentTabHostActivity extends AppCompatActivity { private ViewPager vp; private FragmentTabHost ft; private String[] str={"首页","消息","更多","好友","搜索"}; private int[] imgRes={R.drawable.home, R.drawable.meassage,R.drawable.more,R.drawable.selfinfo,R.drawable.square}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_tab_host); vp=(ViewPager)findViewById(R.id.vp); ft=(FragmentTabHost)findViewById(R.id.ft); init(); } private void init() { List<Fragment> list=new ArrayList<>(); // ft.setup(this,getSupportFragmentManager()); for(int i=0;i<str.length;i++){ Fragment fragment=new TabHostFragment(); Bundle bundle=new Bundle(); bundle.putInt("arg",i); TabHost.TabSpec tabSpec=ft.newTabSpec(str[i]).setIndicator(getView(i)); fragment.setArguments(bundle); ft.addTab(tabSpec,fragment.getClass(),null); list.add(fragment); } MyTabHostAdapter mha=new MyTabHostAdapter(getSupportFragmentManager(),list); vp.setAdapter(mha); ft.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String s) { vp.setCurrentItem(ft.getCurrentTab()); } }); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { ft.setCurrentTab(position); } @Override public void onPageScrollStateChanged(int state) { } }); } public View getView(int i){ View v=getLayoutInflater().inflate(R.layout.tab_layout,null); ImageView iv=(ImageView)v.findViewById(R.id.iv); iv.setImageResource(imgRes[i]); TextView tv=(TextView)v.findViewById(R.id.tv); tv.setText(str[i]); return v; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.jreduch05.FragmentTabHostActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/vp" ></android.support.v4.view.ViewPager> <android.support.v4.app.FragmentTabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ft" ></android.support.v4.app.FragmentTabHost> </LinearLayout>
package com.example.jreduch05.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; import com.example.jreduch05.R; /** * A simple {@link Fragment} subclass. */ public class TabHostFragment extends Fragment { public TabHostFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_tab_host, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); TextView tv=(TextView)getView().findViewById(R.id.tv); Bundle bundle=getArguments(); if (bundle!=null){ int arg=bundle.getInt("arg"); tv.setText("我是Fragment"+(arg+1)); } } }
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jreduch05.fragment.TabHostFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" android:id="@+id/tv" android:gravity="center" /> </FrameLayout>
package com.example.jreduch05.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by 冲天之峰 on 2016/8/9. */ public class MyTabHostAdapter extends FragmentPagerAdapter { private List<Fragment> list; public MyTabHostAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list= list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/icon_home_sel" android:state_selected="true"></item> <item android:drawable="@mipmap/icon_home_nor"></item> </selector>
作者:冲天之峰 20160809
Android——ViewPager+Fragment+ListView之间
标签:
原文地址:http://blog.csdn.net/zhangyufeng0126/article/details/52166523