正好,今天项目中需要,先写了个demo,给大家参考参考、
先上图,需要的自己,看看具体的代码实现步骤
大概说一下实现步骤:
1.布局中先用 scrollview 包裹 LinearLayout
18 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; Listdatas; 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; } }}