ListView的行项目中ViewPager [英] ViewPager within ListView row item
问题描述
我已经读了很多关于这个问题相关的问题。然而,他们中没有得到回答。我想一个 ViewPager
添加到的ListView
,其中有寻呼机内的两个布局每一行。我相信这是因为可行的 SwipeListView 的
现在我已经实现了。然而,一个空白页面是显示在中间的线。没有行正在出现。
下面是我的code:
MainActivity + ArrayAdapter
公共类MainActivity延伸活动{ ListView控件列表;
的String []英雄; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 英雄= getResources()getStringArray(R.array.heros)。 名单=(ListView控件)findViewById(R.id.listView1);
list.setAdapter(新CustomAdapter(这一点,英雄));
} 公共类CustomAdapter扩展ArrayAdapter<串GT;
{
的String []英雄;
上下文语境;
公共CustomAdapter(上下文的背景下,字符串[]英雄){
超(背景下,R.layout.pager_item_list,R.id.listView1,英雄);
// TODO自动生成构造函数存根
this.heros =英雄;
this.context =背景;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根
LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看排= inflater.inflate(R.layout.pager_item_list,父母,假); Log.d(多少次,我真的不知道应该如何); ViewPager寻呼机=(ViewPager)row.findViewById(R.id.pager);
pager.setId(位置);
pager.setAdapter(新PagerCustomAdapter()); pager.setCurrentItem(0); 返回行;
}
}
}
下面是我的pageAdapter
公共类PagerCustomAdapter扩展PagerAdapter {
@覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
返回2;
} @覆盖
公共对象instantiateItem(View容器,INT位置){
// TODO自动生成方法存根 LayoutInflater吹气=(LayoutInflater)container.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
INT标识= 0; Log.d(我在pagerAdater,我在这里,该死!); 开关(位置)
{
情况下0:
ID = R.layout.fragment_top_item;
打破;
情况1:
ID = R.layout.fragment_bottom_item;
打破;
}
查看查看= inflater.inflate(ID,NULL);
((ViewPager)容器).addView(视图,0);
返回视图。
} @覆盖
公共无效destroyItem(查看为arg0,ARG1 INT,对象ARG2){
((ViewPager)为arg0).removeView((查看)ARG2);
} @覆盖
公共布尔isViewFromObject(查看为arg0,ARG1对象){
// TODO自动生成方法存根
返回将arg0 ==((查看)ARG1);
} @覆盖
公共Parcelable saveState和(){
返回null;
}}
我已经试过这两个 pagerAdapter
&安培; FragmentPagerAdapter
和两个有同样的结果。
我添加了两个日志消息 getView()
和 instantiateItem()
和两个正在读在logcat的预期。既没有警告也没有错误在 LogCat中。
我是什么失踪?
更新
activity_main的XML
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>< ListView控件
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真正的>
< /&的ListView GT;< / RelativeLayout的>
pager_item_list的XML
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < android.support.v4.view.ViewPager
机器人:ID =@ + ID /寻呼机
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< /android.support.v4.view.ViewPager>
< / LinearLayout中>
我不知道你想实现..你想要当一个事件发生更改视图是什么?当你点击样的?还是要像SwipeListView相同的结果。
如果您要更改视图,您coult尝试使用 ViewFlipper 一>
ANC更改布局事件发生时。但我不知道这是你想要的结果。
I have read a lot of related questions regarding this question. However, none of them has been answered. I am trying to add a ViewPager
to each row in the ListView
where there are two layouts within the pager. I believe this is doable because of SwipeListView.
Now I have implemented it. However, a blank page is showing with a line in the middle. No rows are appearing.
Here is my code:
MainActivity + ArrayAdapter
public class MainActivity extends Activity {
ListView list;
String[] heros;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heros = getResources().getStringArray(R.array.heros);
list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new CustomAdapter(this, heros));
}
public class CustomAdapter extends ArrayAdapter<String>
{
String[] heros;
Context context;
public CustomAdapter(Context context, String[] heros) {
super(context, R.layout.pager_item_list, R.id.listView1, heros);
// TODO Auto-generated constructor stub
this.heros = heros;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.pager_item_list, parent, false);
Log.d("how many times", "I really don't know how it should be");
ViewPager pager = (ViewPager) row.findViewById(R.id.pager);
pager.setId(position);
pager.setAdapter(new PagerCustomAdapter());
pager.setCurrentItem(0);
return row;
}
}
}
Here is my pageAdapter
public class PagerCustomAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int id = 0;
Log.d("I am in pagerAdater", "I am here, dammit!");
switch(position)
{
case 0:
id = R.layout.fragment_top_item;
break;
case 1:
id = R.layout.fragment_bottom_item;
break;
}
View view = inflater.inflate(id, null);
((ViewPager)container).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
I have tried both pagerAdapter
& FragmentPagerAdapter
and both had the same result
I added two log messages in getView()
and instantiateItem()
and both are being read in logcat as expected. There are neither warnings nor errors in LogCat.
What am I missing?
Update
XML of activity_main
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
XML of pager_item_list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
I am not sure what you're trying to achieve.. Do you want to change the view when a event happens? like when you click on it? or do you want the same result like the SwipeListView.
If you want to change the view you coult try to use a ViewFlipper anc change the layout when the event occurs. But i'm not sure if this is the result you want
这篇关于ListView的行项目中ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!