Abddoid ViewPager+Fragment+ListView àì夳堔傛蜴生んèń 2022-07-19 02:23 193阅读 0赞 **利用ViewPager.Fragment.ListView完成新闻滑动界面,并且添加标题栏。** **\*首先注意要添加第三方组件PagerSlidingTabStrip** **1.写FragmentAdapter和ListAdapter以及News新闻类** FragmentAdapter: package com.example.administrator.jreduch06.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 Administrator on 2016/8/9. */ public class MyFragmentAdapter extends FragmentPagerAdapter{ private List<Fragment> list; private List<String> titles; public MyFragmentAdapter(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); } } **ListAdapter:** package com.example.administrator.jreduch06.adapter; import android.content.Context; 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.administrator.jreduch06.R; import com.example.administrator.jreduch06.enitty.News; import java.util.List; /** * Created by Administrator on 2016/8/9. */ public class MyListAdapter extends BaseAdapter{ private List<News> list; private Context context; public MyListAdapter(List<News> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vh; if(convertView==null){ vh=new ViewHolder(); convertView= LayoutInflater.from(context).inflate(R.layout.list_layout,null); vh.img=(ImageView)convertView.findViewById(R.id.img); vh.title=(TextView)convertView.findViewById(R.id.title); vh.pubData=(TextView)convertView.findViewById(R.id.pubData); vh.from=(TextView)convertView.findViewById(R.id.from); convertView.setTag(vh); }else{ vh=(ViewHolder)convertView.getTag(); } News news=list.get(position); vh.title.setText(news.getTitle()); vh.pubData.setText(news.getPubData()); vh.from.setText(news.getFrom()); vh.img.setImageResource(news.getImg()); return convertView; } public class ViewHolder{ ImageView img; TextView title; TextView pubData; TextView from; } } **News:** package com.example.administrator.jreduch06.enitty; /** * Created by Administrator on 2016/8/9. */ public class News { private int img; private String title; private String pubData; private String from; public News(int img, String title, String pubData, String from) { this.img = img; this.title = title; this.pubData = pubData; this.from = from; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPubData() { return pubData; } public void setPubData(String pubData) { this.pubData = pubData; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } } **2.FragmentPagerActivity代码及布局** **布局:放置一个PagerSlidingTabStrip和ViewPager** <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.administrator.jreduch06.FragmentPagerActivity"> <com.astuetz.PagerSlidingTabStrip android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/pst" > </com.astuetz.PagerSlidingTabStrip> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" android:layout_below="@+id/pst" > </android.support.v4.view.ViewPager> </RelativeLayout> **代码:** package com.example.administrator.jreduch06; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.astuetz.PagerSlidingTabStrip; import com.example.administrator.jreduch06.adapter.MyFragmentAdapter; import com.example.administrator.jreduch06.fragment.PagerFragment; import java.util.ArrayList; import java.util.List; public class FragmentPagerActivity extends AppCompatActivity { private ViewPager vp; private PagerSlidingTabStrip pst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_pager); 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("奥运"); for(int i=1;i<=5;i++){ Fragment fragment=new PagerFragment(); Bundle bundle=new Bundle(); bundle.putInt("arg", i); fragment.setArguments(bundle); list.add(fragment); } MyFragmentAdapter ma= new MyFragmentAdapter (getSupportFragmentManager(),list,titles); vp.setAdapter(ma); // pst.setShouldExpand(true); // 设置标题栏中的文字平分。 pst.setIndicatorColor(getResources().getColor(R.color.colorPrimary)); //标题栏颜色。 pst.setIndicatorHeight(5); //指示条高度。 pst.setTextColor(getResources().getColor(R.color.colorAccent)); //标题栏文字颜色。 pst.setTabBackground(R.drawable.color_background); //设置标题栏背景色。 pst.setViewPager(vp); } } **3.PageFragment布局及代码** 布局: <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.administrator.jreduch06.fragment.PagerFragment"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv"> </ListView> </FrameLayout> 代码: package com.example.administrator.jreduch06.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.example.administrator.jreduch06.R; import com.example.administrator.jreduch06.adapter.MyListAdapter; import com.example.administrator.jreduch06.enitty.News; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class PagerFragment extends Fragment { public PagerFragment() { // 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_pager, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView lv=(ListView)getView().findViewById(R.id.lv); List<News> list=new ArrayList<>(); MyListAdapter ma=new MyListAdapter(list,getActivity()); lv.setAdapter(ma); Bundle bundle=getArguments(); if (bundle!=null) { int arg= bundle.getInt("arg"); switch (arg){ case 1: for(int i=0;i<20;i++){ list.add(new News(R.mipmap.sannv,"体育新闻","今天","搜狐网")); } break; case 2: for(int i=0;i<20;i++){ list.add(new News(R.mipmap.dahai,"国际新闻","昨天","易迅网")); } break; case 3: for(int i=0;i<20;i++){ list.add(new News(R.mipmap.jietu1,"财经新闻","前天","天涯网")); } break; case 4: for(int i=0;i<20;i++){ list.add(new News(R.mipmap.ruzixiaoge,"娱乐新闻","今天","网易网")); } break; case 5: for(int i=0;i<20;i++){ list.add(new News(R.mipmap.huanjue,"奥运新闻","今天","腾讯网")); } break; } } } } 4.新闻格式布局 <?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"> <ImageView android:layout_width="100dp" android:layout_height="70dp" android:src="@mipmap/ic_launcher" android:id="@+id/img" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" android:text="我是标题" android:layout_alignParentTop="true" android:layout_toEndOf="@+id/img" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pubData" android:text="今天" android:layout_alignBaseline="@+id/from" android:layout_alignBottom="@+id/from" android:layout_toEndOf="@+id/title" android:layout_marginStart="96dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/from" android:text="搜狐网" android:layout_alignBottom="@+id/img" android:layout_toEndOf="@+id/title" /> </RelativeLayout> 完成效果: ![Center][] **![Center 1][]** [Center]: /images/20220718/80555ac06e2a43ff8eec56b1c7c68d74.png [Center 1]: /images/20220718/a89c43bee1a942388f614caa146b28e2.png
相关 Abddoid ViewPager+Fragment+ListView 利用ViewPager.Fragment.ListView完成新闻滑动界面,并且添加标题栏。 \首先注意要添加第三方组件PagerSlidingTabStrip 1.写Fr àì夳堔傛蜴生んèń/ 2022年07月19日 02:23/ 0 赞/ 193 阅读
还没有评论,来说两句吧...