我尝试使用片段而不是活动,在我的自定义适配器PostAdapter
中,我使用holder.itemView.setOnClickListener
方法来传递并最后打开DetailsFragment
,但是当我单击在recyclerView上的任何项目上什么都不做
这是 PostAdapter 类
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private Context context;
private List<Item> items;
public PostAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public PostAdapter.PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.post_item, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
final Item item = items.get(position);
holder.postTitle.setText(item.getTitle());
final Document document = Jsoup.parse(item.getContent());
Elements elements = document.select("img");
// Log.d("CODE", "Image: "+elements.get(0).attr("src"));
// Log.d("Text",document.text());
holder.postDescription.setText(document.text());
Glide.with(context).load(elements.get(0)
.attr("src"))
.into(holder.postImage);
holder.itemView.setOnClickListener(view -> {
DetailsFragment detailsFragment = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putString("url", item.getUrl());
bundle.putString("itemTitle", item.getTitle());
bundle.putString("title", item.getTitle());
bundle.putString("content", item.getContent());
int youtubeThumbnailImagesetVisibility = 0;
Element element = document.body();
String youtubeThumbnailImageSrc = "";
String youTubeLink = "";
for (Element e : element.getElementsByClass
("YOUTUBE-iframe-video")) {
youtubeThumbnailImageSrc = e.attr("data-thumbnail-src");
youTubeLink = e.attr("src");
Log.e("YouTube thumbnail", youtubeThumbnailImageSrc);
Log.e("Youtube link", youTubeLink);
}
if (youtubeThumbnailImageSrc.isEmpty()) {
youtubeThumbnailImagesetVisibility = 8;
bundle.putInt("youtubeThumbnailImageSetVisibility",
youtubeThumbnailImagesetVisibility);
} else {
bundle.putString("youtubeThumbnailImageSrc", youtubeThumbnailImageSrc);
bundle.putString("youTubeLink", youTubeLink);
}
// String imageSrc = elements.get(0).attr("src");
// bundle.putString("blogImage",imageSrc);
detailsFragment.setArguments(bundle);
FragmentManager fragmentManager = ((Activity)context).getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_view,detailsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
});
}
@Override
public int getItemCount() {
return items.size();
}
public class PostViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDescription;
public PostViewHolder(View itemView) {
super(itemView);
postImage = (ImageView) itemView.findViewById(R.id.postImage);
postTitle = (TextView) itemView.findViewById(R.id.postTitle);
postDescription = (TextView) itemView.findViewById(R.id.postDescription);
}
}
}
DetailsActivity片段
public class DetailsFragment extends android.app.Fragment {
private TextView titleTextView, contentTextView;
// private ImageView blogImage;
private PicassoImageGetter imageGetter;
private ImageView youtubeThumbnailImage;
private FloatingActionButton fab;
private ScrollView scrollView;
private SpinKitView spinKitView;
private Button visitSite;
private View view;
private String url,title,content, youtubeThumbnailImageSrc, youTubeLink,sharingContent;
private int youtubeThumbnailImageSetVisibility;
public DetailsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_details, container, false);
spinKitView = (SpinKitView) view.findViewById(R.id.spin_kit);
scrollView = (ScrollView) view.findViewById(R.id.scrollView);
titleTextView = (TextView) view.findViewById(R.id.titleTextView);
contentTextView = (TextView)view.findViewById(R.id.blogContent);
fab = (FloatingActionButton) view.findViewById(R.id.fab);
youtubeThumbnailImage = (ImageView) view.findViewById(R.id.youtubeThumbnailImage);
visitSite = (Button) view.findViewById(R.id.visitSite);
if(getArguments() != null){
Bundle bundle = getArguments();
url = bundle.getString("url");
title = bundle.getString("itemTitle");
content = bundle.getString("content");
youtubeThumbnailImageSrc = bundle.getString("youtubeThumbnailImageSrc");
youTubeLink = bundle.getString("youTubeLink");
youtubeThumbnailImageSetVisibility = bundle.getInt("youtubeThumbnailImageSetVisibility");
}
// blogImage = (ImageView) findViewById(R.id.blogImage);
spinKitView.setVisibility(View.VISIBLE);
titleTextView.setText(title);
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
// Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY);
if (scrollY > 0 && fab.isShown()) {
fab.hide();
} else if (scrollY < 22) {
fab.show();
}
});
} else {
scrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
int mScrollY = scrollView.getScrollY();
if (mScrollY > 0 && fab.isShown()) {
fab.hide();
} else if (mScrollY < 22) {
fab.show();
}
});
}
fab.setOnClickListener(view -> {
sharingContent = "\n" + url;
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT,sharingContent);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
});
// String imageSrc = getIntent().getStringExtra("blogImage");
// Glide.with(getApplicationContext()).load(imageSrc).into(blogImage);
youtubeThumbnailImage.setVisibility(youtubeThumbnailImageSetVisibility);
youtubeThumbnailImage.setAdjustViewBounds(true);
Picasso.get().load(youtubeThumbnailImageSrc).into(youtubeThumbnailImage);
youtubeThumbnailImage.setOnClickListener(view -> {
Intent youTube = new Intent
(Intent.ACTION_VIEW, Uri.parse(youTubeLink));
startActivity(youTube);
});
imageGetter = new PicassoImageGetter(contentTextView, getActivity());
Spannable html;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
html = (Spannable) Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY, imageGetter, null);
} else {
html = (Spannable) Html.fromHtml(content, imageGetter, null);
}
contentTextView.setText(html);
spinKitView.setVisibility(View.GONE);
visitSite.setOnClickListener(view -> {
Bundle bundle = new Bundle();
bundle.putString("url", url );
VisitSiteFragment visitSiteFragment = new VisitSiteFragment();
visitSiteFragment.setArguments(bundle);
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.detailsFragment,visitSiteFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
});
return view;
}
}
我尝试使用android.support.v4.app
并替换此行
FragmentManager fragmentManager = ((Activity)context).getFragmentManager();
与此
FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
如本answer中所述,但这也不起作用
答案 0 :(得分:1)
活动类的通话片段
片段应附加到活动中。使接口在您的活动中实现它,然后在适配器中初始化接口。在clickListener中调用该方法,然后您可以在click调用的适当方法内进行任何活动。
holder.nameImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourInterface.OnItemclick();
}
});
请确保将其初始化。
答案 1 :(得分:0)
在 onBindViewHolder 内,您必须像这样实现它:
holder.nameImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
//Toast.makeText(mContext,position+ " pozicioni",Toast.LENGTH_LONG).show();
}
});
必须替换“ nameImage
”的地方。
答案 2 :(得分:0)
已解决
此行中问题的原因
fragmentTransaction.replace(R.id.nav_view,detailsFragment);
我试图替换导航抽屉视图,但是应该放置包含recyclerView的视图
fragmentTransaction.replace(R.id.parentLayout,detailsFragment);