0

私は Android プロジェクトを持っています - ListClass には Listview があります。Listview の項目をクリックすると、ページめくりアクションとして表示されるように設定された 2 つのフラグメントで CardFlipActivity が開きます。CardFlipActivity の一番上のフラグメントでビデオを再生し、一番下のフラグメントに画像を表示する必要があります

1) CardFlipActivity のフラグメントで表示するために取得する ListClass のリストビュー アイテム スイッチ位置から (raw フォルダーのビデオ ファイルと画像ファイル) の文字列を渡す方法。

ListClass コード:

public class ListClass extends ListActivity {
static final String VIDEO_PATH = null;
static final String IMAGE_PATH = null;
static final String VIDEO_NAME = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into ArrayList
    String[] coronary_pathology_clips = getResources().getStringArray(R.array.coronary_pathology_clips);
    ArrayList<String> coronary_pathology_clipsList = new ArrayList<String>(Arrays.asList(coronary_pathology_clips));

    // Binding resources ArrayList to CathListAdapter
    setListAdapter(new CathListAdapter(this,R.layout.list_item, coronary_pathology_clipsList));
    getListView().setBackgroundColor(Color.GRAY);
}   
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    change(position);
}
void change(int position){
      String selectedValue = (String) getListAdapter().getItem(position);
      Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(getApplicationContext(), CardFlipActivity.class);

    switch(position){  

    case 0 :
        String path1 = "android.resource://" + getPackageName() + "/" + R.raw.video1;
        String path1a = "android.resource://" + getPackageName() + "/" + R.raw.image1;

        intent.putExtra (VIDEO_PATH, path1);  
        intent.putExtra (IMAGE_PATH, path1a);               

        break;
    case 1 :
        String path2 = "android.resource://" + getPackageName() + "/" + R.raw.video2;
        String path2a = "android.resource://" + getPackageName() + "/" + R.raw.image2;

        intent.putExtra (VIDEO_PATH, path2);  
        intent.putExtra (IMAGE_PATH, path2a);   
        break;

    } 
    Bundle dataBundle = new Bundle();
    dataBundle.putString("VIDEO_NAME", selectedValue);
    intent.putExtras(dataBundle);
    startActivity(intent);
}

}

CardFlipActivity コード

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class CardFlipActivity extends Activity
    implements FragmentManager.OnBackStackChangedListener {
private Handler mHandler = new Handler();
private boolean mShowingBack = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_flip);

    if (savedInstanceState == null) {
        // If there is no saved instance state, add a fragment representing the
        // front of the card to this activity. If there is saved instance state,
        // this fragment will have already been added to the activity.
        getFragmentManager()
                .beginTransaction()
                .add(R.id.container, new CardFrontFragment())
                .commit();
    } else {
        mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);
    }

    // Monitor back stack changes to ensure the action bar shows the appropriate
    // button (either "photo" or "info").
    getFragmentManager().addOnBackStackChangedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // Add either a "photo" or "finish" button to the action bar, depending on which page
    // is currently selected.
    MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE,
            mShowingBack
                    ? R.string.action_photo
                    : R.string.action_info);
    item.setIcon(mShowingBack
            ? R.drawable.ic_action_photo
            : R.drawable.ic_action_info);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Navigate "up" the demo structure to the launchpad activity.
            // See http://developer.android.com/design/patterns/navigation.html for more.
            NavUtils.navigateUpTo(this, new Intent(this, ListClass.class));
            return true;

        case R.id.action_flip:
            flipCard();
            return true;
    }

    return super.onOptionsItemSelected(item);
}

private void flipCard() {
    if (mShowingBack) {
        getFragmentManager().popBackStack();
        return;
    }

    // Flip to the back.

    mShowingBack = true;

    // Create and commit a new fragment transaction that adds the fragment for the back of
    // the card, uses custom animations, and is part of the fragment manager's back stack.

    getFragmentManager()
            .beginTransaction()


            .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)

            .replace(R.id.container, new CardBackFragment())

            .addToBackStack(null)

            .commit();


    mHandler.post(new Runnable() {
        @Override
        public void run() {
            invalidateOptionsMenu();
        }
    });
}

@Override
public void onBackStackChanged() {
    mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);

    // When the back stack changes, invalidate the options menu (action bar).
    invalidateOptionsMenu();
}

/*A fragment representing the frontcard.*/

public static class CardFrontFragment extends Fragment {
    public CardFrontFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_card_front, container, false);

    }
}

/*A fragment representing the back card.*/

public static class CardBackFragment extends Fragment {
    public CardBackFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_card_back, container, false);
    }
}
}
4

2 に答える 2

1

文字列を別のフラグメントに渡すには、フラグメントのメソッドを使用できますsetArgument(bundle)。これらの文字列を受け取るには、getArgument()メソッドを使用できます。

于 2013-02-24T06:35:43.310 に答える
0

startactivityforresult を使用して、listclass から結果を取得できます。

于 2013-02-24T00:22:11.863 に答える