關於AS(Android studio)新增recyclerview控制元件後的進一步仿微信介面

牛奶糖不甜發表於2020-11-01

關於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

相關文章