zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    web api 设置允许跨域,并设置预检请求时间
    T4模板
    DDD模式
    Vue watch用法
    第三章--第五节:集合
    简单的Python API爬虫与数据分析教程--目录
    第三章--第四节:字典
    第三章--第三节(补充):列表排序
    汇总张小龙在知乎上的问答
    第三章--第三节:列表
  • 原文地址:https://www.cnblogs.com/cnchengv/p/9864516.html
Copyright © 2011-2022 走看看