如何在Android中单击片段打开片段

如何在Android中单击片段打开片段,android,android-fragments,fragment,Android,Android Fragments,Fragment,首先,我是android新手,很抱歉我的英语不好 我有一个显示图像的应用程序,在其中我使用带有图像库的网格 现在我需要用Gif制作一个图像库,所以我找到了这个项目 我的项目使用Fragmets。我正试图在我的项目中转换上面的项目,但是我在调用活动中遇到了问题 初始项目活动如下所示: package com.tenor.android.demo.search.activity; import android.content.Context; import android.content.Inte

首先,我是android新手,很抱歉我的英语不好

我有一个显示图像的应用程序,在其中我使用带有图像库的网格

现在我需要用Gif制作一个图像库,所以我找到了这个项目

我的项目使用Fragmets。我正试图在我的项目中转换上面的项目,但是我在调用活动中遇到了问题

初始项目活动如下所示:

package com.tenor.android.demo.search.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tenor.android.core.constant.StringConstant;
import com.tenor.android.core.model.impl.Tag;
import com.tenor.android.core.response.BaseError;
import com.tenor.android.core.util.AbstractUIUtils;
import com.tenor.android.core.widget.adapter.AbstractRVItem;
import com.tenor.android.demo.search.R;
import com.tenor.android.demo.search.adapter.TagsAdapter;
import com.tenor.android.demo.search.adapter.decorations.MainTagsItemDecoration;
import com.tenor.android.demo.search.adapter.rvitem.TagRVItem;
import com.tenor.android.demo.search.adapter.view.IMainView;
import com.tenor.android.demo.search.presenter.IMainPresenter;
import com.tenor.android.demo.search.presenter.impl.MainPresenter;
import com.tenor.android.demo.search.widget.TenorStaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * For the MainActivity, we will display a search bar followed by a stream of Tags pulled from the Tenor API.
 * Either by clicking on a tag or entering a search, SearchActivity will open.
 */
public class MainActivity extends AppCompatActivity implements IMainView{
    // Number of columns for the RecyclerView
    private static final int STAGGERED_GRID_LAYOUT_COLUMN_NUMBER = 2;
    // Minimum length a search term can be
    private static final int TEXT_QUERY_MIN_LENGTH = 2;

    // A search box for entering a search term
    public EditText mEditText;
    // RecyclerView to display the stream of Tags
    public RecyclerView mRecyclerView;

    // Api calls for MainActivity performed here
    private IMainPresenter mPresenter;
    // Adapter containing the tag items/view holders
    private TagsAdapter mTagsAdapter;

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

        mEditText = (EditText) findViewById(R.id.am_et_search);
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {

                final String query = textView.getText().toString().trim();

                if (query.length() < TEXT_QUERY_MIN_LENGTH) {
                    Toast.makeText(MainActivity.this, getString(R.string.search_error), Toast.LENGTH_LONG).show();
                    return true;
                }

                // The keyboard enter will perform the search
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    startSearch(query);
                    return true;
                }
                return false;
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.am_rv_tags);
        mRecyclerView.addItemDecoration(new MainTagsItemDecoration(getContext(), AbstractUIUtils.dpToPx(this, 2)));

        // Two column, vertical display
        final TenorStaggeredGridLayoutManager layoutManager = new TenorStaggeredGridLayoutManager(STAGGERED_GRID_LAYOUT_COLUMN_NUMBER,
                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        mTagsAdapter = new TagsAdapter<>(this);
        mRecyclerView.setAdapter(mTagsAdapter);

        mPresenter = new MainPresenter(this);
        mPresenter.getTags(getContext(), null);

    }

    private void startSearch(@Nullable final CharSequence text) {
        final String query = !TextUtils.isEmpty(text) ? text.toString().trim() : StringConstant.EMPTY;
        Intent intent = new Intent(this, SearchActivity.class);
        intent.putExtra(SearchActivity.KEY_QUERY, query);
        startActivity(intent);
    }

    @Override
    public Context getContext() {
        return getBaseContext();
    }

    @Override
    public void onReceiveReactionsSucceeded(List<Tag> tags) {

        // Map the tags into a list of TagRVItem for the mTagsAdapter
        List<AbstractRVItem> list = new ArrayList<>();
        for (Tag tag : tags) {
            list.add(new TagRVItem(TagsAdapter.TYPE_REACTION_ITEM, tag));
        }
        mTagsAdapter.insert(list, false);
    }

    @Override
    public void onReceiveReactionsFailed(BaseError error) {
        // For now, we will just display nothing if the tags fail to return
    }
}
这里呢

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //setContentView(R.layout.activity_main_gif);
    View view = inflater.inflate(R.layout.activity_main_gif, container, false);
如何调用另一个片段中的片段

private void startSearch(@Nullable final CharSequence text) {
    final String query = !TextUtils.isEmpty(text) ? text.toString().trim() : StringConstant.EMPTY;
    Intent intent = new Intent(getActivity(), SearchActivity.class);
    intent.putExtra(SearchActivity.KEY_QUERY, query);
    MainActivityGif.this.startActivity(intent);
}
如果我错过了问路,我很抱歉,让我明白我错在哪里了

private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
在你的onCreate中

 mFragmentManager = getSupportFragmentManager();
 mFragmentTransaction = mFragmentManager.beginTransaction();
和点击通话:

FragmentB fragment = new FragmentB ();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.contentFragment, fragment );
mFragmentTransaction.commit();

@ʍѳђ ઽ ૯ ท 您是否注意到他传递了参数?尝试.putExtra(SearchActivity.KEY\u查询,查询);哦,不!我的错!那么,你在哪里以及如何尝试替换或转到另一个片段?正如其他用户提到的,用replace替换另一个片段很容易。当调用其他活动(我必须转换为Fragment)时,您是否注意到我需要传递参数?朋友,我如何传递此参数?尝试.putExtra(SearchActivity.KEY\u查询,查询);您不能在片段中使用putextra,您必须使用bundle fragment fragment=new fragment();Bundle=新Bundle();bundle.putInt(键、值);fragment.setArguments(bundle);要获得bundle,请使用bundle=this.getArguments();如果(bundle!=null){int myInt=bundle.getInt(key,defaultValue);}
FragmentB fragment = new FragmentB ();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.contentFragment, fragment );
mFragmentTransaction.commit();