gpt4 book ai didi

android-listview - 同一个 Activity 上的 Viewpager 和 listView

转载 作者:行者123 更新时间:2023-12-02 04:53:17 25 4
gpt4 key购买 nike

我想做一个包含 viewpager 和 listview 的事件,你可以在它下面滚动它们..就像这些应用程序......任何人都可以提供帮助

The Verge app

最佳答案

好吧,我可以通过测量 listView 的高度并将 ListView 和 View 寻呼机添加到 ScrollView 中来实现,这是我制作的示例

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);

ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);

myPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
PointF downP = new PointF();
PointF curP = new PointF();
int act = event.getAction();
if (act == MotionEvent.ACTION_DOWN
|| act == MotionEvent.ACTION_MOVE
|| act == MotionEvent.ACTION_UP) {
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
if (downP.x == curP.x && downP.y == curP.y) {
return false;
}
}
return false;
}
});

Aadpater aa = new Aadpater(this, text);
final ListView ll = (ListView) findViewById(R.id.listView);

ll.setAdapter(aa);

Utility.setListViewHeightBasedOnChildren(ll);
}

private int imageArra[] = { R.drawable.about_logo, R.drawable.ic_launcher,
R.drawable.nagy_cv, R.drawable.rewrew };

private String text[] = { "one", "two", "three", "four", " five", "six",
"seven", "eight", "one", "two", "three", "four", " five", "six",
"seven", "eight", "one", "two", "three", "four", " five", "six",
"seven", "eight", "one", "two", "three", "four", " five", "six",
"seven", "eight", "one", "two", "three", "four", " five", "six",
"seven", "eight" };

}

下面是我如何测量 listView 的高度

  public class Utility {

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();

if (listAdapter == null) {
// pre-condition
return;
}


int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, 0);
totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}

这是我的布局

  <?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="wrap_content" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="fill_parent"
android:layout_height="300dp" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp" >
</ListView>
</LinearLayout>

</ScrollView>

关于android-listview - 同一个 Activity 上的 Viewpager 和 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com