问题
I know this has been asked many times but I still have yet to find a solution I really like. Can someone please suggest the most elegant way to do what I'm trying to do:
I have two Listviews which I currently have in a LinearLayout (vertical). I want the Listviews to wrap their content when it comes to height so they never need to scroll internally. Instead, I would like to scroll down on the page in order to see anything that overflows.
Thanks in advance for the help! If you suggest completely re-doing the architecture then I'm open to that as well. The only thing I ask is for a solution to be as clean and simple as possible. Hackish workarounds not preferred. :)
回答1:
As far as I know, you are trying to make the ListView as height as its content requires.
You can create a custom listview which extends ListView and override its onMeasure method like this:
public class UnscrollableListView extends ListView {
public UnscrollableListView(Context context) {
super(context);
}
public UnscrollableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UnscrollableListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxHeightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, maxHeightSpec);
}
}
This will make your listview to wrap their content.
As you need to scroll down, try to add a ScrollView in your layout to wrap the LinearLayout. Finally, your layout is something like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.UnscrollableListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.example.UnscrollableListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Is it what you want?
But I have to say that these codes make listview to perform just like a vertical linearlayout with a lot of similar children. It could not take advantage of the recycling views to improve layout performance because no view is going to be recycled.
You can have a look on this article: Performance Tips for Android’s ListView
回答2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
回答3:
do this way,
listview_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:id="@+id/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" >
<LinearLayout
android:id="@+id/list_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@android:color/black" />
<LinearLayout
android:id="@+id/list_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Adapter1.java(Adapter2 is same as Adapter1)
public class Adapter1 extends BaseAdapter {
private Activity activity;
private LayoutInflater mInflater;
private ArrayList<String> arraylist1 = new ArrayList<String>();
public Adapter1(Activity act, ArrayList<String> array) {
this.activity = act;
this.arraylist1 = array;
mInflater = LayoutInflater.from(activity);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist1.size();
}
@Override
public Object getItem(int position) {
return arraylist1.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return arraylist1.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item1, null);
holder = new ViewHolder();
holder.lablel = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.lablel.setText(arraylist1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView lablel = null;
}
}
MainActivity.java
public class MainActivity extends Activity {
private ArrayList<String> arraylist1 = new ArrayList<String>();
private ArrayList<String> arraylist2 = new ArrayList<String>();
private Adapter1 adapter1 = null;
private Adapter2 adapter2 = null;
private LinearLayout listview2;
private LinearLayout listview1;
private ScrollView scrollView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist1.add("listview_item1");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
arraylist2.add("listview_item2");
listview1 = (LinearLayout) findViewById(R.id.list_view1);
listview2 = (LinearLayout) findViewById(R.id.list_view2);
scrollView = (ScrollView) findViewById(R.id.scrollview);
adapter1 = new Adapter1(MainActivity.this, arraylist1);
adapter2 = new Adapter2(MainActivity.this, arraylist2);
listview1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
scrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
listview1.removeAllViews();
if (adapter1 != null && adapter1.getCount() > 0) {
for (int i = 0; i < adapter1.getCount(); i++) {
final View convertView = adapter1.getView(i, null, null);
if (listview1 != null)
listview1.addView(convertView);
}
}
listview2.removeAllViews();
if (adapter2 != null && adapter2.getCount() > 0) {
for (int i = 0; i < adapter2.getCount(); i++) {
final View convertView = adapter2.getView(i, null, null);
if (listview2 != null)
listview2.addView(convertView);
}
}
}
}
来源:https://stackoverflow.com/questions/29817442/how-to-make-linearlayout-scrollable-with-two-listviews-scrollable-which-wrap-the