如何在Cardview onclicklistener上打开片段?

时间:2018-02-11 00:01:18

标签: android android-recyclerview shared-element-transition

我想通过recyclerview使用共享元素转换。当我点击我的Recyclerview中的Cardview项目时,它应该将图像加载到Fragment中并显示一些texz和东西。

我已经尝试过了:

  public MyViewHolder(View v) {
            super(v);
            //Sachen aus dem Layout zuordnen
            mCardView = (CardView) v.findViewById(R.id.card);
            mName = (TextView) v.findViewById(R.id.tvCaption);
            mBild = (ImageView) v.findViewById(R.id.ivImage);
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){

                    AppCompatActivity activity = (AppCompatActivity) view.getContext();
                    Fragment myFragment = new Details();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.ivImage, myFragment).addToBackStack(null).commit();


                }
            });

但是当我点击我的RecyclerView中的一个项目时,我的片段会显示出来。

我的片段

:package audioguide.com.myaudioguide;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Details extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public Details() {

    }


    public static Details newInstance(String param1, String param2) {
        Details fragment = new Details();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_details, container, false);
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

我的片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="audioguide.com.myaudioguide.Details">

    <ImageView
        android:id="@+id/ivProfile"
        android:transitionName="profile"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="380dp" />

</FrameLayout>

我还将公共接口OnFragmentInteractionListener添加到我的主活动中。

提前谢谢。

我的适配器:

import android.content.Intent;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private static final int TYPE_FULL = 0;
    private static final int TYPE_HALF = 1;
    private static final int TYPE_QUARTER = 2;
    ArrayList <Sights> mData;

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public CardView mCardView;
        public TextView mName;
        public ImageView mBild;

        public MyViewHolder(View v) {
            super(v);
            //Sachen aus dem Layout zuordnen
            mCardView = (CardView) v.findViewById(R.id.card);
            mName = (TextView) v.findViewById(R.id.tvCaption);
            mBild = (ImageView) v.findViewById(R.id.ivImage);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });


        }
    }


    public MyAdapter(ArrayList <Sights> Daten) {
        this.mData = Daten;

    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // 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(final MyViewHolder holder, final int position) {

            holder.mName.setText(mData.get(position).getName());
            holder.mBild.setImageBitmap(mData.get(position).getImage());

        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppCompatActivity activity = (AppCompatActivity) view.getContext();
                Fragment myFragment = new Details();
                activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, myFragment).addToBackStack(null).commit();

            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }



}

1 个答案:

答案 0 :(得分:0)

向您的FrameLayout添加ID:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="audioguide.com.myaudioguide.Details">

    <ImageView
        android:id="@+id/ivProfile"
        android:transitionName="profile"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="380dp" />

</FrameLayout>

然后改变这一行:

activity.getSupportFragmentManager().beginTransaction().replace(R.id.ivImage, myFragment).addToBackStack(null).commit();

到:

activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, myFragment).addToBackStack(null).commit();