团队项目——和谐共生(1)

由于这学期的时间比较紧迫,所以是边学边做

今天学习了ViewPager和fragment,使用这两个知识做出了我们app的首页,效果图是这样的

 

项目结构目前是这样的

 

 核心代码:

package com.example.peace;
//登录之后进入的界面
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

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

public class BrowerActivity extends AppCompatActivity implements View.OnClickListener {
    ViewPager2 viewPager;
    private LinearLayout lbrow,ldialog,lgame;
    private ImageView ivBrow,ivDialog,ivGame,ivCurrent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brower);

        //初始化ViewPager
        initPager();
        initTabView();
    }

    private void initTabView() {
        lbrow=findViewById(R.id.tab_brow);
        lbrow.setOnClickListener(this);
        ldialog=findViewById(R.id.tab_dialog);
        ldialog.setOnClickListener(this);
        lgame=findViewById(R.id.tab_game);
        lgame.setOnClickListener(this);
        ivBrow=findViewById(R.id.tab_iv_brow);
        ivDialog=findViewById(R.id.tab_iv_dialog);
        ivGame=findViewById(R.id.tab_iv_game);

        ivBrow.setSelected(true);
        ivCurrent=ivBrow;
    }

    private void initPager() {
        viewPager=findViewById(R.id.viewpager);
        ArrayList<Fragment> fragments=new ArrayList<>();
        fragments.add(BlankFragment.newInstance("浏览"));
        fragments.add(BlankFragment.newInstance("讨论"));
        fragments.add(BlankFragment.newInstance("娱乐一把"));
        PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        switch (position){
            case R.id.tab_brow:
                viewPager.setCurrentItem(0);
            case 0:ivBrow.setSelected(true);
                ivCurrent=ivBrow;
                break;
            case R.id.tab_dialog:
                viewPager.setCurrentItem(1);
            case 1:ivDialog.setSelected(true);
                ivCurrent=ivDialog;
                break;
            case R.id.tab_game:
                viewPager.setCurrentItem(2);
            case 2:ivGame.setSelected(true);
                ivCurrent=ivGame;
                break;
        }
    }

    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}
package com.example.peace;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    View rootView;

    // TODO: Rename and change types of parameters
    private String mText;

    public BlankFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mText = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (rootView==null){
            rootView = inflater.inflate(R.layout.fragment_blank, container, false);
        }
        initView();
        return rootView;
    }

    private void initView() {
        TextView textView=rootView.findViewById(R.id.text);
        textView.setText(mText);
    }
}
<include layout="@layout/bottom_layout"/>
<!--必须要在MainActivity中引入bottom文件-->

 

posted on 2022-04-18 22:02  跨越&尘世  阅读(36)  评论(0编辑  收藏  举报