我想在我的适配器中使用片段共享元素事务。按下时显示细节阅读更多按钮,但我不能这样做。我不知道我的错在哪里。任何的想法 ?
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import java.util.List;
/**
* Created by r00t on 16.12.2015.
*/
public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder>{
private List<NewsModel> newsModels;
private Context context;
private int lastPosition = -1;
public NewsListAdapter(Context context, List<NewsModel> feedItemList) {
this.newsModels = feedItemList;
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected TextView title;
protected TextView desc;
protected TextView readmore;
public ViewHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.asd);
this.desc = (TextView) view.findViewById(R.id.desc);
this.readmore = (TextView) view.findViewById(R.id.readmore);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_recyclerlist_item, parent, false);
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
setAnimation(holder.itemView, position);
holder.title.setText(newsModels.get(position).getTitle());
holder.desc.setText(newsModels.get(position).getDesc());
holder.readmore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewsFragment newsFragment = new NewsFragment();
NewsDetailFragment newsDetailFragment = new NewsDetailFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Transition changeTransform = TransitionInflater.from(v.getContext()).
inflateTransition(R.transition.change_image_transform);
Transition explodeTransform = TransitionInflater.from(v.getContext()).
inflateTransition(android.R.transition.explode);
newsFragment.setSharedElementReturnTransition(changeTransform);
newsFragment.setExitTransition(explodeTransform);
newsDetailFragment.setSharedElementEnterTransition(changeTransform);
newsDetailFragment.setEnterTransition(explodeTransform);
TextView title = (TextView) v.findViewById(R.id.asd);
FragmentTransaction fragmentTransaction = ((Activity)context).getFragmentManager()
.beginTransaction()
.replace(R.id.flContent, newsDetailFragment)
.addToBackStack("s")
.addSharedElement(title, "dsa");
fragmentTransaction.commit();
} }
});
}
@Override
public int getItemCount() {
return newsModels.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private void setAnimation(View viewToAnimate, int position)
{
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
我得到了这个错误。我不知道我要做什么。
02-08 16:30:59.565 2755-2755/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lux.lux, PID: 2755
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.getTransitionName()' on a null object reference
at android.app.BackStackRecord.addSharedElement(BackStackRecord.java:565)
at com.lux.lux.adapters.NewsListAdapter$1.onClick(NewsListAdapter.java:79)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)