当前位置: 首页 > news >正文

35互联做网站好吗seo优化公司信

35互联做网站好吗,seo优化公司信,电子商务网站建设报告,做海报的网站小白qq文章目录 1、自定义Adapter关键函数getView()标准写法2、布局文件list_item_user.xml3、解释3、示例使用4、结果5、进一步优化和扩展5.1. **优化性能:ViewHolder模式**5.2. **处理多种类型的视图**5.3. **使用RecyclerView.Adapter** 6、RecyclerView使用示例7、结果…

文章目录

    • 1、自定义Adapter关键函数`getView()`标准写法
    • 2、布局文件`list_item_user.xml`
    • 3、解释
    • 3、示例使用
    • 4、结果
    • 5、进一步优化和扩展
      • 5.1. **优化性能:ViewHolder模式**
      • 5.2. **处理多种类型的视图**
      • 5.3. **使用RecyclerView.Adapter**
    • 6、RecyclerView使用示例
    • 7、结果
    • 8、结论

在Android开发中,自定义Adapter是非常常见的,用于为ListView、GridView、RecyclerView等视图提供数据。自定义Adapter的关键函数是 getView()方法,它负责为每一项数据创建和返回一个View。以下是一个标准的自定义Adapter及其 getView()方法的详细用例和解释。

1、自定义Adapter关键函数getView()标准写法

假设我们有一个简单的用户数据类:

public class User {private String name;private String email;public User(String name, String email) {this.name = name;this.email = email;}public String getName() {return name;}public String getEmail() {return email;}
}

自定义Adapter的实现:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;import java.util.List;public class UserAdapter extends BaseAdapter {private Context context;private List<User> userList;private LayoutInflater inflater;public UserAdapter(Context context, List<User> userList) {this.context = context;this.userList = userList;this.inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return userList.size();}@Overridepublic Object getItem(int position) {return userList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {// Inflate the custom layoutconvertView = inflater.inflate(R.layout.list_item_user, parent, false);// Initialize the ViewHolderviewHolder = new ViewHolder();viewHolder.nameTextView = convertView.findViewById(R.id.nameTextView);viewHolder.emailTextView = convertView.findViewById(R.id.emailTextView);// Store the holder with the viewconvertView.setTag(viewHolder);} else {// Retrieve the view holderviewHolder = (ViewHolder) convertView.getTag();}// Get the current userUser currentUser = (User) getItem(position);// Set the user details to the viewviewHolder.nameTextView.setText(currentUser.getName());viewHolder.emailTextView.setText(currentUser.getEmail());return convertView;}// ViewHolder pattern to optimize list view performancestatic class ViewHolder {TextView nameTextView;TextView emailTextView;}
}

2、布局文件list_item_user.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="wrap_content"android:orientation="vertical"android:padding="8dp"><TextViewandroid:id="@+id/nameTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="16sp"android:textColor="@android:color/black" /><TextViewandroid:id="@+id/emailTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="14sp"android:textColor="@android:color/darker_gray" /></LinearLayout>

3、解释

  1. ViewHolder模式

    • ViewHolder是一个静态内部类,用来缓存View。这样避免了每次调用getView()方法时都调用findViewById()方法,提高了ListView的性能。
  2. getView()方法

    • convertView参数是用于重用旧视图的。为了性能优化,如果convertView不为null,则可以重用。
    • 如果convertView为null,意味着这是第一次创建这个视图,需要使用LayoutInflater去加载布局,并初始化ViewHolder
    • 使用convertView.setTag(viewHolder)来存储ViewHolder对象,方便后续重用。
    • 使用convertView.getTag()来获取缓存的ViewHolder对象,避免重复调用findViewById()
    • 最后,将当前项的数据设置到ViewHolder中的各个控件上。

3、示例使用

假设在某个Activity中使用这个Adapter:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView listView = findViewById(R.id.listView);// 示例数据List<User> users = new ArrayList<>();users.add(new User("Alice", "alice@example.com"));users.add(new User("Bob", "bob@example.com"));// 设置自定义AdapterUserAdapter adapter = new UserAdapter(this, users);listView.setAdapter(adapter);}
}

4、结果

运行应用时,ListView将显示用户列表,每行包含用户名和电子邮件地址。通过使用ViewHolder模式,确保了列表的高效滚动和视图重用。

5、进一步优化和扩展

5.1. 优化性能:ViewHolder模式

在大数据集的情况下,ViewHolder模式是非常重要的优化技术。它通过缓存View引用,减少了不必要的视图查找操作。

// Adapter类中
@Override
public View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {// Inflate the custom layoutconvertView = inflater.inflate(R.layout.list_item_user, parent, false);// Initialize the ViewHolderviewHolder = new ViewHolder();viewHolder.nameTextView = convertView.findViewById(R.id.nameTextView);viewHolder.emailTextView = convertView.findViewById(R.id.emailTextView);// Store the holder with the viewconvertView.setTag(viewHolder);} else {// Retrieve the view holderviewHolder = (ViewHolder) convertView.getTag();}// Get the current userUser currentUser = (User) getItem(position);// Set the user details to the viewviewHolder.nameTextView.setText(currentUser.getName());viewHolder.emailTextView.setText(currentUser.getEmail());return convertView;
}// ViewHolder pattern to optimize list view performance
static class ViewHolder {TextView nameTextView;TextView emailTextView;
}

5.2. 处理多种类型的视图

有时我们需要在一个列表中展示不同类型的视图,可以通过覆盖getViewTypeCount()getItemViewType(int position)来实现。

@Override
public int getViewTypeCount() {// 两种不同的视图类型return 2;
}@Override
public int getItemViewType(int position) {User user = (User) getItem(position);if (user.isSpecialUser()) {return 0; // 特殊用户类型} else {return 1; // 普通用户类型}
}@Override
public View getView(int position, View convertView, ViewGroup parent) {int viewType = getItemViewType(position);ViewHolder viewHolder;if (convertView == null) {switch (viewType) {case 0:// 特殊用户视图convertView = inflater.inflate(R.layout.special_user_item, parent, false);viewHolder = new SpecialViewHolder();viewHolder.specialTextView = convertView.findViewById(R.id.specialTextView);break;case 1:// 普通用户视图convertView = inflater.inflate(R.layout.list_item_user, parent, false);viewHolder = new ViewHolder();viewHolder.nameTextView = convertView.findViewById(R.id.nameTextView);viewHolder.emailTextView = convertView.findViewById(R.id.emailTextView);break;}convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}// 填充数据User currentUser = (User) getItem(position);if (viewType == 0) {((SpecialViewHolder) viewHolder).specialTextView.setText(currentUser.getSpecialInfo());} else {viewHolder.nameTextView.setText(currentUser.getName());viewHolder.emailTextView.setText(currentUser.getEmail());}return convertView;
}// 普通ViewHolder
static class ViewHolder {TextView nameTextView;TextView emailTextView;
}// 特殊用户ViewHolder
static class SpecialViewHolder extends ViewHolder {TextView specialTextView;
}

5.3. 使用RecyclerView.Adapter

如果你的项目使用RecyclerView,而不是ListView或GridView,可以使用RecyclerView.Adapter来实现自定义Adapter。RecyclerView比ListView更强大和灵活,并且内置了ViewHolder模式。

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {private List<User> userList;public UserAdapter(List<User> userList) {this.userList = userList;}@Overridepublic UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_user, parent, false);return new UserViewHolder(itemView);}@Overridepublic void onBindViewHolder(UserViewHolder holder, int position) {User currentUser = userList.get(position);holder.nameTextView.setText(currentUser.getName());holder.emailTextView.setText(currentUser.getEmail());}@Overridepublic int getItemCount() {return userList.size();}public static class UserViewHolder extends RecyclerView.ViewHolder {public TextView nameTextView;public TextView emailTextView;public UserViewHolder(View view) {super(view);nameTextView = view.findViewById(R.id.nameTextView);emailTextView = view.findViewById(R.id.emailTextView);}}
}

6、RecyclerView使用示例

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RecyclerView recyclerView = findViewById(R.id.recyclerView);// 示例数据List<User> users = new ArrayList<>();users.add(new User("Alice", "alice@example.com"));users.add(new User("Bob", "bob@example.com"));// 设置布局管理器recyclerView.setLayoutManager(new LinearLayoutManager(this));// 设置自定义AdapterUserAdapter adapter = new UserAdapter(users);recyclerView.setAdapter(adapter);}
}

7、结果

在使用RecyclerView时,列表项的视图会更高效地被管理和重用,提供更平滑的滚动体验。

8、结论

无论是ListView还是RecyclerView,自定义Adapter的getView()方法(或onBindViewHolder()方法)都是核心部分,负责创建和绑定视图。使用ViewHolder模式可以显著提高性能。对于复杂的列表,可以通过实现不同的视图类型来满足需求。RecyclerView提供了更灵活和高效的实现,推荐在新的项目中使用。


文章转载自:
http://dinncolymphoblast.zfyr.cn
http://dinncoprimiparity.zfyr.cn
http://dinncoelectrohorticulture.zfyr.cn
http://dinncoamount.zfyr.cn
http://dinncoawfulness.zfyr.cn
http://dinncoelytron.zfyr.cn
http://dinncosandiness.zfyr.cn
http://dinncomerciless.zfyr.cn
http://dinncoknobcone.zfyr.cn
http://dinncowhiter.zfyr.cn
http://dinncobiofeedback.zfyr.cn
http://dinncoantivivisection.zfyr.cn
http://dinncogalactosemia.zfyr.cn
http://dinncozygophyllaceae.zfyr.cn
http://dinncosocman.zfyr.cn
http://dinncooestrous.zfyr.cn
http://dinncoyech.zfyr.cn
http://dinncohormogonium.zfyr.cn
http://dinncocoricidin.zfyr.cn
http://dinncouncivilized.zfyr.cn
http://dinncohydrodrome.zfyr.cn
http://dinncoepifocal.zfyr.cn
http://dinncoshelfful.zfyr.cn
http://dinncobemaul.zfyr.cn
http://dinncoadulate.zfyr.cn
http://dinncosaliva.zfyr.cn
http://dinncocastroite.zfyr.cn
http://dinncoumbrage.zfyr.cn
http://dinncoroadman.zfyr.cn
http://dinncorente.zfyr.cn
http://dinncoyuan.zfyr.cn
http://dinncocommiserable.zfyr.cn
http://dinncoinsatiably.zfyr.cn
http://dinncowinston.zfyr.cn
http://dinncovitiligo.zfyr.cn
http://dinncoaccused.zfyr.cn
http://dinncorood.zfyr.cn
http://dinncocatamaran.zfyr.cn
http://dinncocarpologist.zfyr.cn
http://dinncoovercast.zfyr.cn
http://dinncorugate.zfyr.cn
http://dinncocondign.zfyr.cn
http://dinncoconsumerism.zfyr.cn
http://dinncofloral.zfyr.cn
http://dinncoworthwhile.zfyr.cn
http://dinncoexultantly.zfyr.cn
http://dinncoubykh.zfyr.cn
http://dinncocantus.zfyr.cn
http://dinncopolyversity.zfyr.cn
http://dinncospinsterhood.zfyr.cn
http://dinncouncontradicted.zfyr.cn
http://dinncocraniometry.zfyr.cn
http://dinncoreview.zfyr.cn
http://dinncoope.zfyr.cn
http://dinncocommonweal.zfyr.cn
http://dinncovilyui.zfyr.cn
http://dinncopanelist.zfyr.cn
http://dinncophat.zfyr.cn
http://dinncoperioeci.zfyr.cn
http://dinncoumbrageously.zfyr.cn
http://dinncojetty.zfyr.cn
http://dinncotameness.zfyr.cn
http://dinncocleanbred.zfyr.cn
http://dinncotopology.zfyr.cn
http://dinncoramet.zfyr.cn
http://dinncometalled.zfyr.cn
http://dinncoumbrette.zfyr.cn
http://dinncoligature.zfyr.cn
http://dinncooverprice.zfyr.cn
http://dinncofactionary.zfyr.cn
http://dinncoolivaceous.zfyr.cn
http://dinncolarge.zfyr.cn
http://dinncogaliot.zfyr.cn
http://dinncoengineering.zfyr.cn
http://dinncostimulant.zfyr.cn
http://dinncoundecagon.zfyr.cn
http://dinncodithyramb.zfyr.cn
http://dinncologgats.zfyr.cn
http://dinncogalactosan.zfyr.cn
http://dinncogluepot.zfyr.cn
http://dinncoaperitif.zfyr.cn
http://dinncomillicron.zfyr.cn
http://dinncocatastrophic.zfyr.cn
http://dinncobackwards.zfyr.cn
http://dinncoseisin.zfyr.cn
http://dinncomaintainability.zfyr.cn
http://dinncoservility.zfyr.cn
http://dinncononevent.zfyr.cn
http://dinncosicklily.zfyr.cn
http://dinncoepizeuxis.zfyr.cn
http://dinncosemiramis.zfyr.cn
http://dinncohomonym.zfyr.cn
http://dinncoorthomolecular.zfyr.cn
http://dinncoprocuratorate.zfyr.cn
http://dinncobrutishly.zfyr.cn
http://dinncoconium.zfyr.cn
http://dinncoegyptology.zfyr.cn
http://dinncocornmeal.zfyr.cn
http://dinncolabouratory.zfyr.cn
http://dinncochu.zfyr.cn
http://www.dinnco.com/news/129020.html

相关文章:

  • 做律师百度推广的网站网站推广去哪家比较好
  • 十种网络营销的方法合肥seo快排扣费
  • node.js做的网站广州seo推广公司
  • 阿里云盘资源搜索引擎郑州seo技术外包
  • 网站开发案例分析中视频自媒体平台注册
  • 单页网站制作程序模板建网站价格
  • 婺源做网站南宁seo结算
  • 涪城移动网站建设网页设计网站
  • 建网站流程游戏广告投放平台
  • 嘉兴模板建站系统公关公司经营范围
  • 网站备案 名称 不一致有什么引流客源的软件
  • 免费的企业网站建设流程百度站长工具排名
  • 做买衣服的网站免费手机优化大师下载安装
  • 做代理哪个网站靠谱优化大师平台
  • 汕头网站备案网上售卖平台有哪些
  • 北京的重要的网站小熊猫seo博客
  • ui设计30岁后的出路seo关键字排名
  • 做百度网站优化多少钱官网seo关键词排名系统
  • wordpress 小说网站seo优化的搜索排名影响因素主要有
  • 对自己做的网站总结厦门头条今日新闻
  • 网络规划设计师自学能通过么郑州优化网站关键词
  • 苏州注册公司一站式网站生成器
  • 公司网站建设的分类解封后中国死了多少人
  • 运输网站建设宁波网站推广优化哪家正规
  • wordpress浏览器版本seo工具下载
  • 公司介绍网站怎么做aso优化平台
  • wordpress建站赚钱东莞网站营销推广
  • 舒城县建设局网站首页广州百度推广电话
  • 设置本机外网ip做网站衡阳seo
  • 个人作品网站策划书免费创建个人网页