标签: android-fragments

如何通过操作栏返回上一个活动?

我有一个带有操作栏的活动,我想返回到以前的活动,我该如何使用操作栏来做到这一点?

android android-layout android-fragments android-actionbar

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

在片段中重新膨胀视图是什么意思?

在将用户界面添加到片段时,它显示每次调用 onCreateView 都会使视图膨胀:

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

参考: http //developer.android.com/guide/components/fragments.html#UI

如果我缓存膨胀的视图并在下一次调用时返回它,例如:

public static class ExampleFragment extends Fragment {
    private View mView = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(mView == null){
            mView = inflater.inflate(R.layout.example_fragment, container, false);
        }else{
            //detach mView from previous parent if exist
            ...
        }
        return mView;
    } …
Run Code Online (Sandbox Code Playgroud)

android fragment android-inflate android-fragments

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

Android - 片段 findViewById() 始终为空?

是的,我意识到有些问题与这个问题非常相似,但与其他问题不同的是,我的onCreateView()方法夸大了片段的视图。当我不使用 时findViewById(),一切都会膨胀,我的应用程序按预期工作(除了我想要进入的视图功能findViewById()

这是我的代码:

@Override
public View onCreateView(LayoutInflater p_inflater, ViewGroup p_container, Bundle p_prev_state)
{
    return p_inflater.inflate(R.layout.fragment_main, p_container, false);
}
Run Code Online (Sandbox Code Playgroud)

每当我想访问布局中的 ListView 时,我都会这样做:

private ListView getListView()
{
    return (ListView)getView().findViewById(R.id.main_events);
}
Run Code Online (Sandbox Code Playgroud)

但这始终为空。为什么?我的fragment_main.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"
    android:background="@color/white"
    tools:context="me.k.cal.MainActivity$PlaceholderFragment" >

    <ListView android:id="@+id/main_events"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="1dp"
        android:divider="@color/background_color" />

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

我的完整片段代码可以在这里找到。

java xml android android-fragments

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

为什么我的片段中的活动上下文为空?

我正在尝试创建一个样板片段。当我尝试设置 RecyclerView 的 LayoutManager 时,我不断收到空引用错误:

尝试在空对象引用上调用虚拟方法“void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)”

以下是我调用以获取错误的顺序:

主类:

public class PagerAdapter extends FragmentPagerAdapter {
    ...
    @Override
    public Fragment getItem(int position){
        switch(position){
            default:
                return new WhatsNew();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

WhatsNew.class:

public class WhatsNew extends Base {

    private LayoutInflater mInflater;
    private Handler mHandler = new Handler();

    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    public void init() {
        mInflater = LayoutInflater.from(mContext);
        mLayoutManager = new LinearLayoutManager(mContext);
        mDataView.setLayoutManager(mLayoutManager);  //<-- Where the null reference is
    }

    @Override
    public void doWork() {

        WhatsNewAdapter mAdapter = new WhatsNewAdapter();
        mDataView.setAdapter(mAdapter);
        ... …
Run Code Online (Sandbox Code Playgroud)

java android android-fragments

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

即使在任务完成后,刷卡刷新轮也不会消失(Android)

我在我的 android 应用程序中创建了一个滑动刷新布局。主要活动片段中有一个 ListView,当用户滑动列表视图时,应用程序从互联网获取数据以显示在列表视图中。我的问题是,即使在(正确)获取数据之后,滑动刷新轮也不会离开应用程序。这是我的主要片段 xml 文件的代码:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ListView
    android:id="@+id/listview_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#00000000"
    android:dividerHeight="4dp"/>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的主要活动片段文件的代码(部分)

swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.activity_main_swipe_refresh_layout);
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.v(TAG, "Swiped!") ;
        }
    });
Run Code Online (Sandbox Code Playgroud)

我在 Main 活动片段的 onCreateView 方法中创建了刷新侦听器。我能做些什么来纠正它?

android listview android-fragments

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

ViewPager 不显示页面内容/更改视图后不刷新

我在我的一个应用程序片段中使用 ViewPager / FragmentPagerAdapter 在每个页面中显示不同的列表视图(见下图): 在此处输入图片说明

问题是,在我更改当前视图(片段)并再次重新打开它后,列表视图未呈现(见下图):

在此处输入图片说明

行为是随机的,有时当我在不同的选项卡/页面之间滑动时会显示列表视图。

这是我的片段代码:

public class AstucesFragment extends Fragment implements ActionBar.TabListener {

    ActionBar actionBar;
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    ViewPager mViewPager;
    public static String[] tabslist = new String[5];

    public AstucesFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_astuces, container, false);

        tabslist[0] = getActivity().getResources().getString(R.string.astuces_quotidien);
        tabslist[1] = getActivity().getResources().getString(R.string.astuces_resto);
        tabslist[2] = getActivity().getResources().getString(R.string.astuces_loisirs);
        tabslist[3] = getActivity().getResources().getString(R.string.astuces_transports);
        tabslist[4] = getActivity().getResources().getString(R.string.astuces_tous);

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getActivity().getSupportFragmentManager());

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager = (ViewPager) view.findViewById(R.id.astuces_pager); …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-viewpager

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

热更改片段内父 TextView 的文本?

在我Fragemt.java有这样的事情:

public class MainFragment extends Fragment implements View.OnClickListener {
    private TextView mTitleTextView;
    [...] irrelevant code cut out

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        [...] some other code

        mTitleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
        mTitleTextView.setText("Text I Want to Set"); // Problem! App crashes on start if TextView isn't part of the fragment
        [...] more irrelevant code
Run Code Online (Sandbox Code Playgroud)

这现在工作正常。在应用程序总是crashed加载之后,我搜索了几个小时。问题是TextView (R.id.titleTextView)位于XML父活动的 中,而不是分配给片段的 xml。

有没有办法可以TextView从片段java代码中更改父母的文本?

编辑 logcat …

android android-fragments

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

切换片段时滑动标签滞后

我有一个小问题,我只有 3 个片段,我在其中一个片段中添加了一个 Photoshop 背景(1080 x 1920),问题是当它加载到手机时,选项卡之间的切换会滞后很多,但如果我只是渲染图像为 720 x 1280 它工作正常并且没有出现延迟,我实现了mViewPager.setOffscreenPageLimit(0); 但它似乎不起作用,我不知道是什么导致了问题,我将在这里提供我的主要代码,在此先感谢

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * …
Run Code Online (Sandbox Code Playgroud)

java tabs android swipe android-fragments

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

运行片段事务一旦打开,就会停止应用程序

如果我在事务中删除.commit(),应用程序将编译但显示空屏幕.如果我添加.commit(),应用程序会立即打开和关闭.我是片段新手,任何人都可以帮助解决这个问题.我只是添加了一个ImageView布局.在Fragment类中对其进行实施,并在其中硬编码图像.此外,我刚刚在MainActivity中创建了一个Fragment实例.

我的Android Studio版本是:3.1.3

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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=".MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="@+id/head_container"
/>


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

body_part_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:id="@+id/body_part_frag"
>

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

BodyPartFragment.java

 package com.example.musa.my_frag_app;

 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;

public class BodyPartFragment extends Fragment {

public BodyPartFragment(){
    //Do Nothing
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View rootView = inflater.inflate(R.layout.body_part_fragment, container);
    ImageView body_image = rootView.findViewById(R.id.body_part_frag);
    body_image.setImageResource(R.drawable.ic_launcher_background);

  return rootView;
}

}
Run Code Online (Sandbox Code Playgroud)

MainActivity.java …

android android-fragments

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

Android-具有CHIPS风格的自定义标签布局?

如何在标签中获得这种外观?
描述我需要的样式的图像

我希望它:

•根据查看的页面更改文本的颜色(我已经通过默认的supportLibrary的Widget TabLayout实现了此功能)
•更改了背景笔触的颜色
•每个选项卡元素之间都有间距(无法通过padding选项实现)

我搜索了图书馆吗,但没有任何有用的细节,实际上,我不知道在搜索栏中键入什么内容。
这种风格的名称是什么?

提前致谢。

tabs android android-layout android-fragments android-studio

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