0

我有 android 项目,其中 ListClass 有一个 Listview。单击 Listview 的一个项目时 - 打开 CardFlipActivity ,其中包含两个片段,这些片段设置为被视为翻页动作。我需要在顶部片段上播放视频并在 CardFlipActivity 的底部片段中显示图像

1)如何从ListClass中的listview项切换位置传递(原始文件夹视频文件和图像文件)的字符串以检索以显示在CardFlipActivity的片段中。

列表类代码:

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);
}

}

CardFlip活动代码

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 回答