I want to use the shared element transition with a recyclerview. When I click on an Cardview item from my Recyclerview it should load the image into the Fragment and show some texz and stuff.
I already tried this :
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();
}
});
But my fragment dosent show when I click on an Item from my RecyclerView.
My Fragment
: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);
}
}
My fragment Layout:
<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>
I also added the public Interface OnFragmentInteractionListener to my Main Activity.
Thank you in advance.
My Adapter :
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();
}
}
Add an Id to your FrameLayout
:
<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>
Then change this line :
activity.getSupportFragmentManager().beginTransaction().replace(R.id.ivImage, myFragment).addToBackStack(null).commit();
to :
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, myFragment).addToBackStack(null).commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.