正好,今天項目中須要,先寫了個demo,給你們參考參考、java
先上圖,須要的本身,看看具體的代碼實現步驟android
大概說一下實現步驟:數組
1.佈局中先用 scrollview 包裹 LinearLayoutide
1 <ScrollView 2 android:id="@+id/scrollview" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:layout_below="@+id/relativeLayout1" 6 android:fadingEdge="none" 7 android:scrollbars="vertical"> 8 9 <LinearLayout 10 android:id="@+id/linearlayout" 11 android:layout_width="match_parent" 12 android:layout_height="fill_parent" 13 android:orientation="vertical"> 14 </LinearLayout> 15 </ScrollView>
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<String> datas; public ListViewAdapter(Context _context, List<String> 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; } } }