gpt4 book ai didi

android - 如何在 ViewPager 布局的页面之一中填充 Listview

转载 作者:行者123 更新时间:2023-11-29 22:05:21 27 4
gpt4 key购买 nike

我有一个关于 ViewPager 的非常基本的问题。我有一个包含 5 个页面的 ViewPagerActivity,其中每个页面都有自己的布局 xml,此外还有 ViewPagerActivity.java 的 setContentView(R.layout.main) 中引用的 main.xml(请参阅下面的代码)。

“中间”页面(middle.xml,它是 5 个布局 xml 之一)包括一个用数据库中的数据动态填充的 ListView 。

这是我的问题。我应该在哪里放置以下代码来填充 Listview 并设置 ClickListener?

mMemoListView = (ListView)findViewById(R.id.list_in_middle_layout);
mMemoListAdapter = new MemoListAdapter(this);
mMemoListView.setAdapter(mMemoListAdapter);
mMemoListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
viewMemo(position);
}
});

我不认为它会在ViewPagerActivity.java中,除非它可以通过setContentView()同时引用main.xml和middle.xml。我还考虑过添加一个将引用 middle.xml 的 java 类,但我不知道如何在中间页面膨胀时激活它(?)。 (请参阅下面的 MyPageAdapter 以了解每个页面是如何膨胀的。)

感谢阅读。请随时要求进一步说明。

这是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myfivepanelpager"/>
</LinearLayout>

这里是middle.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
>
<RelativeLayout
android:id="@+id/titleLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffffff"
>
<com.ViewPager.TitleBackgroundButton
android:id="@+id/titleBackgroundBtn"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:text="Memo"
android:textSize="18dp"
android:textStyle="bold"
/>
</RelativeLayout>
<ListView
android:id="@+id/list_in_middle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:layout_below="@+id/titleLayout"
android:layout_above="@+id/buttonLayout"
android:divider="#00000000"
android:listSelector="#00000000"
android:cacheColorHint="#00000000"
/>
<LinearLayout
android:id="@+id/buttonLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
>
<LinearLayout
android:id="@+id/buttonLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<com.ViewPager.TitleBitmapButton
android:id="@+id/newMemoBtn"
android:layout_width="100dp"
android:layout_height="48dp"
android:text="New"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#ff420000"
/>
<com.ViewPager.TitleBitmapButton
android:id="@+id/closeBtn"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_marginLeft="20dp"
android:text="Close"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#ff420000"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

这里是ViewPagerActivity.java

public class ViewPagerActivity extends Activity {
/** Called when the activity is first created. */

public static final String TAG = "ViewPagerActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);

}

private class MyPagerAdapter extends PagerAdapter {

public int getCount() {
return 5;
}

public Object instantiateItem(View collection, int position) {

LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}

View view = inflater.inflate(resId, null);

((ViewPager) collection).addView(view, 0);

return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);

}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);

}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}

}

最佳答案

你应该在 instantiateItem 的 switch case 中添加它:

public Object instantiateItem(View collection, int position) {


View view=null;

LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;
view = inflater.inflate(resId, null);
break;
case 1:
resId = R.layout.left;
view = inflater.inflate(resId, null);
break;
case 2:
resId = R.layout.middle;
view = inflater.inflate(resId, null);
ListView memoListView = (ListView)view.findViewById(R.id.list_in_middle_layout);
MemoListAdapter memoListAdapter = new MemoListAdapter(this);
memoListView.setAdapter(memoListAdapter);

break;
case 3:
resId = R.layout.right;
view = inflater.inflate(resId, null);
break;
case 4:
resId = R.layout.farright;
view = inflater.inflate(resId, null);
break;
}



((ViewPager) collection).addView(view, 0);

return view;
}

关于android - 如何在 ViewPager 布局的页面之一中填充 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877757/

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