Domanda

I have the following Fragment, IdentificationFragment

I want to have this fragment load an initial screen, then when I press a button, switch views. (Identification face, identification rfid, and identification voice) eventually. But I am having trouble handling this inside the Fragment itself.

This is what the fragment looks like currently.

public class IdentificationFragment extends Fragment {

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

    return inflater.inflate(R.layout.identification_face, container, false);
}

Here is my identification_face.xml layout:

<?xml version="1.0" encoding="utf-8"?>

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/identificationSwitcher"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent">    
<RelativeLayout 
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:onCLick="nextView"
    android:text="Next View" />
  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:gravity="center_vertical"
    android:text="@string/article2" />

More generic things, and another Relative Layout

  </RelativeLayout>
  </ViewSwitcher>

I am following the example provided Here but that uses an activity instead of a fragment.

I don't know if this is even possible due to the issues involved in targeting fragment methods via onCLick().

I tried using this code inside my fragment, but I seem to be doing more harm then good.

public void onCreate(Bundle savedInstanceState, LayoutInflater inflater) {
    super.onCreate(savedInstanceState);
    inflater.inflate(R.layout.identification_face, article_container);
    ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.identificationSwitcher);
}

Hopefully this is possible, the reason I am using fragments is to eliminate starting another activity due to the fact that my application will be loading up real-time video processing, and I would like to only use it within this fragment.

I have spent plenty of time on developer.android with this functionality and they don't provide much help.

Thanks!

È stato utile?

Soluzione

It seems like you are are almost there with your set up. You just need to find your views within the onCreateView method.

The following is a example of how you would go about setting up your ViewSwitcher and how to trigger a view switch if button1 was clicked:

ViewSwitcher mViewSwitcher;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
   View root = inflater.inflate(R.layout.identification_face, container, false);
    mViewSwitcher = root.findViewById(R.id.identificationSwitcher);
    Button button1 = root.findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener()
   { 
        @Override
        public void onClick(View v)
        {
          mViewSwitcher.setDisplayedChild(1);
        }
    });

    return root;
  }

setDisplayedChild is the method used for ViewSwitcher to switch between views. 0 is the index for your first view.

GoodLuck

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top