在Activity上加载Fragment实现点击切换页面

在Activity上加载Fragment实现点击切换Fragment

没有ViewPager的帮助,单凭Fragment实现滑动比较困难,这里我就只给大家说一下如何实现点击切换。
首先是xml的布局:
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"
    tools:context="com.example.wtl.bk1.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/acttitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#82FFF0">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="center"
            android:orientation="vertical">

            <TextView   //这里是点击事件1
                android:id="@+id/frag1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关注"
                android:paddingBottom="5dp"
                android:paddingTop="10dp"
                android:textSize="20sp"
                android:textColor="#E51C23"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:layout_gravity="center">

            <TextView    //这里是点击事件2
                android:id="@+id/frag2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="推荐"
                android:paddingBottom="5dp"
                android:paddingTop="10dp"
                android:textSize="20sp"
                android:textColor="#343434"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="center"
            android:orientation="vertical">

            <TextView   //这里是点击事件3
                android:id="@+id/frag3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="同城"
                android:paddingBottom="5dp"
                android:paddingTop="10dp"
                android:textSize="20sp"
                android:textColor="#343434"/>

        </LinearLayout>

    </LinearLayout>

    <FrameLayout    //FrameLayout加载你想要加入的Fragment
        android:id="@+id/frag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

这里我写了一个recyclerview让他在不同的界面显示不同的内容,如果有需要则用三个界面分别加载
recyclerview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

最后是recyclerview的内容:(只有一个简单的textview)
recycler.card:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/text"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="26sp" />

</LinearLayout>

接下来是实现三个Fragment:
对应recycler_card文件的类:
Text.java:

public class Text {

    private String text; 

    public Text(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

recyclerview的适配器:

public class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> {

    private List<Text> textList;   //存储Text对象的list
    private Context context;       //获取fragment的context

    public TextAdapter(List<Text> textList,Context context) {
        this.textList = textList;
        this.context = context;
    }
    @Override
    public TextAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //传入recyclerview的布局文件
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_card,null,false);
        ViewHolder viewHolder = new ViewHolder(view);//为ViewHolder添加view
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final TextAdapter.ViewHolder holder, int position) {
        /*
        * 获取当前位置的对象
        * */
        final Text text = textList.get(position);
        /*
        * 为textview添加数据
        * */
        holder.text.setText(text.getText());
    }
    @Override
    public int getItemCount() {
        return textList.size();
    }
    static class ViewHolder extends RecyclerView.ViewHolder { //创建一个继承RecyclerView.ViewHolder的类
        TextView text;  //布局文件中的控件
        public ViewHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.text);
        }
    }
}

分别实现三个Fragment,这里只给大家展示一个,其他的都一样只是加载内容不同:
Fragment1:

public class Fragment1 extends Fragment {

    private List<Text> list = new ArrayList<>();
    private RecyclerView recycleview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview,container,false);//给Fragment加载界面
        init();
        recycleview = (RecyclerView) view.findViewById(R.id.recycleview);  //获取xml文件的recyclerview
        LinearLayoutManager manager = new LinearLayoutManager(getContext());  //给recyclerview设置线性布局
        recycleview.setLayoutManager(manager);  //将线性布局加载到recyclerview中
        TextAdapter adapter = new TextAdapter(list,getContext());  //设置你写的适配器
        recycleview.setAdapter(adapter); //加载
        return view;
    }
    //给list添加数据
    private void init() {
        Text text = new Text("页面1");
        list.add(text);
    }
}

最后是在activity中加载三个Fragment
MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //三个加载的fragment
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;

    //三个对应的按钮
    private TextView frag1;
    private TextView frag2;
    private TextView frag3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Montior();
        change_fragment(0);//选择fragment
    }

    //从xml中获取所有部件
    private void Montior() {
        frag1 = (TextView) findViewById(R.id.frag1);
        frag2 = (TextView) findViewById(R.id.frag2);
        frag3 = (TextView) findViewById(R.id.frag3);

        frag1.setOnClickListener(this);
        frag2.setOnClickListener(this);
        frag3.setOnClickListener(this);
    }

    //三个按钮的点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.frag1:
                change_fragment(0);
                //改变点击时Textview的颜色
                frag1.setTextColor(0xffE51C23);
                frag2.setTextColor(0xff343434);
                frag3.setTextColor(0xff343434);
                break;
            case R.id.frag2:
                change_fragment(1);

                frag1.setTextColor(0xff343434);
                frag2.setTextColor(0xffE51C23);
                frag3.setTextColor(0xff343434);
                break;
            case R.id.frag3:
                change_fragment(2);

                frag1.setTextColor(0xff343434);
                frag2.setTextColor(0xff343434);
                frag3.setTextColor(0xffE51C23);
                break;
        }
    }

    private void change_fragment(int x) {
        FragmentManager manager = getSupportFragmentManager();  //初始化FragmentManager,获取v4包所支持的fragment
        FragmentTransaction transaction = manager.beginTransaction(); //初始化FragmentTransaction
        hide_fragment(transaction);//隐藏当前fragment
        switch (x) {
            case 0:
                if(fragment1 == null) {
                    fragment1 = new Fragment1();  //如果fragment1未初始化,进行初始化
                    transaction.add(R.id.frag,fragment1); //将fragment1和对应的布局加入栈中
                } else {  
                    transaction.show(fragment1);  //如果fragment1已经初始化,则展示
                }
                break;
            case 1:
                if(fragment2 == null) {
                    fragment2 = new Fragment2();
                    transaction.add(R.id.frag,fragment2);
                } else {
                    transaction.show(fragment2);
                }
                break;
            case 2:
                if(fragment3 == null) {
                    fragment3 = new Fragment3();
                    transaction.add(R.id.frag,fragment3);
                } else {
                    transaction.show(fragment3);
                }
                break;
        }
        transaction.commit();//之前对FragmentTransaction进行添加展示等操作,这里调用commit进行提交
    }

    //如果fragment不为空则隐藏
    private void hide_fragment(FragmentTransaction transaction) {
        if(fragment1 != null) {
            transaction.hide(fragment1);
        }
        if(fragment2 != null) {
            transaction.hide(fragment2);
        }
        if(fragment3 != null) {
            transaction.hide(fragment3);
        }
    }
}

现在已经可以通过点击切换fragment了,但如果想要实现滑动效果,则还是要用到ViewPager,这里就不给大家详述了。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值