我试图通过点击Recyclerview内的ImageView来打开一个新片段......但没有输出
recyclerview屏幕保持不变,我要显示的片段未显示
这是我的适配器类,包含 fragmentTransaction
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private Context context;
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context) {
super();
//Getting all the superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListOfData superHero = superHeroes.get(position);
mImageLoader = MyApplication.getInstance().getImageLoader();
mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
holder.textViewName1.setText(superHero.getName());
//holder.textViewName2.setText(superHero.getName());
//holder.textViewRank.setText(String.valueOf(superHero.getRank()));
//holder.textViewRealName.setText(superHero.getRealName());
//holder.textViewCreatedBy.setText(superHero.getCreatedBy());
//holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());
//String powers = "";
/* for(int i = 0; i<superHero.getPowers().size(); i++){
powers+= superHero.getPowers().get(i);
}*/
//holder.textViewPowers.setText(powers);
holder.imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BehindTheLayoutFragment fragment = new BehindTheLayoutFragment();
FragmentTransaction ft = ((AppCompatActivity)context).getFragmentManager().beginTransaction();
ft.replace(R.id.behind_the_layout_fragment, fragment);
ft.addToBackStack(null);
ft.commit();
//((AppCompatActivity)context).getFragmentManager().beginTransaction().replace(R.id.behind_the_layout_fragment, fragment).commit();
}
});
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView imageView1;
public TextView textViewName1;
public TextView textViewName2;
public TextView textViewRank;
public TextView textViewRealName;
public TextView textViewCreatedBy;
public TextView textViewFirstAppearance;
public TextView textViewPowers;
public ViewHolder(View itemView) {
super(itemView);
imageView1 = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
textViewName1 = (TextView) itemView.findViewById(R.id.category_title_one);
/* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
*/
}
}
}
和我想通过onclicklistner打开的片段
public class BehindTheLayoutFragment extends android.app.Fragment {
private ImageLoader mImageLoader ;
private String imgUrl = "https://upload.wikimedia.org/wikipedia/commons/2/24/Blue_Tshirt.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.behind_the_layout_fragment, container, false);
mImageLoader = MyApplication.getInstance().getImageLoader();
NetworkImageView imageView = (NetworkImageView)view.findViewById(R.id.thumbnail);
imageView.setImageUrl(imgUrl, mImageLoader);
return view;
}
}
这是一个显示为弹出窗口的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="250dp"
android:layout_height="300dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:scaleType="fitXY">
</com.android.volley.toolbox.NetworkImageView>
答案 0 :(得分:1)
这样做
在你的适配器中,你应该像这样传递你的片段的一个实例。将构造函数更改为
RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context, Fragment fragment){
fragmentInstance = fragment; // Declare Fragment fragmentInstance in adapter
....
}
并在你的片段中执行此操作
RecyclerViewDataAdapter(superHeroes, getActivity(), FragmentName.this)
现在在事务中调用它
FragmentTransaction ft = fragmentInstance.getChildFragmentManager().beginTransaction();
同时将LinearLayout更改为FrameLayout。
另请注意,FragmentName是您从其调用适配器的片段的名称。
这应该有用!!