RecyclerViewについて

特徴として
  • ListViewをさらに進化させて柔軟にしたもの
  • 大きなデータセットを表示できるコンテナ
  • 限られたビューを維持して効率的にスクロール可能
  • ヘッダー、フッターは存在しない
  • 区切り線は自身で実装する必要がある
 
というわけで、早速RecyclerViewを使ってみよう!
色々と調べたが、ぶっちゃけ他のサイトで紹介しているものは複雑すぎるか
ビルドエラーが発生するものばかりである。
 
そこで、今回こちらでわかりやすくするように書き換えた。
 

 
Activity_mainのDesignを開こう

そうすると、しばらくすると必要な情報が読み込まられるので、ここまでやる
 
<?xml version=“1.0” encoding=utf-8?>
    xmlns:tools=http://schemas.android.com/tools
    android:layout_width=“match_parent”
    android:layout_height=“match_parent”
    tools:context=“.MainActivity”>
 
    <TextView
        android:layout_width=“wrap_content”
        android:layout_height=“wrap_content”
        android:text=Hello World!”
        app:layout_constraintBottom_toBottomOf=“parent”
        app:layout_constraintLeft_toLeftOf=“parent”
        app:layout_constraintRight_toRightOf=“parent”
        app:layout_constraintTop_toTopOf=“parent” />
 
    <android.support.v7.widget.RecyclerView
        android:layout_width=“368dp”
        android:layout_height=“231dp”
        tools:layout_editor_absoluteX=“8dp”
        tools:layout_editor_absoluteY=“8dp” />
 
 
 
 
こうなると思うけど、TextViewとidを付け加える
 
<?xml version=“1.0” encoding=utf-8?>
    xmlns:tools=http://schemas.android.com/tools
    android:layout_width=“match_parent”
    android:layout_height=“match_parent”
    tools:context=“.MainActivity”>
 
    <android.support.v7.widget.RecyclerView
        android:id=“@+id/recycler_view”
        android:layout_width=“368dp”
        android:layout_height=“231dp”
        tools:layout_editor_absoluteX=“8dp”
        tools:layout_editor_absoluteY=“8dp” />
 
 
 
public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
 
    private String myDataset = new String[20];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mRecyclerView = findViewById(R.id.recycler_view);
 
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);
 
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
 
        mRecyclerView.setLayoutManager(mLayoutManager);
 
        for(int i=0; i<myDataset.length; i++) {
            myDataset[i] = “Data_0”+String.valueOf(i);
        }
 
        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
}
 
 
MianActivityをこのように書き換えます。
 
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class RecyclerViewAdapter extends RecyclerView.Adapter {
 
    private String mDataset = new String[20];
 
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
 
        // each data item is just a string in this case
        public TextView mTextView;
 
        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView)v.findViewById(R.id.text_view);
        }
    }
 
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String myDataset) {
        mDataset = myDataset;
    }
 
    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false);
 
        // set the view’s size, margins, paddings and layout parameters
 
 
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
 
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // – get element from your dataset at this position
        // – replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);
    }
 
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
 
 
RecyclerViewAdapter.javaというクラスを作成します。
 
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class RecyclerViewAdapter extends RecyclerView.Adapter {
 
    private String mDataset = new String[20];
 
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
 
        // each data item is just a string in this case
        public TextView mTextView;
 
        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView)v.findViewById(R.id.text_view);
        }
    }
 
    // Provide a suitable constructor (depends on the kind of dataset)
    public RecyclerViewAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
 
    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row, parent, false);
 
        // set the view’s size, margins, paddings and layout parameters
 
 
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
 
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // – get element from your dataset at this position
        // – replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);
    }
 
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
 
 
row.xmlというファイルを作り、このように記載します。
 

 
<?xml version=“1.0” encoding=utf-8?>
    android:layout_width=“match_parent”
    android:layout_height=“wrap_content”>
 
    <TextView
        android:id=“@+id/text_view”
        android:layout_width=“wrap_content”
        android:layout_height=“50dp” />
 
 
 

ビルドして、こんな風に表示されれば成功です。
 
 
 
 
リストの背景色を変更する
 
これとかよくわかりそう→バグってるぞクソ
 
CardViewというのもあるぞ
 
これ最高!
 
横スワイプで削除
 
onBindeの使い方とか
 
CardViewにリストを挿入
 
 
 
 

  1. Superb, what a web site it is! This web site presents helpful information to us, keep it up.

  2. I just couldn’t depart your web site prior to suggesting that I
    actually enjoyed the usual information a person provide for your visitors?

    Is going to be again ceaselessly to inspect new posts

  3. Agen Bandarq より:

    Thanks for a marvelous posting! I seriously enjoyed reading it,
    you can be a great author. I will always bookmark your blog and
    definitely will come back later on. I want to encourage one to
    continue your great work, have a nice day!

  4. Imogen Marlar より:

    Does your website have a contact page? I’m having problems locating it but,
    I’d like to shoot you an e-mail. I’ve got some recommendations for your blog
    you might be interested in hearing. Either way,
    great website and I look forward to seeing it expand over time.

  5. Please let me know if you’re looking for a article author for your weblog.
    You have some really great posts and I believe I would be a good asset.
    If you ever want to take some of the load off,
    I’d love to write some articles for your blog in exchange for a
    link back to mine. Please blast me an e-mail if interested.
    Cheers!

タイトルとURLをコピーしました