2015年12月17日 星期四

Android-RecyclerView Sample 範例 Kotlin

更新日期:20189116 新增範例( kotlin )

雖然有 ListView 和 GridView 顯示資訊
但還要另外寫選配器
Google 又另外推出 RecyclerView
聞說效率比前 2 個 View 效率更高
話不多說

先來個簡單流程說明吧
1 . 首先設計要顯示表格的畫面內容
2 . 將剛剛設計的內容格式放置選配器內
3 . 將資料放置選配器中
4 . 依照不同的需求顯示

直接來個範例

1 . build.gradle 先匯入以下
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'

2 . 新增元素介面
<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/item_record_1_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/item_record_1_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

3 . 設定頁面參數
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.light.blue.counter.R;

public class MyViewHolder_1 extends RecyclerView.ViewHolder {

    public ImageView mImageView;
    public TextView mTextView;

    public MyViewHolder_1(View view) {
        super(view);
        // 添加預設的模組
        mImageView = view.findViewById(R.id.item_record_1_img);
        mTextView = view.findViewById(R.id.item_record_1_text);
    }
}


4 . 設定選配器
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.light.blue.adpater.view.MyViewHolder_1;
import com.light.blue.counter.R;

import java.util.List;

public class MyAdapter_1 extends RecyclerView.Adapter<MyViewHolder_1> {

    private List<String> mList;

    public MyAdapter_1(List<String> list) {
        mList = list;
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder_1 holder, int position) {
        holder.mTextView.setText(mList.get(position).toString());
    }

    @Override
    public MyViewHolder_1 onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_record_1, viewGroup, false);

        return new MyViewHolder_1(view);
    }

}

5 . 在主畫面宣告
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/recycleView_recyclerList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

6 .
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.light.blue.adpater.MyAdapter_1
import com.light.blue.sql.until.MyDatabaseHandler
import kotlinx.android.synthetic.main.activity_recycleview.*
import java.util.*

class RecycleListActivity : AppCompatActivity() {

    private lateinit var myDatabaseHandler: MyDatabaseHandler;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycleview)

        myDatabaseHandler = MyDatabaseHandler(this);

        var listStr = ArrayList<String>();
        listStr.add("小明");
        listStr.add("小王");
        listStr.add("小黃");
        listStr.add("小黑");
        listStr.add("小白");

        recycleView_recyclerList.layoutManager = LinearLayoutManager(this);
        // recycleView_recyclerList.layoutManager = GridLayoutManager(this, 3);
        recycleView_recyclerList.adapter = MyAdapter_1(listStr);

    }

}


( LinearLayoutManager )






















(GridLayoutManager)




參考資料 : Creating Lists with RecyclerView in Android
參考資料 : RecyclerView使用详解(一)

1 則留言: