RecyclerView

首先要通过 Library dependenceRecyclerView 并添加进去

选择的是 implementation 'androidx.recyclerview:recyclerview:1.1.0-beta01'

 

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);
        }
    }
}

 

posted @ 2018-10-28 08:51  cnchengv  阅读(117)  评论(0编辑  收藏  举报