如何从recyclerview导航到片段?

时间:2017-06-28 10:02:24

标签: android android-fragments android-recyclerview

我的mainactivity中有一个recyclerview(带有适配器),显示不同的类别,我想点击这些类别,然后从那里导航到一个片段。我该怎么做呢?什么是最好的方法?

这是mainActivity

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_info_layout);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
    getSupportActionBar().setTitle("");
    myToolbar.setTitle(Html.fromHtml("<font color='#272d85'> </font>"));

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyAdapter();
    mRecyclerView.setAdapter(mAdapter);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) 
    MenuItemCompat.getActionView(searchItem);
    searchItem.expandActionView();
    searchView.requestFocus();
    searchView.setQueryHint("Sök efter ord");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            searchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);


}

}

我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private Context context;
private int[] cardimages = {
        R.drawable.stockholm,
        R.drawable.invandrar,
        R.drawable.goteborg,
        R.drawable.norrland,
        R.drawable.svengelska,
        R.drawable.skanska,
        R.drawable.ovrigt

};
private String[] titles = {
        "Stockholmsslang",
        "Invandrarslang",
        "Skane",
        "Norrland",
        "Svengelska",
        "Skåne",
        "Övrigt",

};


public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView itemImage;

    public ViewHolder(View itemView) {
        super(itemView);
        itemImage = (ImageView) itemView.findViewById(R.id.item_image);
        itemImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               final int position = getAdapterPosition();

               //ONclick for the recyclerview
            }

        });
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view;
    view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recylerview_main, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Context context = viewHolder.itemView.getContext();
    Picasso.with(context).load(cardimages[i]).fit().centerInside().into(((MyAdapter.ViewHolder) viewHolder).itemImage);
}

@Override
public int getItemCount() {
    return titles.length;
}


}

2 个答案:

答案 0 :(得分:0)

在Fragment中初始化适配器时声明此方法。 如果您正在使用活动,请在初始化getActivity()时删除FragmentManager。您还需要在当前活动中的某个位置放置您要放置片段的位置。

public void replaceFragment()
    {
           YourFragment yourFragment=new YourFragment();
           FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            fragmentTransaction.replace(R.id.frameLayout, yourFragment,null);
            fragmentTransaction.addToBackStack(tag);

            fragmentTransaction.commitAllowingStateLoss();
    }

现在从适配器

调用此方法
               @Override
                public void onClick(View view) {
                   final int position = getAdapterPosition();

                (Activity)context.replaceFragment();
                }

            });

//在这样的活动中初始化ypu适配器:

//声明变量是适配器

MyAdapter myAdapter=new MyAdapter(this);

在适配器中创建构造函数

 public MyAdapter(Context context)
{
  this.context=context;
}

答案 1 :(得分:0)

您无法启动类似活动的片段。您需要父视图来启动片段。在您的情况下,我想您想要在新屏幕中显示它。

在布局中创建一个带有framelayout的新活动,将片段名称发送到活动并使用此代码。如果您有多个片段,则可以使用额外内容为所有片段使用一个活动。

Intent i = new Intent(context,FragmentParentActivity.class);
i.putExtra("fragment",1);
startActivity(i);

您可以使用额外参数来决定要启动的片段(onCreate)

String fragmentTag="Fragment1";
int fragmentID = getIntent().getIntExtra("fragment",0);
if(fragmentID == 1){
  fragment = new YourFragment();
}
...
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame, fragment, fragmentTag).commit();