RecyclerView

Posted cnchengv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView相关的知识,希望对你有一定的参考价值。

1.在activity_main.xml里加入

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv_list"
        />

2.在MainActivity中

    private RecyclerView mRecyclerView;
    private ArticleAdapter mArticleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(llm);
        mArticleAdapter=new ArticleAdapter(/*this,*/llm);
        mRecyclerView.setAdapter(mArticleAdapter);
}

 

然后创建跟item相关的

3 card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:transitionName="open_mediaplayer"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="3dp">

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

            <TextView
                android:id="@+id/file_name_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="file_name"
                android:textSize="15sp"
                android:fontFamily="sans-serif-condensed"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/file_length_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="00:00"
                android:textSize="12sp"
                android:fontFamily="sans-serif-condensed"
                android:layout_marginTop="7dp"/>


    </LinearLayout>

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

</LinearLayout>

4.ArticleAdapter.java

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {

    private List<String> mStringList;
    //Context mContext;
    LinearLayoutManager llm;

    public ArticleAdapter(/*Context context,*/ LinearLayoutManager linearLayoutManager) {
        super();
       // mContext = context;
        mStringList=new ArrayList<>();
        mStringList.add("fisrt");
        mStringList.add("second");
        mStringList.add("th3");
        mStringList.add("th4");
        llm = linearLayoutManager;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.card_view, viewGroup, false);

       // mContext = viewGroup.getContext();
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        String str=mStringList.get(i);
        viewHolder.vName.setText(str);
        viewHolder.vLength.setText(str+i);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        protected TextView vName;
        protected TextView vLength;
        protected View cardView;

        public ViewHolder(View v) {
            super(v);
            vName = (TextView) v.findViewById(R.id.file_name_text);
            vLength = (TextView) v.findViewById(R.id.file_length_text);
        }
    }
}

 

以上是关于RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Firebase 获取数据到 Recyclerview 中的片段?

从片段类中的对话框添加项目到recyclerview。

从其他片段添加新的 RecyclerView 项

为啥 recyclerview$adapter 在片段中为空

片段中的Android Studio RecyclerView [重复]

RecyclerView holder中的Android Google Maps动态片段