記一次Android完整項目(4) RecyclerView線性佈局

RecyclerView

使用的依賴和權限 : 在app/build.gradle中的dependencies閉包添加如下內容:java

implementation 'androidx.recyclerview:recyclerview:1.1.0'
複製代碼

RecyclerView 基本用法

修改 activity_main.xml: 因爲RecyclerView不是內置在系統SDK中,須要把其完整的包名路徑寫出來
<LinearLayout 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="match_parent"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
</LinearLayout>
複製代碼

RecyclerView 線性佈局

1.在 xml 佈局文件中添加
<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
複製代碼
2.在 對應的.java 文件中初始化RecyclerView
/**
         * 1.假設已經知道列表高度,可直接定義列表高度
         * 2.不知道列表高度,須要手動計算RecyclerView高度
         */
        mRvList = fd(R.id.rv_list);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRvList.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRvList.setNestedScrollingEnabled(false);// 限制每一個單獨的RecyclerView滑動
        mListAdapter = new MusicListAdapter(this,mRvList,mMusicSourceModel.getHot());
        mRvList.setAdapter(mListAdapter);
複製代碼
補充:.setLayoutManager() 用法

效果一:rv.setLayoutManager(new LinearLayoutManager(this)); 這樣設置的效果是組件垂直往下android

效果二: 修改LinearLayoutManager(this)裏面的參數,第二個參數表示水平佈局,第三個參數表示是否反轉,就會呈現出另外一個效果bash

rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));網絡

效果三: 在效果二的基礎上,咱們把第三個參數反轉改一下,改成true,看看有什麼效果閉包

rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true));app

咱們發現item0在最右邊了,並且一樣能夠左右滑動ide

效果四:表格佈局,第一個參數表示上下文,第二個參數表示表格有多少列 .rv.setLayoutManager(new GridLayoutManager(this,3)); 能夠上下拖動,表格爲3列函數

效果五:在表格佈局中還能夠修改參數,使其水平佈局,那麼那個數字就表明着有幾行了,一樣最後一個參數表示反轉,我這裏就不演示了佈局

rv.setLayoutManager(new GridLayoutManager(this,3,GridLayoutManager.HORIZONTAL,false));gradle

圖中分爲三列,能夠左右拖動

3.建立一個新的適配器 Adapter
/** * @author iwenLjr * Create to 2020/2/20 22:49 */
public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {

    private Context mContext;
    private View mItemView;
    private RecyclerView mRv;
    private boolean isCalcaulationRvHeight;
    private List<MusicModel> mDataSource;
	
    // 給函數一個構造方法
    public MusicListAdapter(Context context, RecyclerView recyclerView,List<MusicModel> dataSource) {
        mContext = context;
        mRv = recyclerView;
        this.mDataSource = dataSource;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 添加適配器佈局
        mItemView = LayoutInflater.from(mContext).inflate(R.layout.item_list_music, parent, false);
        return new ViewHolder(mItemView);
    }
	// 綁定數據
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        setRecyclerViewHeight();

        final MusicModel musicModel = mDataSource.get(position);

        // 展現網絡圖片
        Glide.with(mContext)
                .load(musicModel.getPoster())
                .into(holder.ivIcon);

        holder.tvName.setText(musicModel.getName());
        holder.tvAuthor.setText(musicModel.getAuthor());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PlayMusicActivity.class);
                intent.putExtra(PlayMusicActivity.MUSIC_ID,musicModel.getMusicId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataSource.size();
    }

    /** * 自定義RecyclerView高度 * * 1.獲取ItemView高度 * 2.獲取ItemView數量 * 3.計算高度:使用ItemViewHeight * ItemViewNum */
    private void setRecyclerViewHeight() {

        if (isCalcaulationRvHeight || mRv == null) return;

        isCalcaulationRvHeight = true;

        // 獲取itemView高度
        RecyclerView.LayoutParams itemViewLp = (RecyclerView.LayoutParams) mItemView.getLayoutParams();

        // 獲取ItemView數量
        int itemCount = getItemCount();

        // 計算
        int recyclerViewHeight = itemViewLp.height * itemCount;

        // 設置RecyclerView高度
        LinearLayout.LayoutParams rvLp = (LinearLayout.LayoutParams) mRv.getLayoutParams();
        rvLp.height = recyclerViewHeight;
        mRv.setLayoutParams(rvLp);

    }
	// 先獲取ViewHolder
    class ViewHolder extends RecyclerView.ViewHolder {

        View itemView;
        ImageView ivIcon;
        TextView tvName,tvAuthor;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
			// 綁定視圖
            this.itemView = itemView;
            ivIcon = itemView.findViewById(R.id.iv_icon_list);
            tvName = itemView.findViewById(R.id.tv_name);
            tvAuthor = itemView.findViewById(R.id.tv_author);
        }
    }
}
複製代碼
4.在 res-layout 下面建立一個佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_commit_selector"
    android:orientation="horizontal"
    android:padding="@dimen/marginSize">


    <ImageView
        android:id="@+id/iv_icon_list"
        android:layout_width="@dimen/itemIconSize"
        android:layout_height="@dimen/itemIconSize"
        android:src="@mipmap/img1" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/marginSize"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="音樂名稱"
            android:textColor="@color/titleColor"
            android:textSize="@dimen/titleSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="做者"
            android:textColor="@color/infoColorH"
            android:textSize="@dimen/infoSize" />


    </LinearLayout>

    <ImageView
        android:layout_width="@dimen/itemPlaySize"
        android:layout_height="@dimen/itemPlaySize"
        android:src="@mipmap/play"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/marginSize"/>

</LinearLayout>
複製代碼
相關文章
相關標籤/搜索