• 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);
            }
        }
    }
  • 相关阅读:
    linux 常用命令-编辑模式
    关于react虚拟DOM的研究
    oracle 分页的sql语句
    react+webpack+wepack-dev-server的环境中ant design图标离线的方法
    oracle 语句之对数据库的表名就行模糊查询,对查询结果进行遍历,依次获取每个表名结果中的每个字段(存储过程)
    eclipse 中使用git
    好东西要分享
    《梦断代码》阅读笔记二
    《梦断代码》阅读笔记一
    第二段冲刺进程4
  • 原文地址:https://www.cnblogs.com/cnchengv/p/9864516.html
Copyright © 2020-2023  润新知