1.MainActivity package com.example.lenovo.moni0922; import android.content.Intent; import android.content.SharedPreferences; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Button btn; private ViewPager vp; private SharedPreferences sp; private List<Integer> tupian; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn_insert); vp = findViewById(R.id.vp_plash); sp = getSharedPreferences("config",MODE_PRIVATE); if (sp.getBoolean("isFirst",false)){ Intent intent = new Intent(MainActivity.this,ShowActivity.class); startActivity(intent); } btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sp.edit().putBoolean("isFirst",true).commit(); Intent intent = new Intent(MainActivity.this,ShowActivity.class); startActivity(intent); } }); tupian = new ArrayList<>(); tupian.add(R.drawable.abc); tupian.add(R.drawable.bb); tupian.add(R.drawable.bc); vp.setAdapter(new PagerAdapter() { @Override public int getCount() { return tupian.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { ImageView image = new ImageView(MainActivity.this); image.setImageResource(tupian.get(position)); container.addView(image); return image; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } }); } }
Layout
<?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" android:orientation="vertical"> <Button android:id="@+id/btn_insert" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="点击进入"/> <android.support.v4.view.ViewPager android:id="@+id/vp_plash" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
2.ShowActivity
package com.example.lenovo.moni0922; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.example.lenovo.moni0922.fragment.ShowFragment; import com.example.lenovo.moni0922.fragment.XiaoFragment; import com.example.lenovo.moni0922.fragment.XiguaFragment; import java.util.ArrayList; import java.util.List; public class ShowActivity extends AppCompatActivity implements View.OnClickListener{ private TextView txtShow; private TextView txtXigua; private TextView txtZhao; private TextView txtXiao; private TextView txtMe; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); txtShow = findViewById(R.id.txt_shou); txtXigua = findViewById(R.id.txt_xigua); txtXiao = findViewById(R.id.txt_xiao); vp = findViewById(R.id.vp); final List<Fragment> fragmentList = new ArrayList<>(); ShowFragment show = new ShowFragment(); XiaoFragment xiao = new XiaoFragment(); XiguaFragment xigua = new XiguaFragment(); fragmentList.add(show); fragmentList.add(xiao); fragmentList.add(xigua); vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }); //点击事件 txtShow.setOnClickListener(this); txtXigua.setOnClickListener(this); txtXiao.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.txt_shou: vp.setCurrentItem(0); break; case R.id.txt_xiao: vp.setCurrentItem(1); break; case R.id.txt_xigua: vp.setCurrentItem(2); break; } } }
Layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll_bottom" android:layout_marginTop="5dp" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/txt_shou" android:text="首页" android:padding="10dp" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_xiao" android:text="小视频" android:gravity="center" android:padding="10dp" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_xigua" android:text="西瓜" android:padding="10dp" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_above="@id/ll_bottom" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v4.view.ViewPager> </RelativeLayout>
3.ShowFragment
package com.example.lenovo.moni0922.fragment; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; 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.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.lenovo.moni0922.R; import java.util.ArrayList; import java.util.List; /** * */ public class ShowFragment extends Fragment{ private TabLayout tbl; private ViewPager vp; private List<String> stringList; private List<Fragment> fragmentList; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_show,container,false); tbl = v.findViewById(R.id.tbl); vp = v.findViewById(R.id.vp); return v; } // top(头条),shehui(社会),guonei(国内),guoji(国际),yule(娱乐) // ,tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚) @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); stringList = new ArrayList<>(); stringList.add("头条"); stringList.add("社会"); stringList.add("国内"); stringList.add("国际"); stringList.add("娱乐"); stringList.add("体育"); stringList.add("军事"); stringList.add("科技"); stringList.add("财经"); stringList.add("时尚"); fragmentList = new ArrayList<>(); fragmentList.add(ContentFragment.newInstance("top")); fragmentList.add(ContentFragment.newInstance("shehui")); fragmentList.add(ContentFragment.newInstance("guonei")); fragmentList.add(ContentFragment.newInstance("guoji")); fragmentList.add(ContentFragment.newInstance("yule")); fragmentList.add(ContentFragment.newInstance("tiyu")); fragmentList.add(ContentFragment.newInstance("junshi")); fragmentList.add(ContentFragment.newInstance("keji")); fragmentList.add(ContentFragment.newInstance("caijing")); fragmentList.add(ContentFragment.newInstance("shishang")); tbl.setTabMode(TabLayout.MODE_SCROLLABLE); vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return stringList.get(position); } }); tbl.setupWithViewPager(vp); } }
Layout
<?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" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tbl" android:layout_width="match_parent" android:layout_height="50dp"></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
4.ContentFragment
package com.example.lenovo.moni0922.fragment; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; 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 com.example.lenovo.moni0922.HttpUtils; import com.example.lenovo.moni0922.News; import com.example.lenovo.moni0922.NewsAdapter; import com.example.lenovo.moni0922.R; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; /** * */ public class ContentFragment extends Fragment{ private ListView lvNews; private String str; private List<News.DataBean> list; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_content,container,false); lvNews = v.findViewById(R.id.lv_news); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); str = bundle.getString("s"); String url = "http://www.xieast.com/api/news/news.php?type="+str+"&page="; list = new ArrayList<>(); final NewsAdapter adapter = new NewsAdapter(getActivity(),list); lvNews.setAdapter(adapter); new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... strings) { return HttpUtils.getHttpUrlConnetion(strings[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); News news = gson.fromJson(s, News.class); list.clear(); list.addAll(news.getData()); adapter.notifyDataSetChanged(); } }.execute(url); } public static ContentFragment newInstance(String s){ ContentFragment contentFragment = new ContentFragment(); Bundle bundle = new Bundle(); bundle.putString("s",s); contentFragment.setArguments(bundle); return contentFragment; } }
//Layout
<?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" android:orientation="vertical"> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
NewsAdapter
package com.example.lenovo.moni0922; import android.content.Context; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * */ public class NewsAdapter extends BaseAdapter{ private static final int TYPE_ONE = 0; private static final int TYPE_TWO =1; private static final int TYPE_THREE = 2; private Context context; private List<News.DataBean> list; public NewsAdapter(Context context, List<News.DataBean> list) { this.context = context; this.list = list; } @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 int getViewTypeCount() { return 3; } @Override public int getItemViewType(int position) { News.DataBean bean = list.get(position); if (!TextUtils.isEmpty(bean.getThumbnail_pic_s03())){ return TYPE_THREE; }else if(!TextUtils.isEmpty(bean.getThumbnail_pic_s02())){ return TYPE_TWO; }else { return TYPE_ONE; } } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; ViewHolder3 holder3 = null; int type = getItemViewType(i); switch (type){ case TYPE_THREE: if (view == null){ holder3= new ViewHolder3(); view = View.inflate(context,R.layout.item_2,null); holder3.txtTitle = view.findViewById(R.id.txt_title); holder3.imgLogo31 = view.findViewById(R.id.img_logo31); holder3.imgLogo32 = view.findViewById(R.id.img_logo32); holder3.imgLogo33 = view.findViewById(R.id.img_logo33); view.setTag(holder3); }else { holder3 = (ViewHolder3) view.getTag(); } holder3.txtTitle.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holder3.imgLogo31); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(), holder3.imgLogo32); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s03(), holder3.imgLogo33); break; case TYPE_TWO: if (view == null){ holder2 = new ViewHolder2(); view = View.inflate(context,R.layout.item_1,null); holder2.txtTitle = view.findViewById(R.id.txt_title); holder2.imgLogo21 = view.findViewById(R.id.img_logo21); holder2.imgLogo22 = view.findViewById(R.id.img_logo22); view.setTag(holder2); }else { holder2 = (ViewHolder2) view.getTag(); } holder2.txtTitle.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holder2.imgLogo21); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s02(), holder2.imgLogo22); break; case TYPE_ONE: if (view == null){ holder1 = new ViewHolder1(); view = View.inflate(context,R.layout.item_0,null); holder1.txtTitle = view.findViewById(R.id.txt_title); holder1.imgLogo = view.findViewById(R.id.img_logo); view.setTag(holder1); }else { holder1 = (ViewHolder1) view.getTag(); } holder1.txtTitle.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(), holder1.imgLogo); break; } return view; } class ViewHolder1{ private TextView txtTitle; private ImageView imgLogo; } class ViewHolder2{ private TextView txtTitle; private ImageView imgLogo21; private ImageView imgLogo22; } class ViewHolder3{ private TextView txtTitle; private ImageView imgLogo31; private ImageView imgLogo32; private ImageView imgLogo33; } }