Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——ViewPager+Fragment+ListView之間

Android——ViewPager+Fragment+ListView之間

編輯:關於Android編程

\

 

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 list=new ArrayList<>();
        List 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);

    }
}


    


        

        

        

    


    
    
        
        
        
    
    

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 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 android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.jreduch05.fragment.VpFragmentLv" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    
    

    

</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 CREATOR = new Creator() {
        @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 list;
    private  List titles;
    public MyVpFragmentAdapter(FragmentManager fm, List list, List 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="" bundle="" tabhost.tabspec="" tabspec="ft.newTabSpec(str[i]).setIndicator(getView(i));" mytabhostadapter="" mha="new" new="" override="" public="" void="" string="" int="" float="" view="" v="getLayoutInflater().inflate(R.layout.tab_layout,null);" imageview="" iv="(ImageView)v.findViewById(R.id.iv);" textview="" tv="(TextView)v.findViewById(R.id.tv);" return="" pre="">



    
    

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">

    
    

</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 list;
    public MyTabHostAdapter(FragmentManager fm, List list) {
        super(fm);
        this.list= list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }
}


    
    

    
\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved