标签: android-fragments

片段中的runOnUiThread

我正在尝试将Activity转换为片段.错误标记打开runOnUiThread.在过去:

GoogleActivityV2从Activity扩展而来.类ExecuteTask中的runOnUiThread.class ExecuteTask嵌套在activity上.

(运行确定)现在:

GoogleActivityV2从Fragment扩展而来.类ExecuteTask中的runOnUiThread.class ExecuteTask嵌套在activity上.(runOnUiThread出错)

这是我的代码

public class GoogleActivityV2 extends SherlockMapFragment implements OnMapClickListener , OnMapLongClickListener , OnCameraChangeListener , TextWatcher {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.activity_googlev2, container, false);
        Init();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line);
        textView = (AutoCompleteTextView) getView().findViewById(R.id.autoCompleteTextView1);
        return rootView;
    }

    public void onCameraChange(CameraPosition arg0){
        // TODO Auto-generated method stub
    }

    public void onMapLongClick(LatLng arg0){
        llLoc = arg0;
        stCommand = "onTouchEvent";
        lp = new ExecuteTask();
        lp.execute();
    }

    public void onMapClick(LatLng …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-activity

106
推荐指数
3
解决办法
10万
查看次数

为片段设置主题

我正在尝试为片段设置主题.

在清单中设置主题不起作用:

android:theme="@android:style/Theme.Holo.Light"
Run Code Online (Sandbox Code Playgroud)

通过查看以前的博客,似乎我必须使用ContextThemeWrapper.任何人都可以参考我的编码示例吗?我找不到任何东西.

android android-theme android-fragments

103
推荐指数
8
解决办法
7万
查看次数

进一步了解setRetainInstance(true)

什么究竟,当你调用发生setRetainInstance(true)Fragment?文档几乎不存在,这似乎是一个非常重要的功能.具体来说,我想知道这个序列(我编造的)有多少是真的:

  1. 用户旋转设备.
  2. 该片段从分离ActivityFragment.onDetach()被调用.
  3. 活动被摧毁; Activity.onDestroy()叫做.
  4. 所述Activityjava对象被删除(如果可能,通过GC).
  5. Activity创建一个新的java对象; 它的构造函数,并被onCreate()调用.
  6. Activity.onCreate()我们要么setContentView(...)设置包含片段的布局,要么我们FragmentTransaction 用来添加片段.
  7. 我真的不确定这一点,但我认为android很聪明,可以找到旧的片段,并呼吁Fragment.onAttach()将它重新连接到新的Activity
  8. Activity.onResume()调用下一个(或之前?谁知道?).

那是对的吗?即使我FragmentTransaction.add(new MyFragment(), ...)第一次明确使用,Android是否足够智能找到旧片段?如果是这样,我如何避免添加另一个片段onCreate()?我需要做这样的事吗?:

    if (getSupportFragmentManager().findFragmentByTag("foo") == null)
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(new FooFragment(), "foo").commit();
    }
Run Code Online (Sandbox Code Playgroud)

android rotation android-fragments android-activity

103
推荐指数
2
解决办法
5万
查看次数

Android 4.2:嵌套片段的后台堆栈行为

在Android 4.2中,支持库获得了对嵌套片段的支持,请参见此处.我玩过它并发现了一个有关back stack和getChildFragmentManager()的有趣行为/ bug .当使用getChildFragmentManager()和addToBackStack(String name)时,通过按下后退按钮,系统不会将后栈运行到前一个片段.另一方面,当使用getFragmentManager()和addToBackStack(String name)时,按下后退按钮,系统将返回上一个片段.

对我来说,这种行为是出乎意料的.通过按下设备上的后退按钮,即使片段被添加到子节点片段管理器中的后栈,我也希望弹出最后添加到后栈的片段.

这种行为是否正确?这种行为是个错误吗?这个问题有解决方法吗?

使用getChildFragmentManager()的示例代码:

public class FragmentceptionActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    final FrameLayout wrapper1 = new FrameLayout(this);
    wrapper1.setLayoutParams(new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
    wrapper1.setId(1);

    final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;

    final TextView text = new TextView(this);
    text.setLayoutParams(params);
    text.setText("fragment 1");
    wrapper1.addView(text);

    setContentView(wrapper1);

    getSupportFragmentManager().beginTransaction().addToBackStack(null)
            .add(1, new Fragment1()).commit();
}

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-nested-fragment

103
推荐指数
6
解决办法
4万
查看次数

在Android中使用Fragments而不是Views有什么好处?

开发时Android,您可以将目标(或最小)sdk设置为4(API 1.6)并添加android兼容包(v4)以添加支持Fragments.昨天我做了这个并成功实现Fragments了可视化来自自定义类的数据.

我的问题是:使用的好处是什么Fragments,而不是简单地从自定义对象获取View,并且仍然支持API 1.5?

例如,假设我有类Foo.java:

public class Foo extends Fragment {

    /** Title of the Foo object*/
    private String title;
    /** A description of Foo */
    private String message;

    /** Create a new Foo
     * @param title
     * @param message */
    public Foo(String title, String message) {
        this.title = title;
        this.message = message;
    }//Foo

    /** Retrieves the View to display (supports API 1.5. To use,
     * remove 'extends Fragment' from the class …
Run Code Online (Sandbox Code Playgroud)

android software-design android-lifecycle android-fragments android-view

102
推荐指数
2
解决办法
4万
查看次数

如何在片段之间传递值

我很擅长使用Fragments.

我只是想构建一个使用Fragments的简单示例应用程序.我的方案是,我有两个活动,每个活动中有一个片段.第一个片段有一个edittext和一个按钮.第二个片段有一个textview.当我在edittext中输入名称并单击按钮时,第二个片段中的textview应显示在第一个片段的edittext中输入的名称.

我能够将第一个片段的值发送到它的活动,然后从该活动发送到第二个活动.现在我如何在第二个片段中使用此值.

这是Java代码:::

package com.example.fragmentexample;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Fragment_1 extends Fragment{

    OnFragmentChangedListener mCallback;

    // Container Activity must implement this interface
    public interface OnFragmentChangedListener {
        public void onButtonClicked(String name);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

102
推荐指数
8
解决办法
21万
查看次数

如何从片段中获取工具栏?

ActionBarActivityNavigationDrawer和使用support_v7 Toolbar的动作条.在我的一个片段工具栏中有自定义视图.在其他片段中Toolbar应显示标题.

如何Toolbar从片段中获取自定义实例?我可以使用ActionBar getActivity().getActionBar(),但是如果我调用setTitle()这个实例ActionBar它什么都不做.

UPD:

就我而言

((ActionBarActivity) getActivity()).getSupportActionBar().setTitle();

(正如MrEngineer13所说)在第一次片段创建时不起作用,因为我从onHiddenChanged()调用它.现在我向onCreateView()添加更多一个,它工作正常.

android android-fragments android-actionbar android-support-library android-toolbar

102
推荐指数
4
解决办法
14万
查看次数

在notifyDatasetChanged()之后RecyclerView闪烁

我有一个RecyclerView,它从API加载一些数据,包括一个图像URL和一些数据,我使用networkImageView来延迟加载图像.

@Override
public void onResponse(List<Item> response) {
   mItems.clear();
   for (Item item : response) {
      mItems.add(item);
   }
   mAdapter.notifyDataSetChanged();
   mSwipeRefreshLayout.setRefreshing(false);
}
Run Code Online (Sandbox Code Playgroud)

这是适配器的实现:

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (isHeader(position)) {
            return;
        }
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        MyViewHolder holder = (MyViewHolder) viewHolder;
        final Item item = mItems.get(position - 1); // Subtract 1 for header
        holder.title.setText(item.getTitle());
        holder.image.setImageUrl(item.getImg_url(), VolleyClient.getInstance(mCtx).getImageLoader());
        holder.image.setErrorImageResId(android.R.drawable.ic_dialog_alert);
        holder.origin.setText(item.getOrigin());
    }
Run Code Online (Sandbox Code Playgroud)

问题是当我们在recyclerView中刷新时,它在开始时很短暂地看起来很奇怪.

我只使用了GridView/ListView,它按预期工作.没有瞎眼.

RecycleView的配置 …

android android-fragments android-recyclerview

102
推荐指数
9
解决办法
5万
查看次数

Android片段中的getIntent().getExtras()在哪里/如何?

通过活动,我曾经这样做:

在活动1中:

Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class);
                i.putExtra("name", items.get(arg2));
                i.putExtra("category", Category);
                startActivity(i);
Run Code Online (Sandbox Code Playgroud)

在活动2中:

Item = getIntent().getExtras().getString("name");
Run Code Online (Sandbox Code Playgroud)

你是如何使用Fragments做到这一点的?我也在使用兼容性库v4.

它是否属于FragmentActivity?还是实际的碎片?它采用哪种方法?的onCreate?onCreateView?另一个?

我可以看一下示例代码吗?

编辑:值得注意的是我试图将Activity 1保持为Activity(或者实际上是ListActivity,我点击时传递listitem的意图)然后传递给一组tabbed-fragments(通过Fragment Activity)和我需要任何一个标签才能获得额外的东西.(我希望这可能吗?)

android android-intent android-fragments android-activity

101
推荐指数
2
解决办法
14万
查看次数

片段中未调用OnCreateOptionsMenu()


我有一个应用程序,它有一个活动,水平放置2个片段.
在我的活动中,我使用onCreateOptionsMEnu()对菜单进行了充气,​​使用该菜单我可以显示操作栏选项菜单.但是当我尝试从我的一个片段更新操作栏菜单项时,onCreateoptionsmenu()没有被调用.所以,我无法更新操作栏选项菜单.任何人都可以帮我解决这个问题.
我的活动中的代码如下:
myActivity.java
===============

package com.andr.androidtablelist;

import java.io.IOException;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.andr.fragments.ListLeftFragment;
import com.andr.utils.DBHelper;

      public class TabletActivity extends Activity {
    ActionBar actionBar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DBHelper dbhelper;
        dbhelper = new DBHelper(this);
        try {
            dbhelper.createDatabase();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        setContentView(R.layout.activity_tablet);
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        getFragmentManager().beginTransaction()
                .replace(R.id.master, ListLeftFragment.newInstance()).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.tablet, …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

100
推荐指数
4
解决办法
4万
查看次数