- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个使用寻呼机作为导航的安卓应用程序。对于选项卡,我有 3 种布局作为内容。其中一个 fragment 是一个画廊,我想向其中添加图像。为此,我必须设置一个 ImageAdapter,但我需要知道如何访问 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()));
我在 onCreate 方法中使用上面的这段代码,从不是内容 View 的布局中获取图库。我必须为 ImageAdapter 提供上下文。但是我必须在那里设置什么上下文?
编辑:这是我的完整代码:
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);
}
}
}
最佳答案
fragment 没有自己的上下文,它们使用父 Activity 。
getActivity()
getActivity().getApplicationContext()
尽可能选择应用上下文。
更新:
fragment 的getActivity()
返回一个 Activity 实例当且仅当,所述 fragment 当前附加到一个 Activity。
所以,
Fragment f = new MyFragment();
创建一个 fragment ,但它还没有附加到 Activity 。因此 f.getActivity()
返回 null
。
添加到 Activity 后:
getFragmentManager().beginTransaction().add(f,"fragment").commit();
现在,getActivity()
将返回一个 Activity 实例。
同样,如果我们从 Activity 中分离 fragment :
getFragmentManager().beginTransaction().detach(f).commit()
getActivity()
将再次返回一个 null
值。
因此,我们不应该在 Fragment
类之外使用 getActivity()
,因为我们无法确定附加状态。因此,我建议您在 fragment 自己的类中的方法中使用 getActivity()
:onAttach()
、onCreate()
或 onActivityCreated()
。
关于Android - 在寻呼机中获取 fragment 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15848809/
我需要能够垂直翻转一组图像。我看过使用 ViewPager,但它似乎只支持水平分页。 我也查看了 Gallery,但我也没有看到垂直分页的方法。无论如何,它已被弃用,取而代之的是 Horizont
我正在使用寻呼机中内置的 View ,并且希望有一个“显示全部”选项。即显示全部 第 2 页,共 10 页。 实现这一目标的最佳方法是什么? 史蒂夫 最佳答案 史蒂文,您可以克隆您的实际显示,然后在
当我使用 vim 编辑 Markdown 文件时,我有很好的语法着色(通过 vim-markdown 和 vim-markdown-preview 插件)。 当我使用 less(寻呼机)查看文件时,我
在我的 Android 应用程序中,我已经能够在一段时间后自动滚动查看寻呼机。我找不到显示滚动发生前剩余时间的寻呼机指示器。 我在下面附上了一个网站的屏幕截图,以了解我想要实现的目标。 如果您可以共享
当您编写的每个代码都立即运行并且您未按时完成计划时,您是否知道这种感觉:-P 就像'哦,是的,现在我有时间让它变得完美'。这就是我现在的位置^^ 所以我用 JSF (ui:repeat) 实现了一个转
我在 Android 应用程序中有一个项目列表,我希望每个项目都在一个页面上。问题是每个项目都设置了一个带有 imageview 的布局,它从一个唯一的 url 下载它的图像。这有可能实现吗?如何实现
我想同步两个 View 寻呼机之间的触摸事件。我已经试过了,但没有发现任何效果。 viewPager.setOnTouchListener(new View.OnTouchListener() {
我必须构建具有无限自动滚动功能的 View 寻呼机。我还需要在 View 寻呼机下方显示一个页面指示器,它应该根据滚动事件做出响应。目前我已经为无限自动滚动应用了以下逻辑: public void s
我正在尝试按照一些教程使用 View 寻呼机创建一个自动图像 slider 。我让一切正常,但后来我看到了 编舞:跳过 1 帧!应用程序可能在其主线程上做了太多工作。 并搜索了错误。` 你看,在我的代
如何从viewpager的第0个index跳转到view pager的最后一个index?那就是如何让view pager循环呢?如果用户向左滑动,我想跳转到最后一个索引;如果用户向右滑动,我想从最后
我在一个 iOS 应用程序上工作,它从服务器获取视频和图像,每个对象都有很多图像和视频,我需要在 slider (寻呼机)中显示这些图像和视频,用户可以在其中滑动以获取下一个,并且我需要将这些视频和图
我们目前正在使用 VS 2008 C# 开发 Windows 窗体应用程序。此应用程序用于阅读长(200 - 300 页)的法律文件,它处理大约 30 - 40 个文档。应用程序在文档文本中搜索,在文
我正在开发一个项目,其中我在一个屏幕上使用 View Pager,如下所示: 但是你可以在那里看到,有一个上层布局,其中列出了根据显示的页面选择的年份。当我们滑动 View 寻呼机时,年份也会根据显示
我需要在带有图像的 View 寻呼机文件中获取页面指示器。这是我的代码。 public class IndicatorActivity extends Activity { /** Called w
有没有办法用代表每张幻灯片的单词替换寻呼机中 slider 的圆圈(就像可单击的菜单,滑动其下方的内容)?选项在这里http://bxslider.com/options但不确定哪一个可以实现这一目标
ViewPager 叠加在 fab 之上。如何将 fab 放在首位? xml: 或者也许这不是原因?当我在上面时,只是没有按下按钮,但选项卡被翻转。 最佳答案 Coordinat
我正在尝试实现一个垂直可滑动的ViewPager,它有一堆卡片,就像外观一样。 我能够使用 ViewPager.PageTransformer 并交换触摸点来实现 VerticalViewPager。
我无法以编程方式创建和添加 View 寻呼机。 我找到了这个问题Android create ViewPager programmatically但接受的答案对我不起作用。 相关代码如下。
我有一个 View 寻呼机,其中每个页面都是一个自定义 View ( ... 而不是 Fragment ),我正在使用 LayoutInflator 进行扩充。 .每个自定义 View 都有一个 Im
我已经使用 jQuery 设置了一个动画 slider : jsFiddle 演示:http://jsfiddle.net/neal_fletcher/9zRDV/ 原因是我想在幻灯片上实现轻微的悬垂
我是一名优秀的程序员,十分优秀!