有碎片添加的条件 [英] having a condition to fragment adding
问题描述
是清楚的,我需要在特定条件下添加一个片段,出于我的目的,条件可以是:
a。如果父母的身份证与我的要求相符。
b。如果片段的ID符合我的要求。
我试图将条件放入片段本身:
公共类BlockFrag扩展Fragment {
SharedPreferences sp;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
sp = getActivity()。getSharedPreferences( myOptions,Context.MODE_PRIVATE);
int id = container.getId(); //没有用(我认为容器由于某种原因为空
开关(id)
{
默认值:返回充气机.inflate(R.layout.nothing,container,false);
case(R.id.redFrame):{
if(sp.getBoolean( redBlock,true)){
return inflater.inflate(R.layout.block_frag,container,false);
}
}
case(R.id.greenFrame):{
if( sp.getBoolean( greenBlock,true)){
return inflater.inflate(R.layout.block_frag,container,false);
}
我也尝试以编程方式获取条件,但是当我调用放置的布局时,我无法理解如何防止片段自动生成。
这是它放置的布局
< FrameLayout
android:layout_width = match_parent
androi d:layout_height = match_parent
android:id = @ + id / redFrame
android:layout_weight = 0.33333>
<片段
android:name = com.example.dor.myapplication.BlockFrag
android:id = @ + id / blockRed
android:layout_width = match_parent
android:layout_height = match_parent
工具:layout = @ layout / block_frag />
如果重要的话,这是片段的布局...
< FrameLayout xmlns:android = http://schemas.android.com/apk/res/android
android:layout_width = match_parent
android:layout_height = match_parent
android:layout_alignParentLeft = true
android:layout_alignParentStart = true>
< ImageView
android:layout_width = 118dp
android:layout_height = match_parent
android:id = @ + id / blockFrag
android:src = @ drawable / block
android:layout_weight = 0.33333
android:layout_above = @ + id / linearLayout
android:layout_alignParentLeft = true
android:layout_alignParentStart = true
android:layout_gravity = left
android:scaleType = fitXY />
感谢您的帮助
谢谢你的回答,但我不知道你是否愿意
无论如何,我通过以编程方式在活动的on = onCreate中添加片段来解决了我的问题并将插入的内容插入到 if中。
BlockFrag rb = new BlockFrag();
FragmentManagerfragmentManager = getFragmentManager(); //声明Fragment Manager
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //声明Fragment Transaction
if(condition)
{
editor.putBoolean( redBlock,true);
fragmentTransaction.add(R.id.redFrame,rb); //添加片段
}
不用说我没有在活动布局中声明片段,也没有在片段类或片段xml布局中进行任何更改。
for the last 4 hours ive been trying to understand how to do it. to be clear, i need to add a fragment under a certain condition and for my purpose the condition can be either: a. if the parent's id matches to what i seek. b. if the fragment's id matches to what i seek.
i tried to put the condition inside the fragment itself:
public class BlockFrag extends Fragment{
SharedPreferences sp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
sp = getActivity().getSharedPreferences("myOptions", Context.MODE_PRIVATE);
int id = container.getId();//didn't work(i think the container is null for some reason
switch (id)
{
default: return inflater.inflate(R.layout.nothing, container, false);
case (R.id.redFrame): {
if (sp.getBoolean("redBlock", true)) {
return inflater.inflate(R.layout.block_frag, container, false);
}
}
case (R.id.greenFrame): {
if (sp.getBoolean("greenBlock", true)) {
return inflater.inflate(R.layout.block_frag, container, false);
}
i also tried to get the condition programatically but i cant understand how to prevent the fragment to automatically generate when i call the layout it's placed.
this is the layout it's placed for example
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/redFrame"
android:layout_weight="0.33333">
<fragment
android:name="com.example.dor.myapplication.BlockFrag"
android:id="@+id/blockRed"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/block_frag"/>
this is the fragment's layout if it matters...
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="118dp"
android:layout_height="match_parent"
android:id="@+id/blockFrag"
android:src="@drawable/block"
android:layout_weight="0.33333"
android:layout_above="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="left"
android:scaleType="fitXY"/>
thank you for helping, any kind of way to do this will help me.
Leigh thank you for your answer but i don't know if you didn't understand my question or i didn't understand your answer...
anyway, i solved my problem through programatically adding the fragment in r=the onCreate of the activity and inserting the adding to an "if"
BlockFrag rb = new BlockFrag();
FragmentManager fragmentManager = getFragmentManager ();//declaring Fragment Manager
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();// declaring Fragment Transaction
if(condition)
{
editor.putBoolean("redBlock", true);
fragmentTransaction.add(R.id.redFrame, rb);//adding the fragment
}
needless to say i didn't declare the fragment in my activity layout and didn't perform any changes in the fragment class or fragment xml layout.
这篇关于有碎片添加的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!