[英]How to scroll layout which have 3 list view
我有一個布局。 此布局包含3個列表視圖,Listview中的wrap_content數據的高度未修復。 當Listview當時有大量數據時,數據會進入下面並且數據無法看到,所以我想用所有三個Listview滾動視圖如何實現。
有誰對此有所了解?
這是我的視圖,其中包含3個Listview,現在它的數據較少,但當數據在那時變得很大時,最后一個Listview有問題需要查看。 我想滾動灰色視圖...
在xml文件中使用線性布局而不是listview。 這是你的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scr" android:layout_height="fill_parent"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/r2" android:orientation="vertical"
android:layout_height="fill_parent" android:paddingTop="100dip"
android:paddingBottom="100dip">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l1" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#00B2EE">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l2" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#00EE76">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/l3" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_marginTop="10dip"
android:background="#7171C6">
</LinearLayout>
</LinearLayout>
</ScrollView>
並放入另一個將由列表膨脹的xml。
這是您的主要活動課程。
package com.list.add;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class NewlistActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout l1,l2,l3;
ScrollView scrollView;
ViewGroup contentView;
List<String> list = new ArrayList<String>();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
l3 = (LinearLayout) findViewById(R.id.l3);
scrollView = (ScrollView) findViewById(R.id.scr);
contentView = (ViewGroup) findViewById(R.id.r2);
scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView));
scrollView.post(new Runnable() {
public void run() {
scrollView.scrollTo(0, contentView.getPaddingTop());
}
});
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Shah");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Parth");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Chirag");
list.add("Shah");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list1.add("Parth");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
list2.add("Kalpesh");
System.out.println(list);
System.out.println(list1);
System.out.println(list2);
for (int i=0; i<list.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list.get(i));
l1.addView(vi);
}
for (int i=0; i<list1.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list1.get(i));
l2.addView(vi);
}
for (int i=0; i<list2.size(); i++)
{
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.raw, null);
TextView tv = (TextView) vi.findViewById(R.id.textView1);
tv.setText(list2.get(i));
l3.addView(vi);
}
}
}
並創建一個這樣的滾動類:
public class ScrollPager implements OnTouchListener
public ScrollPager(ScrollView aScrollView, ViewGroup aContentView)
{
mScrollView = aScrollView;
mContentView = aContentView;
scroller = new Scroller(mScrollView.getContext(), new OvershootInterpolator());
task = new Runnable()
{
public void run()
{
scroller.computeScrollOffset();
mScrollView.scrollTo(0, scroller.getCurrY());
if (!scroller.isFinished())
{
mScrollView.post(this);
}
}
};
}
public boolean onTouch(View v, MotionEvent event)
{
// Stop scrolling calculation.
scroller.forceFinished(true);
// Stop scrolling animation.
mScrollView.removeCallbacks(task);
if (event.getAction() == MotionEvent.ACTION_UP)
{
使用此方法,您的列表視圖可在scrollview中滾動: -
ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
ViewNewsDetail.this, viewOfferList));
getListViewSize(lstNewsOffer);
void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
您應該在XML布局中的每個ListView中使用權重屬性android:layout_weight="1"
。 因此,它將為每個列表視圖划分具有相等空間的屏幕,並且您的滾動將適用於每個ListView。
如果要在垂直或水平堆棧中顯示內容,則應使用LinearLayout
,然后使用layout_weight
屬性控制容器中各個行/列的比例。
如果您想要屏幕大小,請將layout_width
和layout_height
設置為fill_parent
否則您將無法獲得所有可用的屏幕尺寸。 如果你嘗試使用wrap_content
作為高度,一切都會崩潰,除非你采用額外的布局約束,例如minHeight
。
我們在任何地方都使用它,它非常可靠。 對於三個項目,您可以使用1/1/1
或3/3/3
。
重量也不必相等! 您可以按照自己的意願分配比例; 權重只是整個跨度(寬度/高度)的相對比率。 例如,如果您想要中間元素的兩倍大小,請使用1/2/1
; 如果你想要40%使用30/40/30
或3/4/3
。
一個好的“技巧”是在一行/列上使用layout_weight
= 1(其他行默認為零),它將“填充”任何剩余空間。 這是一種常見的布局方案。
如果需要堆棧可滾動,可以將其放入ScrollView
。 在這種情況下,您必須將LinearLayout
的layout_height
設置為wrap_content
,並且您將根據布局系統的奇思妙想進行折疊(這意味着您需要使用最小/最大約束)。
相反,讓我對同樣的實現給出最好的想法。
你有沒有想過使用ExpandableListView進行同樣的實現? 只需將第一組項目展開為展開並將其他兩個組展開為折疊,只需在用戶單擊特定組時展開它。
以下是相同的一些示例:
我確信這個想法並不好,但如果你實施了這個,那么你肯定會發現在你的情況下是有益的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.