gpt4 book ai didi

java - 在 View 页面中使用更多 View

转载 作者:行者123 更新时间:2023-12-01 05:37:38 27 4
gpt4 key购买 nike

如何在 View 页面中使用不同的 View ?我想不通。

例如,带有列表框的 View 和带有按钮的 View 。

这是我的代码:

如何在 View 页面中使用不同的 View ?我想不通。

例如,带有列表框的 View 和带有按钮的 View 。

这是我的代码:

package com.stylingandroid.viewpager;

import com.viewpagerindicator.TitlePageIndicator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class ViewPagerActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ViewPagerAdapter adapter = new ViewPagerAdapter(this);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
pager.setAdapter(adapter);
indicator.setViewPager(pager);
}
}

这里是适配器:

package com.stylingandroid.viewpager;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.viewpagerindicator.TitleProvider;

public class ViewPagerAdapter extends PagerAdapter
implements TitleProvider
{
private static String[] titles = new String[]
{
"Page 1",
"Page 2",
"Page 3"
};
private final Context context;

public ViewPagerAdapter( Context context )
{
this.context = context;
}

@Override
public String getTitle( int position )
{
return titles[ position ];
}

@Override
public int getCount()
{
return titles.length;
}

@Override
public Object instantiateItem( View pager, int position )
{
TextView v = new TextView( context );
v.setText(titles[position]);

((ViewPager)pager).addView(v,0);

return v;
}

@Override
public void destroyItem( View pager, int position, Object view )
{
((ViewPager)pager).removeView( (TextView)view );
}

@Override
public boolean isViewFromObject( View view, Object object )
{
return view.equals( object );
}

@Override
public void finishUpdate( View view ) {}

@Override
public void restoreState( Parcelable p, ClassLoader c ) {}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate( View view ) {}
}

最佳答案

您需要修改 PagerAdapter 中的 instantiateItem() 方法并动态构造每个 View ,而不是每次简单地添加 TextView。例如,您可以为每个位置扩展不同的布局:

@Override
public Object instantiateItem( View pager, int position )
{
LayoutInflator inflator = LayoutInflator.from( context );
View v = null;
switch( position )
{
case 0:
v = inflator.inflate( R.layout.layout1, (ViewPager)pager, false );
break;
case 1:
v = inflator.inflate( R.layout.layout2, (ViewPager)pager, false );
break;
.
.
.
}
v.setText(titles[position]);

((ViewPager)pager).addView(v,0);

return v;
}

关于java - 在 View 页面中使用更多 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7902528/

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