标签: android-fragments

无法在扩展片段的类中执行 AsyncTask

我的 AsyncTask、RecyclerAdapter 和 RecycleViewHolder 类在不扩展片段的活动中工作得很好。但是每当我尝试在片段类中执行 AsyncTask 时,我得到的只是一个 IDE 红旗(无法访问的语句)。有人帮助我如何在片段中执行 AsyncTask(我使用的是 Android studio 1.5.1。我搜索了答案但失败了,但得到了一个令我满意的答案。下面是我的代码。-谢谢。

public class TabFragmentNews extends Fragment {
    private static final String ARG_EXAMPLE = "this_is_a_constant";
    private String example_data;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_news, container, false);

       //Executing The Async Task Class Here
        newsupdate();

    }
    //Method For Executing The Async Task
    public void newsupdate() {
           BackgroundTask backgroundTask = new BackgroundTask(getActivity());
            backgroundTask.execute();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        example_data = getArguments().getString(ARG_EXAMPLE);
        Log.i("Fragment …
Run Code Online (Sandbox Code Playgroud)

java android android-asynctask android-fragments android-recyclerview

0
推荐指数
1
解决办法
620
查看次数

每次都调用 Fragment 中的 OnCreateView

我有一个ViewPager,我正在使用FragmentStatePagerAdapter来保存我的片段。现在的问题是,当我滑动OnCreateViewOnCreate 时每次都会调用 Fragment,并且我在 Fragment 中进行网络调用,这些调用会一次又一次地被调用,从而导致延迟。

我从这里复制

如何避免调用OnCreateViewOnCreateFragments

这是我的代码:

FragmentHolder.class

    public class ViewPagerAdapter extends SmartFragmentStatePagerAdapter {
    final int PAGE_COUNT = 3;
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private long baseId = 0;
    private String tabTitles[] = new String[] { "HOME", "DISCOVER", "MESSAGES" };
    private Context context;
    private TabLayout tabLayout;
    public int[] imageResId = {
            R.drawable.ic_ac_unit_black_24dp,
            R.drawable.ic_airport_shuttle_black_24dp,
            R.drawable.ic_all_inclusive_black_24dp
    };


    public ViewPagerAdapter(FragmentManager fm, Context …
Run Code Online (Sandbox Code Playgroud)

android oncreate android-fragments android-viewpager

0
推荐指数
1
解决办法
989
查看次数

在片段中使用 CoordinatorLayout 时如何使状态栏透明

我有一个问题,当我使用包含片段中的图像视图的 CoordinatorLayout 时,我无法使状态栏透明并在状态栏下绘制图像视图(在活动中它没问题)。

为了说明问题,我以cheesesquare为例。

在 cheesesquare 中有一个 CheeseDetailActivity 显示了一些这样的内容(这是我想要的效果):

在活动中 众所周知,在活动中很容易实现这种效果,我拿起关键代码:

  • 样式.xml(v21)

    <资源>

    <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo"> <item name="android:windowDrawsSystemBarBackgrounds">true <item name="android:statusBarColor">@android:color/transparent</style>

</资源>

  • 活动细节.xml

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">
    
        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />
    
    </android.support.design.widget.CollapsingToolbarLayout>
    
    Run Code Online (Sandbox Code Playgroud)

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">
    
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">
    
            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/cheese_ipsum" />
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin"> …
    Run Code Online (Sandbox Code Playgroud)

android android-fragments android-statusbar android-coordinatorlayout

0
推荐指数
1
解决办法
6173
查看次数

在 Android 中从 Fragment 刷新 Activity

我遵循了这个。我想ActivityFragment. 我有回按钮toolbar用于FragmentActivity通信。

这是我的按钮侦听器代码并刷新活动。我将其包含在片段类中:

final Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Handler().post(new Runnable() {

                @Override
                public void run()
                {
                    Intent intent = getActivity().getIntent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    getActivity().overridePendingTransition(0, 0);
                    getActivity().finish();

                    getActivity().overridePendingTransition(0, 0);
                    startActivity(intent);
                }
            });
        }
    });
Run Code Online (Sandbox Code Playgroud)

这工作正常,但它完全重新加载应用程序。在这里可以做什么,以便我可以避免重新加载操作并刷新活动?

android handler android-fragments android-studio

0
推荐指数
1
解决办法
7944
查看次数

当应用程序未在设备上运行时 FCM 打开活动并调用片段

我正在尝试fragment通过 Firebase 云消息传递在 Activity 中打开一个初始化。

片段调用在应用程序打开时起作用,但是一旦我关闭应用程序,推送通知只会打开 Activity 而不是片段。

我的活动.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //All my initial code

        Intent i = getIntent();
        Bundle extras = i.getExtras();

        if(extras != null) {
            String push = extras.getString("push");
            if (push != null) {
                Integer id = Integer.parseInt(extras.getString("id"));
                goToDetalleDenuncia(id);
            }
        }else
            goToMenu();
    }
Run Code Online (Sandbox Code Playgroud)

我可以做些什么来调试(或解决)这个问题?我无法使用 Logcat(至少使用 eclipse),因为此时应用程序已关闭

android android-fragments firebase firebase-cloud-messaging

0
推荐指数
1
解决办法
2593
查看次数

如何将(ImageView 和 TextView)向左对齐,(ImageView 和 TextView)向右对齐

我有一个标题栏,它有两个 ImageViews

一个左对齐,一个右对齐,如下图

<RelativeLayout
    android:id="@+id/headersection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#181b1c" >



    <ImageView
        android:id="@+id/img_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/img_des"
        android:src="@drawable/play" />



    <ImageView
        android:id="@+id/img_backicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:layout_marginLeft="10dp"
        android:contentDescription="@string/img_des"
        android:src="@drawable/back" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我想在每个图像旁边放一个 TextView,这样左边的 ImageView 旁边会有一个 TextView(与左边 ImageView 的左边对齐),右边的 ImageView 旁边会有一个 TextView(与右边的 ImageView 右边对齐) )...

我尝试了大约 30 种不同的编码方式,但无法弄清楚

这是我正在尝试做的一个例子

    <RelativeLayout
        android:id="@+id/headersection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#181b1c" >

<!-- NEED TO PUT A TEXTVIEW HERE THAT ALIGNS TO THE RIGHT IMAGE BELOW -->

    <TextView
        android:id="@+id/RIGHT_TEXT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RIGHT TEXT …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments

0
推荐指数
1
解决办法
596
查看次数

获取片段名称或 ID

我有framelayout一次在我Activity和其他时间的不同时间Fragment(经过相同framelayoutActivity)。

在 my 中MainActivity.class,我需要知道我的framelayout. 例如,我需要知道是否framelayout正在使用MyFragment1.classMyFragment2.class

我需要这样的东西(在这个例子中,log必须说我“你在 MyFragment1”):

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.framelayout);

 getSupportFragmentManager().beginTransaction()
        .replace(R.id.framelayout,new MyFragment1())
        .commit();

if (framelayout.getname.equals("package.MyFragment1.class"))
 Log.d("debug", "you are in MyFragment1");
else if (framelayout.getname.equals("package.MyFragment2.class"))
 Log.d("debug", "you are in MyFragment2");
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

java android android-fragments android-framelayout

0
推荐指数
1
解决办法
2万
查看次数

Android Studio 在片段中打开抽屉

我从应用程序中删除了操作栏,所以我需要创建按钮来自己打开抽屉 Activity 上的按钮工作正常但是当我在片段应用程序中使用它时停止工作(当我调用片段时)

我在 Activity 中使用的代码(工作正常的代码)

 ImageButton btn = (ImageButton)findViewById(R.id.drawerButton);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.openDrawer(Gravity.START);
            }
        });
Run Code Online (Sandbox Code Playgroud)

fragment_achievements.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.AchievementsFragment">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="28dp"
        android:layout_height="29dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent"
        android:scaleType="fitCenter"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_menu_button" />

    <WebView
        android:id="@+id/web_ach"
        android:layout_width="409dp"
        android:layout_height="602dp"
        android:layout_marginBottom="-9dp"
        android:layout_marginLeft="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteY="56dp" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

成就片段.java

package layout;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView; …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-studio android-drawer

0
推荐指数
1
解决办法
3536
查看次数

检查哪个 Activity 加载片段

有没有办法检查哪个活动从片段中加载片段?我有一个用户配置文件片段,它是从 2 个不同的活动加载的,但只有一个传递了一个值,因此当片段从不传递值的活动加载时,从包中获取值会引发空指针异常。或者是否有更好的方法将值传递到片段中,以便片段仅在从该特定活动加载时才能获取值?

android android-fragments

0
推荐指数
1
解决办法
2347
查看次数

如何将数据从底部工作表传递到android中的片段?

我正在尝试将包含一些 TextViews 的底部工作表中的值传递给片段。我的片段包含一个 EditText 字段和一个浮动操作按钮。当用户单击浮动操作按钮时,底部工作表会显示其中有许多 TextViews,当用户单击底部工作表上的任何 textViews 时,值或者该 TextView 的字符串应该显示在片段的 editText 字段中,并且应该关闭底部工作表。

我曾尝试在 setOnShowListener 方法中实现 onClickListener 但它似乎不起作用。

这是我的代码:

Fragment_TextPropert1_EditText.xml

<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"
    tools:context="com.example.sumitroy.TextProperty1_EditText"
    >
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:cardCornerRadius="15dp"
        android:padding="15dp"
        android:layout_below="@+id/view"
        android:layout_marginTop="10dp"
        app:cardUseCompatPadding="true"
        android:id="@+id/view2">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"
            android:hint="Enter Your Comments Here.."
            android:id="@+id/userText"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

    </android.support.v7.widget.CardView>

<ImageButton
      android:layout_width="65dp"
      android:layout_height="65dp"
      android:id="@+id/example_Ads"
      android:background="@drawable/oval"
      android:layout_alignParentBottom="true"
      android:layout_alignParentEnd="true"
      android:layout_marginBottom="20dp"
      android:layout_marginRight="15dp"
      android:src="@drawable/double_plus"
     />


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

TextProperty1_bottomsheet.xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" …
Run Code Online (Sandbox Code Playgroud)

android android-fragments bottom-sheet

0
推荐指数
1
解决办法
3662
查看次数