我在使用寻呼机作为导航Android应用程序。对于标签我有3个布局的内容。在片段中的一个是一个画廊,我想图像添加到它。为此,我必须设置一个ImageAdapter但我需要知道如何访问片段的内容。
最后LayoutInflater厂= getLayoutInflater();
最终的视图中查看= factory.inflate(R.layout.pictures,NULL);
画廊G =(图库论坛)view.findViewById(R.id.gallery1);
g.setAdapter(新ImageAdapter(view.getContext()));
我用这个code以上的onCreate方法来从一个布局,不是内容查看画廊。 我得给一个上下文的ImageAdapter。但是,做什么方面,我有设置有?
编辑:这是我的完整code:
包com.bw2801.uwelugemediathek;
进口java.util.Locale中;
进口android.app.ActionBar;
进口android.app.FragmentTransaction;
进口android.content.Context;
进口android.database.DataSetObserver;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.BaseAdapter;
进口android.widget.Gallery;
进口android.widget.ImageView;
进口android.widget.SpinnerAdapter;
进口android.widget.Toast;
公共类MainActivity扩展FragmentActivity工具
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
PicturesSectionFragment PS =新PicturesSectionFragment();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//设置操作栏。
最后的动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//创建将返回一个片段为三个的适配器
//应用程序的主要部分。
mSectionsPagerAdapter =新SectionsPagerAdapter(
getSupportFragmentManager());
//设置的ViewPager与部分适配器。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//当不同部分之间的刷卡,选择相应的
//标签。我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有
//引用的标签。
mViewPager
.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//对于每个应用程序的章节中,添加一个标签,操作栏。
的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
//创建与对应于由所定义的页标题文本标签
//适配器。同时,指定此活动的对象,它实现
//该TabListener界面,回调(监听程序)进行时
//这个选项卡中选择。
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(I))
.setTabListener(本));
}
最后LayoutInflater厂= getLayoutInflater();
最终的视图中查看= factory.inflate(R.layout.pictures,NULL);
画廊G =(图库论坛)view.findViewById(R.id.gallery1);
g.setAdapter(新ImageAdapter(ps.getActivity()));
}
公共类ImageAdapter扩展了BaseAdapter {
私人语境mContext;
私人整数[] mImageIds = {
R.drawable.image01,
R.drawable.image02,
R.drawable.image03,
R.drawable.image04,
R.drawable.image05,
R.drawable.image06,
R.drawable.image07,
R.drawable.image08,
};
公共ImageAdapter(上下文C){
mContext = C;
}
公众诠释getCount将(){
返回mImageIds.length;
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ImageView的我=新ImageView的(mContext);
i.setImageResource(mImageIds [位置]);
i.setLayoutParams(新Gallery.LayoutParams(150,100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
返回我;
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
//当选择给定的标签,切换到对应页面
//该ViewPager。
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}
@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,
FragmentTransaction fragmentTransaction){
}
/ **
* A {@link FragmentPagerAdapter}返回对应的片段
*章节/标签/页之一。
* /
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
公共SectionsPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT位置){
开关(位置){
情况下0:
返回新DummySectionFragment();
情况1:
返回新SoundSectionFragment();
案例2:
返回PS;
}
返回新DummySectionFragment();
}
@覆盖
公众诠释getCount将(){
返回3;
}
@覆盖
公共CharSequence的getPageTitle(INT位置){
开关(位置){
情况下0:
回到信息详细信息;
情况1:
返回音板;
案例2:
返回画廊;
}
返回null;
}
}
公共静态类DummySectionFragment扩展片段{
公共DummySectionFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.info,集装箱,假);
}
}
公共静态类PicturesSectionFragment扩展片段{
公共PicturesSectionFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.pictures,集装箱,假);
}
}
公共静态类SoundSectionFragment扩展片段{
公共SoundSectionFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
返回inflater.inflate(R.layout.sounds,集装箱,假);
}
}
}
解决方案
您可以使用这样
g.setAdapter(新ImageAdapter(getActivity()));
I have a android application using a pager as navigation. For the tabs I have 3 layouts as content. On one of the fragments is a gallery and I want to add images to it. For that I have to set an ImageAdapter but I need to know how to access the context of a fragment.
final LayoutInflater factory = getLayoutInflater();
final View view = factory.inflate(R.layout.pictures, null);
Gallery g = (Gallery) view.findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(view.getContext()));
I use this code above in the onCreate method to get the gallery from a layout that is not the contentview. I have to give a Context to the ImageAdapter. But what context do I have to set there?
EDIT: This is my complete code:
package com.bw2801.uwelugemediathek;
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
PicturesSectionFragment ps = new PicturesSectionFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
final LayoutInflater factory = getLayoutInflater();
final View view = factory.inflate(R.layout.pictures, null);
Gallery g = (Gallery) view.findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(ps.getActivity()));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
R.drawable.image01,
R.drawable.image02,
R.drawable.image03,
R.drawable.image04,
R.drawable.image05,
R.drawable.image06,
R.drawable.image07,
R.drawable.image08,
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new DummySectionFragment();
case 1:
return new SoundSectionFragment();
case 2:
return ps;
}
return new DummySectionFragment();
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Informationen";
case 1:
return "Soundboard";
case 2:
return "Galerie";
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.info, container, false);
}
}
public static class PicturesSectionFragment extends Fragment {
public PicturesSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.pictures, container, false);
}
}
public static class SoundSectionFragment extends Fragment {
public SoundSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.sounds, container, false);
}
}
}
解决方案
you can use like this
g.setAdapter(new ImageAdapter(getActivity()));
上一篇:如何为我的应用程序选择多播地址?为我、应用程序、地址
下一篇:java.lang.RuntimeException的:申请表] org.eclipse.ui.ide.workbench&QUOT;无法在注册表中找到申请表、注册表、中找到、org