关于AS(Android studio)添加recyclerview控件后的进一步仿微信界面

关于AS(Android studio)添加recyclerview控件后的进一步仿微信界面

要求:在原有基础上添加recyclerview布局
实现页面展示:
在这里插入图片描述
相比较上次,我们添加了删除信息,分类和隐藏缩放的功能。
因此我们需要更新一下我们上次布局里的三个xml文件tab01,tab02,tab03。
分别各自添加一个recyclerview的控件,再分别修改一下id等参数。
然后是Java代码文件里对应的三个Fragment也需要更新。
weixinFragment:

package com.example.Mywechat;


import ...


/**
 * A simple {@link Fragment} subclass.
 */
public class weixinFragment extends Fragment {
    
    

    private RecyclerView recyclerView;
    private List<String> mList = new ArrayList<>();
    private Context context;
    private adapter_swipe adapter;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.tab01, container, false);

        recyclerView=view.findViewById(R.id.rcvone);
        initData();
        initView();

        return view;
    }

    private void initData() {
    
    
        mList.add("Optimus Prime");
        mList.add("Blaster ");
        mList.add("Perceptor");
        mList.add("Hound");
        mList.add("Jazz");
        mList.add("Prowl");
        mList.add("Ratchet ");
        mList.add("Wheeljack");
        mList.add("Bluestreak");
        mList.add("Mirage");
        mList.add("Sideswipe");
        mList.add("Trailbreaker");
        mList.add("Sunstreaker");
        mList.add("Grapple");
        mList.add("Hoist");
        mList.add("Inferno");
        mList.add("Red Alert");
        mList.add("Smokescreen");
        mList.add("Tracks");
        mList.add("Skids");

        mList.add("Megatron");
        mList.add("STARSCREAM");
        mList.add("Barricade");
        mList.add("Bonecrusher");
        mList.add("Brawl");
        mList.add("Blackout");
        mList.add("Scorponok");
        mList.add("Frenzy");
        mList.add("The Fallen");
        mList.add("Alice");
        mList.add("Devastator");
        mList.add("Mixmaster");
        mList.add("Hightower");
        mList.add("Rampage");
        mList.add("Scrapper");
        mList.add("Longhaul");
        mList.add("Demolisher");
        mList.add("Sideways");
        mList.add("Grindor");
        mList.add("Soundwave");
    }

    private void initView(){
    
    
        context=this.getActivity();
        adapter=new adapter_swipe(context,mList);

        ItemTouchHelper.Callback callback = new SwipeItemTouchHelper(adapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recyclerView);

        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
    }

}

FrdFragment:

package com.example.Mywechat;


import ...

/**
 * A simple {@link Fragment} subclass.
 */
public class frdFragment extends Fragment {
    
    

    private static final String TAG = frdFragment.class.getSimpleName();

    private RecyclerView recyclerView;
    private TextView tvArea;
    private List<String> mList = new ArrayList<>();
    private List<StickyData> mDataList = new ArrayList<>();
    private Context context;
    private adapter adapter;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.tab02, container, false);

        recyclerView=view.findViewById(R.id.rcvtwo);
        tvArea=view.findViewById(R.id.tv_sticky_header_view);

        initList();
        initData();
        initView();

        return view;
    }
    private void initList() {
    
    
        mList.add("博派|Optimus Prime");
        mList.add("博派|Blaster ");
        mList.add("博派|Perceptor");
        mList.add("博派|Hound");
        mList.add("博派|Jazz");
        mList.add("博派|Prowl");
        mList.add("博派|Ratchet ");
        mList.add("博派|Wheeljack");
        mList.add("博派|Bluestreak");
        mList.add("博派|Mirage");
        mList.add("博派|Sideswipe");
        mList.add("博派|Trailbreaker");
        mList.add("博派|Sunstreaker");
        mList.add("博派|Grapple");
        mList.add("博派|Hoist");
        mList.add("博派|Inferno");
        mList.add("博派|Red Alert");
        mList.add("博派|Smokescreen");
        mList.add("博派|Tracks");
        mList.add("博派|Skids");
        mList.add("狂派|Megatron");
        mList.add("狂派|STARSCREAM");
        mList.add("狂派|Barricade");
        mList.add("狂派|Bonecrusher");
        mList.add("狂派|Brawl");
        mList.add("狂派|Blackout");
        mList.add("狂派|Scorponok");
        mList.add("狂派|Frenzy");
        mList.add("狂派|The Fallen");
        mList.add("狂派|Alice");
        mList.add("狂派|Devastator");
        mList.add("狂派|Mixmaster");
        mList.add("狂派|Hightower");
        mList.add("狂派|Rampage");
        mList.add("狂派|Scrapper");
        mList.add("狂派|Longhaul");
        mList.add("狂派|Demolisher");
        mList.add("狂派|Sideways");
        mList.add("狂派|Grindor");
        mList.add("狂派|Soundwave");
    }

    private void initData() {
    
    
        for (int i = 0; i < mList.size(); i++) {
    
    
            StickyData bean = new StickyData();

            String s = mList.get(i);
            // area
            String area = s.substring(0, s.indexOf("|"));
            // team
            String team = s.substring(s.indexOf("|") + 1, s.length());

            bean.setArea(area);
            bean.setTeam(team);

            mDataList.add(bean);
        }

        Log.d(TAG, "initData: " + mDataList.size());
    }

    private void initView() {
    
    
        context=this.getActivity();
        adapter=new adapter(context,mDataList);

        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    
    
                super.onScrolled(recyclerView, dx, dy);

                View stickyInfoView = recyclerView.findChildViewUnder(
                        tvArea.getMeasuredWidth() / 2, 5);

                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
    
    
                    tvArea.setText(String.valueOf(stickyInfoView.getContentDescription()));
                }

                View transInfoView = recyclerView.findChildViewUnder(
                        tvArea.getMeasuredWidth() / 2, tvArea.getMeasuredHeight() + 1);

                if (transInfoView != null && transInfoView.getTag() != null) {
    
    

                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - tvArea.getMeasuredHeight();

                    if (transViewStatus == adapter.HAS_STICKY_VIEW) {
    
    
                        if (transInfoView.getTop() > 0) {
    
    
                            tvArea.setTranslationY(dealtY);
                        } else {
    
    
                            tvArea.setTranslationY(0);
                        }
                    } else if (transViewStatus == adapter.NONE_STICKY_VIEW) {
    
    
                        tvArea.setTranslationY(0);
                    }
                }
            }
        });
    }
}

contentFragment:

package com.example.Mywechat;


import ...


/**
 * A simple {@link Fragment} subclass.
 */
public class contactFragment extends Fragment {
    
    

    private RecyclerView recyclerView;
    private List<String> mList = new ArrayList<>();
    private Context context;
    private adapter_expand adapter;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.tab03, container, false);

        recyclerView=view.findViewById(R.id.rcvthere);
        initexpandData();
        initView();

        return view;
    }
    private void initexpandData(){
    
    
        mList.add("Optimus Prime");
        mList.add("Blaster ");
        mList.add("Perceptor");
        mList.add("Hound");
        mList.add("Jazz");
        mList.add("Prowl");
        mList.add("Ratchet ");
        mList.add("Wheeljack");
        mList.add("Bluestreak");
        mList.add("Mirage");
        mList.add("Sideswipe");
        mList.add("Trailbreaker");
        mList.add("Sunstreaker");
        mList.add("Grapple");
        mList.add("Hoist");
        mList.add("Inferno");
        mList.add("Red Alert");
        mList.add("Smokescreen");
        mList.add("Tracks");
        mList.add("Skids");

        mList.add("Megatron");
        mList.add("STARSCREAM");
        mList.add("Barricade");
        mList.add("Bonecrusher");
        mList.add("Brawl");
        mList.add("Blackout");
        mList.add("Scorponok");
        mList.add("Frenzy");
        mList.add("The Fallen");
        mList.add("Alice");
        mList.add("Devastator");
        mList.add("Mixmaster");
        mList.add("Hightower");
        mList.add("Rampage");
        mList.add("Scrapper");
        mList.add("Longhaul");
        mList.add("Demolisher");
        mList.add("Sideways");
        mList.add("Grindor");
        mList.add("Soundwave");
    }

    private void initView(){
    
    
        context=this.getActivity();
        adapter=new adapter_expand(context,mList);

        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
    }
}

参考网址:https://www.jianshu.com/p/c4cfe38a91ed
https://developer.android.google.cn/guide/topics/ui/layout/recyclerview#java
源代码已经上传到gitee:https://gitee.com/milk-sugar-is-not-sweet/as/blob/master/Myweat%20Recyclerview/Myweat%20Recyclerview.rar

猜你喜欢

转载自blog.csdn.net/weixin_46027889/article/details/109431911