博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 多个listview的实现
阅读量:6230 次
发布时间:2019-06-21

本文共 4594 字,大约阅读时间需要 15 分钟。

正好,今天项目中需要,先写了个demo,给大家参考参考、

先上图,需要的自己,看看具体的代码实现步骤

 

大概说一下实现步骤:

1.布局中先用 scrollview 包裹 LinearLayout

1  
8 9
14 15

 

2.具体实现代码

 

 

 

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.ScrollView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;public class SearchUserContentActivity extends BaseActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_search_user_content);               initView();        addUserListview();        addContentListview();    }    ScrollView scrollView;    LinearLayout all;    private void initView() {        LayoutInflater inflater = LayoutInflater.from(this);//LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.main, null);//layout_topic = (LinearLayout) linearLayout.findViewById(R.id.linearlayout);        all = (LinearLayout) findViewById(R.id.linearlayout);        scrollView = (ScrollView) findViewById(R.id.scrollview);    }    private void addUserListview( ){        TextView textView = new TextView(this);        textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));        textView.setText("第一个ListView");        textView.setGravity(Gravity.CENTER);        textView.setTextSize(18);        textView.setTextColor(Color.BLACK);        all.addView(textView);       ArrayList arraylist = new ArrayList(); // 生成动态数组        for (int i = 1; i <= 10; i ++) {            arraylist.add( "第一个测试" + i );        }        ListViewAdapter listViewAdapter = new ListViewAdapter(this, arraylist);        ListView listView = new ListView(this);        int height = arraylist.size() * 50;        listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));        listView.setDividerHeight(1);        listView.setAdapter(listViewAdapter);        all.addView(listView);    }    private void addContentListview( ){        TextView textView = new TextView(this);        textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));        textView.setText("第二个ListView");        textView.setGravity(Gravity.CENTER);;        textView.setTextSize(18);        textView.setTextColor(Color.BLACK);        all.addView(textView);        ArrayList arraylist = new ArrayList(); // 生成动态数组        for (int i = 11; i <= 20; i ++) {            arraylist.add( "第二个测试" + i );        }        ListViewAdapter listViewAdapter = new ListViewAdapter(this, arraylist);        ListView listView = new ListView(this);        int height = arraylist.size() * 50;        listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));        listView.setDividerHeight(1);        listView.setAdapter(listViewAdapter);        all.addView(listView);    }    class ListViewAdapter extends android.widget.BaseAdapter {        Context context;        List
datas; public ListViewAdapter(Context _context, List
relativesList) { this.datas = relativesList; this.context = _context; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } public final class ViewHolder { public TextView name;//昵称 } @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.item_discovery_search_user, null); holder.name = (TextView) convertView.findViewById(R.id.name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(datas.get(position)); return convertView; } }}

 

转载于:https://www.cnblogs.com/kevin-chen/p/5478056.html

你可能感兴趣的文章
LAMP平台详述
查看>>
我的友情链接
查看>>
AsyncTask研究
查看>>
Oracle监听器启动出错:本地计算机上的OracleOraDb10g_home1TNSListener服务启动后又停止了解决方案...
查看>>
ibatis运行的SQL语句的输出——通过配置log4j
查看>>
maven常见问题问答(超全面)
查看>>
JSP中获取各种路径的方法
查看>>
linux 特殊权限 之 SUID 实例
查看>>
linux操作命令
查看>>
Capture Nx
查看>>
RedHat/CentOS命令记录
查看>>
git 学习
查看>>
MySQL基于LVM快照的备份恢复
查看>>
庞升东:个人网站年广告销售收入可超千万
查看>>
[译]ECMAScript 5 Objects and Properties
查看>>
MPEG-7 视觉描述符
查看>>
ELK6.5 Nginx 日志搜集-05 filebeat 安装
查看>>
如何用 Retrofit 2 在安卓上实现 HTTP 访问?
查看>>
2013 北京 QCon热点分享
查看>>
Linux系统下利用文件创建文件系统
查看>>