首页 > 其他分享 >recycleView 简单模板框架

recycleView 简单模板框架

时间:2023-11-24 11:03:26浏览次数:30  
标签:框架 int recycleView item import android public 模板 view



文章目录

  • 1、功能简介
  • 2、文件结构
  • 3、build.gradle(Module:app)
  • 4、activity_main.xml 文件
  • 5、recycleview_item.xml
  • 6、RecycleViewAdapter 文件
  • 7、StudentData 文件
  • 8、MainActivity 文件


1、功能简介

实现recycle 和 自定义 item 的适配
读取 姓名

recycleView 简单模板框架_android

2、文件结构

recycleView 简单模板框架_xml_02

3、build.gradle(Module:app)

添加 recycleView 编译引用

compile ‘com.android.support:recyclerview-v7:26.1.0’

recycleView 简单模板框架_ide_03

4、activity_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <LinearLayout
            android:id="@+id/lay_id"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycle_list_id"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

        </LinearLayout>



</LinearLayout>
5、recycleview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
>


    <RelativeLayout
        android:id="@+id/item_lay_id"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_gravity="center"

        android:gravity="center">


        <LinearLayout
            android:id="@+id/item_id"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ff5a5a5a"
            android:alpha="0.7"
            android:gravity="center"
            >

            <TextView
                android:id="@+id/item_name_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="name"
                android:textColor="#FFFFFF"
                android:textSize="30dp"
                />


        </LinearLayout>
    </RelativeLayout>


</LinearLayout>
6、RecycleViewAdapter 文件
package com.example.lum.myrecycleview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> implements View.OnClickListener {
    private static final String TAG = "RecycleViewAdapter";
    Context mContext;
    private ArrayList<StudentData> mrecycleDataList;


    // Provide a suitable constructor (depends on the kind of dataset)
    public RecycleViewAdapter(Context context, ArrayList<StudentData> mrecycleDataList) {
        this.mContext = context;
        this.mrecycleDataList = mrecycleDataList;
    }

    private void populateLauncherView(final ViewHolder holder, StudentData studentData, int positon) {
        System.out.println(TAG + " 位置 item  Position:" + positon);

        holder.textViewName.setText(studentData.getStudentName()+ ""); //设置
        holder.textViewName.setTag(positon);

        holder.relativeLayout.setTag(positon);
        holder.itemView.setTag(positon);

        holder.textViewName.setOnClickListener(this);

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element


        StudentData studentData = (StudentData) mrecycleDataList.get(position);
        populateLauncherView(holder, studentData,position);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mrecycleDataList.size();
    }


    @Override
    public void onClick(View v) {
        Toast.makeText(mContext, "name  view  this  click", Toast.LENGTH_SHORT).show();
    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder

    //初始化  icon  界面
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case

        public RelativeLayout relativeLayout;
        public TextView textViewName;
        public LinearLayout mRootView;
        public ViewGroup mParent;

        public ViewHolder(LinearLayout v, ViewGroup parent) {
            super(v);
            mRootView = v;
            mParent = parent;
            relativeLayout = (RelativeLayout) v.findViewById(R.id.item_lay_id);
            textViewName = (TextView) v.findViewById(R.id.item_name_id);

        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
        // create a new view
        LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycleview_item, parent, false);

        ViewHolder vh = new ViewHolder(v, parent);
        return vh;
    }

}
7、StudentData 文件
package com.example.lum.myrecycleview;

import android.util.Log;

/**
 * Created by menglux on 5/11/2018.
 */

public class StudentData {
    private  String  TAG = "StudentData: ";

    private String  name = "";

    public StudentData(){}

    public  void  setStudentName(String  name) {
        this.name = name;
        Log.i(TAG,"setName: " + name );
    }
    public String  getStudentName() {
        return  name;
    }


}
8、MainActivity 文件
package com.example.lum.myrecycleview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity: ";

    private RecyclerView recyclerView;  //recycleView 对象
    public ArrayList<StudentData> mStuDataList;   //studentData  类的对象 集合
    private RecycleViewAdapter mRecycleViewAdapter;  //recycle 适配器类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // listvew 显示
        recyclerView = (RecyclerView) findViewById(R.id.recycle_list_id);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL,false);   //设置recycleView 水平
        recyclerView.setLayoutManager(layoutManager);


        creatListData(); //初始化 数据

        mRecycleViewAdapter = new RecycleViewAdapter(this, mStuDataList);  //适配器 和 view适配
        recyclerView.setAdapter(mRecycleViewAdapter);

        mRecycleViewAdapter.notifyDataSetChanged();  //刷新 view 数据

        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));  //recycleView 间隔


    }


    private void setTextColor() {

        int position = 2; //选定的显示学生的位置
        //通过 activity  获得adapter 里面的组件
       LinearLayoutManager mlayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

          int firstPosition = mlayoutManager.findFirstVisibleItemPosition();
           int lastPosition = mlayoutManager.findLastVisibleItemPosition();

        if (position < firstPosition || position > lastPosition) {

            recyclerView.smoothScrollToPosition(position); //滚动到指定的位置
            Log.i(TAG, "```````````不在显示区域  滚动到显示区域");

        } else { //直接获取
            Log.i(TAG, "```````````在显示区域 字体颜色改变");
            //获取view  某个位置的 item
            View viewItem = mlayoutManager.getChildAt(position - (int) mlayoutManager.getChildAt(0).getTag());//通过获取Child(0)的tag得到第一个Child的实际位置 }

            if (viewItem == null) {
                Toast.makeText(MainActivity.this, "viewItem is null", Toast.LENGTH_SHORT).show();
            }
            //获取某个位置 item 的 viewHold
            RecycleViewAdapter.ViewHolder viewHolder = (RecycleViewAdapter.ViewHolder) recyclerView.getChildViewHolder(viewItem);

            //获取某个位置 item 的 的 某个组件
            viewHolder.textViewName.setTextColor(getResources().getColor(R.color.colorPrimary));//设定此位置 字体颜色蓝色

        }


    }


    protected void onResume() {
        super.onResume();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setTextColor();
    }
}).start();
    }

    @Override
    public  void onDestroy() {
        super.onDestroy();
    }

    private void creatListData() {

        Log.i(TAG,"创建 recycleList 数据");
        mStuDataList = new ArrayList<StudentData>();
        mStuDataList.clear();

        for (int i=0; i<10; i++) {      //添加 10 个空的   对象
            StudentData studentData = new StudentData();
            studentData.setStudentName("name: " + i);  //初始化 类名
            mStuDataList.add(studentData);
        }
    }

}


标签:框架,int,recycleView,item,import,android,public,模板,view
From: https://blog.51cto.com/u_15866638/8546129

相关文章

  • java-EasyExcel模板导出
    前言: 需求:根据自定义模板导出Excel,包含图片、表格,采用EasyExcel 提示:EasyExcel请使用3.0以上版本,对图片操作最重要的类就是WriteCellData<Void>如果你的easyexcel没有这个类,说明你的版本太低,请升级到3.0以上<dependency><groupId>com.alibaba</groupId><ar......
  • 集合框架详解 [精选]
    Hii,mJinXiang⭐前言⭐本篇文章主要介绍集合框架的两个接口超级详细介绍,集合框架的使用以及部分理论知识......
  • html大体框架
    html框架<!--这一句话声明这是h5版本的--><!DOCTYPEhtml><!--这一句把lang="en"去掉要不然默认语言为英文--><htmllang="en"><head><!--声明编码格式是UTF-8--><metacharset="UTF-8"><!--设置网站的关键字:方便外部......
  • 框架安全
    常见框架:Spring框架Struts2框架ThinkPHP框架Shiro框架Spring框架框架特征1.ico图标是一个小绿叶2.报错页面的大标题是WhitelabelErrorPage3.X-Application-Context中会出现spring-boot字样Struts2框架框架特征1.路由以.action后缀结尾利用工具K8gege安恒......
  • 模板渲染成标签还是原封不动的字符串 标签(for,for ... empty,if,with,csrf_token)
    模板渲染成标签还是原封不动的字符串:#xss攻击:是什么,如何预防?django已经处理了xss攻击,它的处理原理是什么fromdjango.utils.safestringimportmark_safelink1='<ahref="https://www.baidu.com">点我<a>'link2=mark_safe(link1){link1|safe}  标签:1{%标签名%}......
  • 模板语法之句点符的深度查询
     views.py:defindex(request):num=10ss='lqzishandsome'b=Falsell=[1,2,43,{'name':'egon'}]dic={'name':'lqz','age':18}deftest():print('我是tes......
  • django模板使用的两种方式 模板语法之变量
    模板语法之变量DTL:DjangoTemplateLanguage1模板中使用{{python变量}}############views.pydefindex(request):num=10ss='lqzishandsome'b=Falsell=[1,2,43]dic={'name':'lqz','age':18}deftes......
  • 【模板】可持久化线段树 2
    【模板】可持久化线段树2题目背景这是个非常经典的可持久化权值线段树入门题——静态区间第$k$小。数据已经过加强,请使用可持久化权值线段树。同时请注意常数优化。题目描述如题,给定$n$个整数构成的序列$a$,将对于指定的闭区间$[l,r]$查询其区间内的第$k$小值。输......
  • 软件测试/人工智能|如何使用ChatGPT编写符合PO模式的数据驱动测试框架
    简介上一篇文章我们介绍了使用ChatGPT帮我们编写自动化测试脚本,但是上文编写的脚本并不符合我们的PO设计模式,作为现在主流的设计模式,更加方便我们去编写脚本,一旦页面发生变动,我们的代码改动也会变小,所以我们的目标不是使用ChatGPT编写自动化脚本,而是要使用ChatGPT来编写符合PO设......
  • 全新Self-RAG框架亮相,自适应检索增强助力超越ChatGPT与Llama2,提升事实性与引用准确性
    全新Self-RAG框架亮相,自适应检索增强助力超越ChatGPT与Llama2,提升事实性与引用准确性1.基本思想大型语言模型(LLMs)具有出色的能力,但由于完全依赖其内部的参数化知识,它们经常产生包含事实错误的回答,尤其在长尾知识中。为了解决这一问题,之前的研究人员提出了检索增强生成(RAG),它通......