今天我写下关于,ListView 或 Recyclerview 子项里面再添加子项,或者是点击子项,展开该子项的子项信息,先看看xml代码
下面是MaintActivit里面的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
然后看看item里面的布局
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/bean_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll_order"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/colorAccent" />
</LinearLayout>
我这个里面放了一个 LinearLayout,设置它是不可以见的,然后通过代码的方式去addView进入LinearLayout,然后再看看LinearLyout里面我的View
<?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="match_parent">
<TextView
android:id="@+id/order_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/order_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
OK,xml代码写完了,然后看看代码吧,嘻嘻
MainActivity:
private ListView list;
private Adapter adapter;
private List<Bean> beanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beanList = new ArrayList<>();
list = (ListView) this.findViewById(R.id.list);
for (int i=0;i<30;i++){
List<Order> orderList = new ArrayList<>();
Bean bean = new Bean();
bean.setTitle("天猫"+i);
if (i%2==0){
Order order = new Order();
order.setTitle("京东"+i);
order.setTitle("京东信息"+i);
Order order2 = new Order();
order2.setTitle("京东2"+i);
order2.setTitle("京东信息2"+i);
orderList.add(order);
orderList.add(order2);
}else {
Order order = new Order();
order.setTitle("京东"+i);
order.setTitle("京东信息"+i);
orderList.add(order);
bean.setOrderList(orderList);
beanList.add(bean);
adapter = new Adapter(beanList,this);
list.setAdapter(adapter);
我觉得上面的大家都看得懂
然后看看关键的Adapter
Adapter:
private List<Bean> beanList;
private Activity mActivity;
int currentItem=-1;
public Adapter(List<Bean> beanList, Activity mActivity) {
this.beanList = beanList;
this.mActivity = mActivity;
@Override
public int getCount() {
return beanList.size();
@Override
public Bean getItem(int position) {
return beanList.get(position);
@Override
public long getItemId(int position) {
return position;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Bean bean = beanList.get(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_bean, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.bean_title);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.ll_order);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.textView.setText(bean.getTitle());
setItmes(bean.getOrderList(), holder.linearLayout);
//控制条目隐藏和显示
if (currentItem==position){
holder.linearLayout.setVisibility(View.VISIBLE);
}else {
holder.linearLayout.setVisibility(View.GONE);
holder.textView.setTag(position);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tag = (int) v.getTag();
if (tag == currentItem) {
currentItem = -1;
} else {
currentItem = tag;
notifyDataSetChanged();//数据源改变
return convertView;
//给LinearLyout 添加子视图
private void setItmes(List<Order> orderList, LinearLayout linearLayout) {
if (orderList == null || orderList.isEmpty()) {//如果没有子项,就删除子项容器里原有的子项视图
linearLayout.removeAllViews();
return;
int childCount = linearLayout.getChildCount();
int itemSize = orderList.size();
//这里的目的是为了防止linearLayout 无限制的增加,因为这个是用在复用里面的
if (itemSize > childCount) {
for (int i = 0; i < (itemSize - childCount); i++) {
linearLayout.addView(LayoutInflater.from(mActivity).inflate(R.layout.item_order, null));
} else if (itemSize < childCount) {
for (int i = 0; i < (childCount - itemSize); i++) {
linearLayout.removeViewAt(0);
for (int i = 0; i < orderList.size(); i++) {
View iView = linearLayout.getChildAt(i);
TextView title = (TextView) iView.findViewById(R.id.order_title);
TextView message = (TextView) iView.findViewById(R.id.order_message);
title.setText(orderList.get(i).getTitle());
message.setText(orderList.get(i).getMessage());
static class ViewHolder {
TextView textView;
LinearLayout linearLayout;
好了,代码都写完了
今天我写下关于,ListView 和Recyclerview 子项里面再添加子项,或者是点击子项,展开该子项的子项信息,先看看xml代码 下面是MaintActivit里面的代码<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi
有一个多月没写原创博客了,今天来介绍一下使用
RecyclerView来实现分组列表。之所以使用
RecyclerView,主要原因还是因为项目开发中使用Expandable
ListView无法实现设计师所需要的分割线。
本篇内容偏简单,主要是讲如何集成使用。这一使用场景还是相对常见而实用的,较适合初学者进阶。
实现原因及效果
想要跨平台,就要对平台有了解,最近在看android开发。在为RecyclerView中动态添加子控件和改变ViewHolder高度时遇到了几个坑,费了不少时间填坑,所以记录一下。
这个是效果图:本文demo链接:https://github.com/zhangzhaopds/RecyclerView_demo.git点击cell,那么cell的高度就会增加一倍,同时,在cell上面添加一个控件。关
利用
RecyclerView创建动态列表,动态
添加显示数据。
Android Studio:2020.3.1 这里注意:最新版的2021.1.1在布局编辑器对于material design的某些组件可以会造成无法显示的情况
Android Gradle Plugin Version:7.0.4
Gradle Version:7.0.2
1.设置主界面布局(activity_main.xml)
核心是
RecyclerView,外面加上ScrollView和LinnerLayout.
第一步,添加RecyclerView的依赖库
我们在使用 recyclerview 之前,需要在项目的 build.gradle 中添加相应的依赖库我们才能使用这个控件;
打开app/build.gradle文件,在dependencies闭包中添加以下...
listview和recyclerview都是Android中常用的列表控件,用于展示大量数据。它们的主要区别在于:
1. 数据加载方式:listview一次性将所有数据加载到内存中,而recyclerview则是通过adapter逐个加载数据,可以实现懒加载,减少内存占用。
2. 布局方式:listview只支持垂直布局,而recyclerview支持垂直、水平、网格等多种布局方式。
3. 功能扩展:recyclerview支持更多的功能扩展,如ItemDecoration、ItemAnimator等,可以实现更丰富的列表效果。
总的来说,如果需要展示大量数据且需要实现复杂的列表效果,建议使用recyclerview。如果数据量较小,且只需要简单的列表展示,可以使用listview。
Implicit super constructor Object() is undefined for default constructor. Must define an explicit c
35738
Implicit super constructor Object() is undefined for default constructor. Must define an explicit c
有梦想有行动:
hibernate 查询外键的值
weixin_46273611:
hibernate 查询外键的值
weixin_46273611:
android 百度地图定位不准问题
高数150分的苗子:
android Volley: [461] BasicNetwork.performRequest: Unexpected response code 400 for xxxxx
123KangKang: