将标题标题recyclelerview传递给acticity

时间:2017-03-24 04:40:33

标签: android android-fragments tabs android-actionbar android-recyclerview

我需要一个帮助,将标题标题传递给点击项目recyclerview上的详细活动。然后我想将该标题显示为动作栏标题

这个带有recyclerview的标签: enter image description here

在我点击cardview上的项目后,他们将显示详细活动 我即将在动作栏标题上显示:...

enter image description here 因此标题基于包含recylerview的选项卡(类别)。 有关信息,我使用寻呼机适配器显示标题,我使用适配器与我的Recycler视图。所以我应该将标题传递给适配器吗?

mainactivity(show tab和cardview)代码:

    class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Terbaru","Pilihan", "Prediksi", "Inggris", "Spanyol","Italy","Jerman","Prancis","Indonesia"};
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new BlankFragment();
            case 1:
                return new EditorFragment();
            case 2:
                return new PrediksiFragment();
            case 3:
                return new InggrisFragment();
            case 4:
                return new SpanyolFragment();
            case 5:
                return new ItalyFragment();
            case 6:
                return new JermanFragment();
            case 7:
                return new PrancisFragment();
            case 8:
                return new IndonesiaFragment();            }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) tab.findViewById(R.id.custom_text);
        tv.setText(tabTitles[position]);
        return tab;
    }

}

我的适配器onclick cardview详细活动:

    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
    mContext = parent.getContext();
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, final int position) {
    Glide.with(mContext).load(String.valueOf(coba.get(position).getBetterFeaturedImage().getMediaDetails().getSizes().getMedium().getSourceUrl()))
            .thumbnail(0.8f)
            .override(220,220)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.mImgPost);
    holder.mTextView.setText(Html.fromHtml(String.valueOf(coba.get(position).getTitle().getRendered())));
 //   holder.mDetView.setText(Html.fromHtml(String.valueOf(coba.get(position).getContent().getRendered())));
//    holder.mDetView.setText(String.valueOf(coba.get(position).getSlug()));
    Locale locale= new Locale("id","ID");
    SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat destFormat = new SimpleDateFormat("dd-MMM-yyyy",locale); //here 'a' for AM/PM
    Date date = null;
    try {
        date = sourceFormat.parse(String.valueOf(coba.get(position).getDate()));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    final String formattedDate = destFormat.format(date);
    holder.mVerView.setText(formattedDate);
    holder.mCardView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(v.getContext(),DetailBerita.class);
            intent.putExtra("Gambar",String.valueOf(coba.get(position).getBetterFeaturedImage().getMediaDetails().getSizes().getMedium().getSourceUrl()));
            intent.putExtra("Content",String.valueOf(coba.get(position).getContent().getRendered()));
            intent.putExtra("Judul",String.valueOf(coba.get(position).getTitle().getRendered()));
            intent.putExtra("Tanggal",formattedDate);
            intent.putExtra("Link",String.valueOf(coba.get(position).getLink()));
            intent.putExtra("Tags",String.valueOf(coba.get(position).getTags()));
            v.getContext().startActivity(intent);
            System.out.println("Clicked");
        }
    });
}

我的示例框架加载回收站视图:

        public EditorFragment() {
        // Required empty public constructor
    }

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


    private void loadJSON(){
        final ProgressDialog loading = ProgressDialog.show(getActivity(),"Memuat Berita","Harap tunggu..",false,false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.sumberbola.com/wp-json/wp/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestSumbol request = retrofit.create(RequestSumbol.class);
        Call<List<Coba>> call = request.getPostDet();
        call.enqueue(new Callback<List<Coba>>() {
            @Override
            public void onResponse(Call<List<Coba>> call, Response<List<Coba>> response) {
                try {
                    List<Coba> jsonResponse = response.body();
                    data= new ArrayList<>();
                    data.addAll(jsonResponse);
                    adapter = new MyAdapter(data);
                    adapter.notifyDataSetChanged();
                    recyclerView.setAdapter(adapter);
                    loading.dismiss();
//                    bar.setVisibility(View.GONE);
                    Log.i("HASIL", "onResponse: "+data);
                }catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<List<Coba>> call, Throwable t) {
                Log.d("Error",t.getMessage());
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
        //avi = (AVLoadingIndicatorView) rootView.findViewById(R.id.barload);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        recyclerView.setHasFixedSize(true);
 //       loadJSON();
        adapter = new MyAdapter(data);
        recyclerView.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(llm);

        return rootView;
    }

}
抱歉我的英语不好。

3 个答案:

答案 0 :(得分:0)

首先从适配器获取标题并使用intent extra传递此文本,并在下一个活动工具栏中设置此文本。

getSupportActionBar().setTitle("your title");

答案 1 :(得分:0)

喜欢这个传球标题:

        fragment2 =new PrediksiFragment();
        Bundle arg= new Bundle();
        arg.putString("title","TITLE");
        fragment2.setArguments(arg);

        return fragment2;

现在在create()的片段中:

    Bundle pb=getArguments();
    String title=pb.getString("title");

答案 2 :(得分:0)

好的,有3个步骤 -

1)您需要先将标题传递给var form_to_be_submitted = null; $("#form").validate({ // Specify the validation rules rules: { type: "required", groups: { required: true } }, // Specify the validation error messages messages: { type: "Type is required", groups: { required: "Group is required" } }, submitHandler: function(form) { form_to_be_submitted = form; $.ajax({ type: "POST", url: "check_exists.php", data: { groups: $( "#groups" ).val(), type: $( "#type" ).val() }, success: function(data) { if(data == "true") { form_to_be_submitted.submit(); // It shows form.submit is not a function } else { // Displays error } } }); } }); - 结帐我的answer
   在你的情况下你将需要做类似的事情 -

fragment

2)使用switch (position) { case 0: return BlankFragment.newInstance(tabTitles[position]); 为您的片段添加标题,其行为与getArguments()的{​​{1}}类似。

您的getIntent().getExtras()将有 -

Activity

3)点击BlackFragment项时传递此标题 -
以下是可以使用String title = getArguments().getString("your_key_of_extras"); 的方法 -
创建和界面

recyclerview


interface类中创建setter for和public interface OnListClickedListener () { void onItemClicked(int position); }

Adapter

现在在interface设置public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private OnListClickedListener _listener; public MyAdapter (Context context, OnListClickedListener listener) { ... this._listener = onListClickedListener; } class MyHolder extends RecyclerView.ViewHolder { CardView mCardContainer; AddedProductHolder(View itemView) { super(itemView); .... mCardContainer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ( _listener == null ) return; _listener.OnListClicked(getAdapterPosition()); } }); } } } -

Fragment