android使用RecyclerView思路布局主页

2021/11/6 23:42:40

本文主要是介绍android使用RecyclerView思路布局主页,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

效果图

在这里插入图片描述

HomeFragmentRv

package com.coral3.ah.ui.fragment.tabbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.coral3.ah.R;
import com.coral3.ah.adapter.HomeRvAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * @description Rv制作主页
 * @author  WTY
 * @date 2021/11/06/11/05
 */
public class HomeFragmentRv extends Fragment {

    private static final String ARG_TEXT = "params1";
    private String mTextStr;
    private Activity mActivity;
    private RecyclerView rvHome;
    private View root;
    private HomeRvAdapter RvAdapter;
    private List<String> data = new ArrayList<>();

    public HomeFragmentRv() {}

    public static HomeFragmentRv newInstance(String params) {
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TEXT, params);
        HomeFragmentRv fragment = new HomeFragmentRv();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void initView(){
        initRv();

    }

    private void initRv() {
        rvHome = root.findViewById(R.id.rv_home);
        RvAdapter = new HomeRvAdapter(mActivity);
        rvHome.setAdapter(RvAdapter);
        rvHome.setLayoutManager(new GridLayoutManager(mActivity, 1));
    }

    private void initListener(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(root == null) root = inflater.inflate(R.layout.fragment_home_rv, container, false);
        mActivity = getActivity();
        initView();
        initListener();
        initData();
        return root;
    }

    private void initData() {
        getData(true);
    }

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

    private void getData(Boolean isFirstLoad){
        if(isFirstLoad){
            data.clear();
            data.add("陈");
        }else{

        }
    }
}

fragment_home_rv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/rv_home"
       app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</RelativeLayout>

HomeRvAdapter

ppackage com.coral3.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.coral3.ah.R;
import com.coral3.common_module.components.BannerLayout;

public class HomeRvAdapter extends RecyclerView.Adapter {
	/*
	视图映射
	 */
	private LayoutInflater meLayoutInflater;
	/*
	当前holder类型
	 */
	private int currentType = 0;
	/*
	上下文
	 */
	private Context mContext;
	public static final int BANNER = 0; // 轮播
	public static final int CHANNEL = 1; // 频道

	public HomeRvAdapter(Context context) {
		meLayoutInflater = LayoutInflater.from(context);
		mContext = context;
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if(viewType == BANNER){
			return new BannerViewHolder(mContext, meLayoutInflater.inflate(R.layout.layout_home_banner, null));
		}
		if(viewType == CHANNEL){
			return new ChannelViewHolder(mContext, meLayoutInflater.inflate(R.layout.layout_home_channel, null));
		}
		return null;
	}

	@Override
	public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

	}

	@Override
	public int getItemViewType(int position) {
		switch (position){
			case BANNER:
				currentType = BANNER;
				break;
			case CHANNEL:
				currentType = CHANNEL;
				break;
		}
		return currentType;
	}

	public class BannerViewHolder extends RecyclerView.ViewHolder{

		private Context mContext;
		private View itemView;
		private BannerLayout homeBannerRv;

		public BannerViewHolder(Context context, @NonNull View view) {
			super(view);
			mContext = context;
			itemView = view;
			// 拿到轮播视图就可设置数据等操作
			homeBannerRv = view.findViewById(R.id.home_banner_rv);
		}
	}

	public class ChannelViewHolder extends RecyclerView.ViewHolder{

		private Context context;

		public ChannelViewHolder(Context context, @NonNull View channelView) {
			super(channelView);
			this.context = context;
		}
	}

	@Override
	public int getItemCount() {
		return 2; // 主页有多少项就改为相应数值
	}
}



主要代码就这些 剩余自己决定



这篇关于android使用RecyclerView思路布局主页的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程