我正在使用带有v4软件包的Google API 8(Android 2.2).
嗨,这是我的问题:
我有一个FragmentActivity,它有一个总是停留在屏幕上的菜单和一个小容器(一个FrameLayout),我放了许多片段.当我隐藏并显示片段时,我的应用程序工作正常,但是在我加载所有片段后崩溃,因为我使用的内存有限.在这种情况下,我必须在加载其他片段时删除一些片段,因此应用程序不会崩溃.但是这里出现了另一个问题,我删除后我的寻呼机没有重新加载,所有其他片段工作正常.只有我的寻呼机没有显示,它们不会崩溃,只是消失.这里有一些代码可以帮助您了解我的问题:
MyFragmentActivity.java具有以下布局:
<LinearLayout
...(some code from menu that doesn't matter)
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在那个FrameLayout中,我把所有碎片都放了.寻呼机片段是其中之一,其布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+android:id/some_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我实现了这个抽象类AbstractPagerFragment:
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class AbstractPagerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragment …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager fragmenttransaction
我想在我的应用程序中实现导航,如Pinterest或Trello,即导航+水平滚动的三个选项卡.我为此制作了一个自定义标签栏(因为我无法猜测如何使用导航TAB模式更改操作栏中的标签宽度).所以我有三个按钮可以从一个片段导航到另一个片段.现在我想像这两个例子一样实现水平滚动,以便在我的片段之间导航.我读过有关View Pager但我不知道它是否适合我的情况,因为我没有只有视图而是片段.有人有例子或想法怎么做吗?我可以在不同的活动中应用它吗?谢谢
好吧,我开始了解Android碎片,但这对我来说仍然很困惑.我需要一些帮助.因为它说自API级别11以来支持android片段,但您可以下载"support.v4"库包以获得更低级别的API.首先,我创建新的项目来尝试API级别15的片段.然后我做了同样的API级别8,它不起作用...我导入外部jar,它看到所有需要导入,因为它应该...似乎这是问题吗?
这是我的主要课程:
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class Fragment2Activity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<fragment
android:name="com.frag.Fragment2.frag"
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的片段类:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class frag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用SlidingMenu,并试图弄清楚如何告诉用户菜单何时可见.
我在MainFragment中有一个动画,我希望在菜单可见时停止.
我尝试过这样的方法:
//In MainFragment
//In my animation loop
// ResponsiveUIActivity is where menu is Init
ResponsiveUIActivity resp = (ResponsiveUIActivity) getActivity();
SlidingMenu sm = resp.getSlidingMenu();
if(sm.isFocused()){ //tried isActived(), isEnabled
//cancel animation
}
Run Code Online (Sandbox Code Playgroud)
如果菜单可见,这些似乎都没有告诉我.我无法区分Menu Fragment和MainFragment.
我目前试图使我的情节看起来像这样:

但我不知道如何自定义点在橙色填充周围有黑色笔划,所以我的点现在"胶合"到线条.

或者,至少如何使它看起来像这样(与线相同颜色的外圈).

有帮助吗?
是否可以为ICS版本设置一个开关小部件,但是有一个用于预ICS的复选框?如果是这样,怎么样?
我不担心其他组件,只有开关.
我的解决方案
看到switch和checkbox都从CompoundButton继承,我就是这么做的
((CompoundButton)findViewById(R.id.swTax)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
calculateValues();
}
});
Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个 MapFragment 和常规 Fragment。问题是当我在片段之间切换时,MapFragment 在后台。我有代码来查找 MapFragment 是否存在,但我需要代码来删除它。
代码:
FragmentMapView mapFragmentcheck = (FragmentMapView)getFragmentManager().findFragmentByTag("map");
if (mapFragmentcheck != null) {
Log.d("tag","max exists");
if (mapFragmentcheck.isVisible()) {
Log.d("tag","map is visble, remove it");
// Do remove here, but how?
}
}
else {
Log.d("tag","map does not exists");
}
Run Code Online (Sandbox Code Playgroud) 嘿我得到一个错误:getDefaultSharedPreferences在下面的代码中.我怎么解决这个问题?我试图从我的PreferenceActivity加载prefUsername的edittext值.我尝试使用getSharedPreferences但是当PreferenceActivity存储其数据时返回nullDefaultSharedPreferences.任何帮助都是适当的.
错误是:
The method getDefaultSharedPreferences(Context) in the type PreferenceManager is not applicable for the arguments (RunawayFragment)
全班:
public class RunawayFragment extends DialogFragment {
public static RunawayFragment newInstance() {
RunawayFragment f = new RunawayFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getDialog() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
View root = inflater.inflate(R.layout.runaway_fragment, container, false);
return root;
}
@SuppressLint("NewApi")
@Override
public void onStart() {
super.onStart();
// User Picture …Run Code Online (Sandbox Code Playgroud) android android-preferences sharedpreferences android-fragments