非常簡單的viewpager+fragmen實作類別似商城app結構

來源:互聯網
上載者:User

標籤:viewpager   fragment   

    先說下思路在一個activity中的主布局加入supportv4架包中的viewpager,viewpager相當於一個listview,在viewpager中我們要放的fragment,對viewpager做監聽,實現想要的效果,代碼很簡單一看就懂,一直上代碼。

   主類

package com.example.fragtest;import java.util.ArrayList;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.TextView;import android.os.Build;public class MainActivity extends FragmentActivity {private ArrayList<Fragment> list;private ViewPager viewPager;private TextView [] tvs=new TextView[4];private int pos=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager=(ViewPager) findViewById(R.id.viewPager);tvs[0]=(TextView) findViewById(R.id.tv_f);tvs[0].setBackgroundColor(0x99999999);tvs[1]=(TextView) findViewById(R.id.tv_s);tvs[2]=(TextView) findViewById(R.id.tv_t);tvs[3]=(TextView) findViewById(R.id.tv_tf);First mFirst=new First();Second msSecond=new Second();third mThird=new third();fourth mFourth=new fourth();list=new ArrayList<Fragment>();list.add(mFirst);list.add(msSecond);list.add(mThird);list.add(mFourth);viewPager.setAdapter(new MyFragAdapter(getSupportFragmentManager(), this, list));viewPager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stubfor(int i=0;i<4;i++){if(i==arg0){tvs[i].setBackgroundColor(0x99999999);}else{tvs[i].setBackgroundColor(0xffffffff);}}}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}});for(TextView tv:tvs){tv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {// TODO Auto-generated method stubswitch (view.getId()) {case R.id.tv_f:pos=0;break;case R.id.tv_s:pos=1;break;case R.id.tv_t:pos=2;break;case R.id.tv_tf:pos=3;break;default:break;}for(int i=0;i<4;i++){if(i==pos){tvs[i].setBackgroundColor(0x99999999);}else{tvs[i].setBackgroundColor(0xffffffff);}}viewPager.setCurrentItem(pos);}});}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
adapter

package com.example.fragtest;import java.util.ArrayList;import android.content.Context;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;public class MyFragAdapter extends FragmentPagerAdapter{private Context mContext;  private ArrayList<Fragment> mlist;public MyFragAdapter(FragmentManager fm,Context context,ArrayList<Fragment> list) {super(fm);mContext=context;if(list!=null){mlist=list;}else{mlist=new ArrayList<Fragment>();}// TODO Auto-generated constructor stub}@Overridepublic Fragment getItem(int arg0) {// TODO Auto-generated method stubreturn mlist.get(arg0);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mlist.size();}}

四個fragment

package com.example.fragtest;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.TextView;public class First extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view=inflater.inflate(R.layout.view, container, false);TextView tv=(TextView) view.findViewById(R.id.tv_name);tv.setText("第一個");return view;}}

package com.example.fragtest;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.TextView;public class Second extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view=inflater.inflate(R.layout.view, container, false);TextView tv=(TextView) view.findViewById(R.id.tv_name);tv.setText("第二個");return view;}}

package com.example.fragtest;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.TextView;public class third extends Fragment{public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view=inflater.inflate(R.layout.view, container, false);TextView tv=(TextView) view.findViewById(R.id.tv_name);tv.setText("第三個");return view;}}

package com.example.fragtest;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.TextView;public class fourth extends Fragment{public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view=inflater.inflate(R.layout.view, container, false);TextView tv=(TextView) view.findViewById(R.id.tv_name);tv.setText("第四個");return view;}}

主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <android.support.v4.view.ViewPager
        android:layout_above="@+id/ll_bottom"
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
        <LinearLayout 
    android:id="@+id/ll_bottom"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="50dp">
   <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#999999" />
   <LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
     <TextView
         android:id="@+id/tv_f"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="首頁"
            android:textSize="16sp" />
       <TextView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#999999" />
     
      <TextView
          android:id="@+id/tv_s"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="熱帖"
            android:textSize="16sp" />
        <TextView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#999999" />
      
       <TextView
           android:id="@+id/tv_t"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="活動"
            android:textSize="16sp" />
         <TextView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#999999" />
       
        <TextView
            android:id="@+id/tv_tf"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="我"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>


</RelativeLayout>


 fragment布局

   

<?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" >    <TextView     android:id="@+id/tv_name"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:textSize="25sp"    android:text="第一個"/></LinearLayout>
    http://download.csdn.net/detail/u012303938/8550513


非常簡單的viewpager+fragmen實作類別似商城app結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.