zoukankan      html  css  js  c++  java
  • 关于RecyclerView(一)基本使用

    前言

    最近在项目中用到了ListView,不知道为什么总是出现数据错乱的情况,另外RecyclerView包含很多Item动画,所以改成了RecyclerView。

    简单使用

    1. RecyclerView使用的时候需要引入依赖
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    

    后面的版本号根据自己的项目版本更改,引入完成之后点击上面的Sync Now

    1. 在activity中添加控件
    <android.support.v7.widget.RecyclerView
            android:id="@+id/focus_list_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"></android.support.v7.widget.RecyclerView>
    
    1. 编写item.xml布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="4dp"
        android:background="#fff"
        android:orientation="horizontal">
    
    
        <com.onepilltest.others.SwipeMenu
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
            <LinearLayout
                android:id="@+id/focus_list_show"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:gravity="center_vertical">
    
                    <ImageView
                        android:layout_marginLeft="20dp"
                        android:id="@+id/focus_list_item_img"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_centerInParent="true"
                        android:layout_marginRight="10dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/user">
    
                    </ImageView>
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="7"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingLeft="5dp">
    
                    <TextView
                        android:id="@+id/focus_list_item_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="7dp"
                        android:gravity="left"
                        android:text="李四  18831107935" />
    
                    <TextView
                        android:id="@+id/focus_list_item_tag"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:text="河北师范大学软件学院302" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="65dp"
                    android:layout_weight="3"
                    android:gravity="center">
    
                    <ImageView
                        android:id="@+id/focus_list_item_add"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_gravity="center"
    
                        />
                </LinearLayout>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="160dp"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/focus_list_adds"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:background="#f70"
                    android:gravity="center"
                    android:text="查看"
                    android:textColor="#ffffff"
                    android:textSize="14sp" />
                <TextView
                    android:id="@+id/focus_list_dels"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:background="@color/holo_red_light"
                    android:gravity="center"
                    android:text="取消关注"
                    android:textColor="#ffffff"
                    android:textSize="14sp" />
            </LinearLayout>
    
    
    
        </com.onepilltest.others.SwipeMenu>
    </LinearLayout>
    
    1. 创建Adapter
    package com.onepilltest.personal;
    
    import android.content.Context;
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.RequestOptions;
    import com.onepilltest.R;
    import com.onepilltest.URL.Connect;
    import com.onepilltest.entity.ToFocus;
    import com.onepilltest.entity.focus;
    import com.onepilltest.index.DoctorDetailsActivity;
    import com.onepilltest.index.FoundDoctorActivity;
    
    import java.util.List;
    
    public class FocusListAdapter2 extends RecyclerView.Adapter<FocusListAdapter2.ViewHolder> {
    
        private List<ToFocus> focusList;
    
        public FocusListAdapter2(List<ToFocus> baseList) {
            focusList = baseList;
            for(ToFocus f:focusList){
                Log.e("focus数据源",""+f.toString());
            }
    
        }
    
    
        static class ViewHolder extends RecyclerView.ViewHolder{
            ImageView img ;
            TextView name;
            TextView more;
            TextView add;
            TextView del;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                img = itemView.findViewById(R.id.focus_list_item_img);
                name = itemView.findViewById(R.id.focus_list_item_name);
                more = itemView.findViewById(R.id.focus_list_item_tag);
                add = itemView.findViewById(R.id.focus_list_adds);
                del = itemView.findViewById(R.id.focus_list_dels);
            }
        }
        private Context mContext;
    //    //点击和长按
    //    public interface OnItemClickListener {
    //        void onClick(int position);
    //    }
    //    private OnItemClickListener listener;
    //    public void setOnItemClickListener(OnItemClickListener listener) {
    //        this.listener = listener;
    //    }
    //
    //    public interface OnItemLongClickListener {
    //        void onClick(int position);
    //    }
    //    private OnItemLongClickListener longClickListener;
    //    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
    //        this.longClickListener = longClickListener;
    //    }
    
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.focus_liste_item,viewGroup,false);
            mContext = viewGroup.getContext();
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
            ToFocus focus = focusList.get(i);
            //加载头像
            RequestOptions requestOptions = new RequestOptions().circleCrop();
            Glide.with(mContext)
                    .load(Connect.BASE_URL + focus.getImg())
                    .apply(requestOptions)
                    .into(viewHolder.img);
            viewHolder.name.setText(focus.getName());
            viewHolder.more.setText(focus.getMore());
            viewHolder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("focusAdapter","查看第"+i+"条数据");
                    Toast.makeText(mContext,"查看第"+i+"条数据,Type:"+focus.toString(),Toast.LENGTH_SHORT).show();
                    if (focus.getType() == 1){
                        Intent intent = new Intent(mContext, DoctorDetailsActivity.class);
                        intent.putExtra("id",focus.getTypeId());
                        mContext.startActivity(intent);
                    }else{
                        Intent intent = new Intent(mContext, ProductActivity.class);
                        intent.putExtra("id",focus.getTypeId());
                        mContext.startActivity(intent);
                    }
    
                }
            });
            viewHolder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("focusAdapter","删除第"+i+"条数据");
                    Toast.makeText(mContext,"删除第"+i+"条数据",Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return focusList.size();
        }
    
    }
    
    
    1. 在MainActivity中绑定Adapter
            recyclerView = findViewById(R.id.focus_list_list);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new FocusListAdapter2(baseList);
            recyclerView.setAdapter(adapter);
    
  • 相关阅读:
    23. Sum Root to Leaf Numbers
    22. Surrounded Regions
    21. Clone Graph
    19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
    18. Word Ladder && Word Ladder II
    14. Reverse Linked List II
    20. Candy && Gas Station
    16. Copy List with Random Pointer
    ubuntu 下建立桌面快捷方式
    java基础篇-jar打包
  • 原文地址:https://www.cnblogs.com/charlottepl/p/12811102.html
Copyright © 2011-2022 走看看