您的位置:首页 > 移动开发 > 微信开发

仿微信界面滑动(9.7)

2015-09-07 10:18 453 查看

主布局文件activity_main.xml

<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"
  >
    <android.support.v4.view.ViewPager
        android:id="@+id/fragment_viewPager"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="match_parent"></android.support.v4.view.ViewPager>
    <!--<fragment-->
    <!-- -->
        <!--class="com.test.dukang.myfragment.fragment.MyFirstFragment"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"/>-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></FrameLayout>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:gravity="center">

            <RadioButton
                android:id="@+id/radioButton1"
                android:drawableTop="@drawable/msg_background"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                style="@style/textStyle"
               />
            <RadioButton
                android:id="@+id/radioButton2"
                android:drawableTop="@drawable/contact_background"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                style="@style/textStyle"/>
            <RadioButton
                android:id="@+id/radioButton3"
                android:drawableTop="@drawable/kongjian_background"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                style="@style/textStyle"/>
        </RadioGroup>

</LinearLayout>


其他布局文件

"**********************第一个布局fragment_test**********************"
<?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">
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

"**********************第二个布局fragment_second**********************"
<?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">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好吗?"
        android:textSize="24sp"/>
</LinearLayout>

"**********************第三个布局fragment_thrid**********************"
<?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">
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好啊,好啊"/>
</LinearLayout>


主程序MainActivity

package com.test.dukang.myfragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.test.dukang.myfragment.fragment.MyFirstFragment;
import com.test.dukang.myfragment.fragment.MySecondFragment;
import com.test.dukang.myfragment.fragment.MyThridFragment;
import com.test.dukang.myfragment.fragment.com.test.adapter.MyFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private RadioGroup mRadioGroup;
    private FragmentManager mFragmentManager;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private FragmentTransaction transaction;
    private MySecondFragment fragment2;
    private MyFirstFragment fragment;
    private MyThridFragment fragment3;
    private ViewPager mViewPager;
    private List<Fragment> mFragmentList;
    private MyFragmentAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mViewPager= (ViewPager) findViewById(R.id.fragment_viewPager);

        mFragmentManager = getSupportFragmentManager();
        mFragmentList=new ArrayList<>();
        fragment = new MyFirstFragment();
        fragment2 = new MySecondFragment();
        fragment3 = new MyThridFragment();
        mFragmentList.add(fragment);
        mFragmentList.add(fragment2);
        mFragmentList.add(fragment3);
        mAdapter= new  MyFragmentAdapter(mFragmentManager,mFragmentList);
        mViewPager.setAdapter(mAdapter);

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonId = group.getCheckedRadioButtonId();
                switch (radioButtonId) {
                    case R.id.radioButton1:
                        mViewPager.setCurrentItem(0);

                        break;
                    case R.id.radioButton2:
                        mViewPager.setCurrentItem(1);

                        break;
                    case R.id.radioButton3:
                        mViewPager.setCurrentItem(2);

                        break;
                    default:
                        Toast.makeText(getApplication(), "sd", Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            switch (position){
                case 0:

                    break;
                case 1:

                    break;
                case 2:

                    break;
            }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

}


MyFragmentPagerAdapter

package com.test.dukang.myfragment.fragment.com.test.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 2015/9/7.
 */
public class MyFragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> mList;
    public MyFragmentAdapter(FragmentManager fm,List<Fragment> mList) {
        super(fm);
        this.mList=mList;
    }

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

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


其他Fragment程序

"*********************MyFirstFragment********************"
package com.test.dukang.myfragment.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.Button;
import android.widget.Toast;

import com.test.dukang.myfragment.R;

/**
 * Created by Administrator on 2015/9/6.
 */
//导入support.v4的包,注意这是重点
public class MyFirstFragment extends Fragment {
    private Button button;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_test,null);
        button= (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"你好",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

"*********************MySecondFragment********************"
package com.test.dukang.myfragment.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.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.test.dukang.myfragment.R;

/**
 * Created by Administrator on 2015/9/6.
 */
public class MySecondFragment extends Fragment {
    private TextView mTextView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_second,null);
        mTextView= (TextView) view.findViewById(R.id.textView);

        return view;
    }
    public void setmTextView(String text){
        this.mTextView.setText(text);
    }
}

"*********************MyThridFragment********************"
package com.test.dukang.myfragment.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.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.test.dukang.myfragment.R;

/**
 * Created by Administrator on 2015/9/6.
 */
public class MyThridFragment  extends Fragment {
    private EditText mEditText;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_thrid,null);
        mEditText= (EditText) view.findViewById(R.id.editText);
        return view;
    }
    public String getText(){
        String s=null;
        if(mEditText!=null){
            s=mEditText.getText().toString();

        }
        return s;
    }
}


效果图



FragmentStatePagerAdapter

查看API

当界面较多时,使用该方法来避免出错,具体用法查看api
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: