自定義了ListView,ListViewUtils.class
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class ListViewUtils extends ListView {
public ListViewUtils(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
定義一個動態改變ListView高度的工具類:SetHeightUtils.class
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class SetHeightUtils {
/** 動態改變listView的高度 */
public static void setListViewHeightBasedOnChildren(Activity activity, ListView listView1) {
BaseAdapter listAdapter = (BaseAdapter) listView1.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
//獲取listView的寬度
ViewGroup.LayoutParams params = listView1.getLayoutParams();
int listViewWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView1);
//給item的measure設置參數是listView的寬度就可以獲取到真正每一個item的高度
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
}
params.height = totalHeight + (listView1.getDividerHeight() * (listAdapter.getCount() + 1));
listView1.setLayoutParams(params);
}
}
自定義一個ListView的適配器:ListViewUtilsAdpater.class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class ListViewUtilsAdpater extends BaseAdapter {
private Context context;//運行上下文
private LayoutInflater listContainer; //視圖容器
private List<TestBean> list;
public ListViewUtilsAdpater(Context _context, List<TestBean> list){
this.context = _context;
this.listContainer = LayoutInflater.from(_context);
this.list = list;
}
@Override
public int getCount() {
return this.list.size();
}
@Override
public Object getItem(int position) {
if(position >= list.size() || position < 0) {
return null;
} else {
return list.get(position);
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null) {
view = listContainer.inflate(R.layout.item_list, null);
}
String string1 = list.get(position).getString1();
String string2 = list.get(position).getString2();
TextView tv_string1 = (TextView) view.findViewById(R.id.tv_string1);
TextView tv_string2 = (TextView) view.findViewById(R.id.tv_string2);
tv_string1.setText(string1);
tv_string2.setText(string2);
return view;
}
}
適配器ListViewUtilsAdpater.class使用到的:TestBean.class
public class TestBean {
private String string1;
private String string2;
public TestBean(String string1, String string2) {
this.string1 = string1;
this.string2 = string2;
}
public String getString1() {
return string1;
}
public void setString1(String string1) {
this.string1 = string1;
}
public String getString2() {
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
}
佈局文件xml:
activity_main.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="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<nfc.kh.com.hkapp.view.ListViewUtils
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</nfc.kh.com.hkapp.view.ListViewUtils>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
item_list.xm:
<?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">
<TextView
android:id="@+id/tv_string1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_string2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dp"
android:layout_weight="1"/>
</LinearLayout>
主頁MainActivity.class :
package nfc.kh.com.hkapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import nfc.kh.com.hkapp.view.ListViewUtils;
public class MainActivity extends AppCompatActivity {
ListViewUtils listViewUtils;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewUtils = (ListViewUtils)findViewById(R.id.listview);
tv1 = (TextView)findViewById(R.id.tv1);
List<TestBean> list = new ArrayList<>();
TestBean testBean = new TestBean("asd weyrfwedfhws98frwadmsafdjhweqfasfasdfasasasdaweas,asfsadfmdciuwegfiasm,dfue","afsadhfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean1 = new TestBean("weyrfwedfhws98frwadmsafdjhweqfweas,asfsadfmdciuwegfiasm,dfue","afsadhasdfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean2 = new TestBean("weyrfwedfhws98fe","fgawmndfshubfw");
TestBean testBean3 = new TestBean("asd weyrfwedfhws98frwadmsafdjhweqfasfasdfasasasdaweas,asfsadfmdciuwegfiasm,dfue","afsadhfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean4 = new TestBean("weyrfwedfhws98frwadmsafdjhweqfweas,asfsadfmdciuwegfiasm,dfue","afsadhasdfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean5 = new TestBean("weyrfwedfhws98fe","fgawmndfshubfw");
TestBean testBean6 = new TestBean("asd weyrfwedfhws98frwadmsafdjhweqfasfasdfasasasdaweas,asfsadfmdciuwegfiasm,dfue","afsadhfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean7 = new TestBean("weyrfwedfhws98frwadmsafdjhweqfweas,asfsadfmdciuwegfiasm,dfue","afsadhasdfgweqfdasgfwqetfgawmndfshubfw");
TestBean testBean8 = new TestBean("weyrfwedfhws98fe","fgawmndfshubfw");
list.add(testBean);
list.add(testBean1);
list.add(testBean2);
list.add(testBean3);
list.add(testBean4);
list.add(testBean5);
list.add(testBean6);
list.add(testBean7);
ListViewUtilsAdpater listViewUtilsAdpater = new ListViewUtilsAdpater(this,list);
listViewUtils.setAdapter(listViewUtilsAdpater);
SetHeightUtils.setListViewHeightBasedOnChildren(this,listViewUtils);
tv1.setFocusable(true);
tv1.setFocusableInTouchMode(true);
tv1.requestFocus();
}
}
ScrollView內嵌ListView或者recyclerview打開時沒有在頂部的問題
方法一:
手動設置滾動到頂部
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
方法二:
如果ScrollView控件上方有其他控件,則可以設置給該控件的焦點,讓滾動條回滾到頂部去:
View view=(View)findViewById(R.id.view);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();