Date
Jul. 21st, 2024
 
2024年 6月 16日

Post: Android: RecyclerView 基本使用

Android: RecyclerView 基本使用

Published 12:03 Mar 02, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #Android.

Source format: Markdown

Table of Content

RecyclerView 是 V7 包新增用来替代 ListView 的控件,本文主要介绍它的基本用法。

  • gradle 包引用

(与 CardView 配合使用)。

compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
  • XML 中使用
<android.support.v7.widget.recyclerview
   xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/recycler_view"
   android:layout_centervertical="true"
   android:layout_centerhorizontal="true">
   </android.support.v7.widget.recyclerview>
  • Activity 中设置
public class MainActivity extends ActionBarActivity {
  @InjectView(R.id.recycler_view)
  RecyclerView mRecyclerView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 类似于listview
  // mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 类似于grid view
  // mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
    mRecyclerView.setAdapter(new NormalRecyclerViewAdapter(this));
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
  }
}
  • 适配器代码
public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<normalrecyclerviewadapter.normaltextviewholder> {
  private final LayoutInflater mLayoutInflater;
  private final Context mContext;
  private String[] mTitles;
  public NormalRecyclerViewAdapter(Context context) {
    mTitles = context.getResources().getStringArray(R.array.titles);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context);
  }
  @Override
  public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
  }
  @Override
  public void onBindViewHolder(NormalTextViewHolder holder, int position) {
    holder.mTextView.setText(mTitles[position]);
  }
  @Override
  public int getItemCount() {
    return mTitles == null ? 0 : mTitles.length;
  }

  public static class NormalTextViewHolder extends RecyclerView.ViewHolder {
    @InjectView(R.id.text_view)
    TextView mTextView;
    NormalTextViewHolder(View view) {
        super(view);
        ButterKnife.inject(this, view);
        view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            Log.d("NormalTextViewHolder", "onClick--> position = " + getPosition());
                    }
                });
    }
  }
}</normalrecyclerviewadapter.normaltextviewholder>
Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF